Skip to content

Commit f30e5a6

Browse files
authored
Merge pull request #42 from TUK-DP/fix/39
[fix] 퀴즈를 만들 수 없는 경우 일기만 저장하도록 구조 수정
2 parents 2821c0a + 2bd1234 commit f30e5a6

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

diary/textrank.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,20 @@ def __init__(self, content):
118118
# 명사 추출
119119
nouns = SentenceTokenizer.get_nouns(self.sentences)
120120

121-
# 가중치 그래프 객체 생성
122-
matrix = GraphMatrix(nouns)
123-
# 문장별 가중치 그래프 [문장수, 문장수], {index: 문장} 사전
124-
# sent_graph, sent_vocab = matrix.get_sent_graph_vocab()
125-
# 단어별 가중치 그래프 [단어수, 단어수], {index: 단어} 사전
126-
words_graph, word_vocab = matrix.get_words_graph_vocab()
127-
128-
# (문장, index, 가중치) 리스트 생성
129-
# sent_rank = [(sent_vocab[index], index, weight) for index, weight in (Rank.get_ranks(sent_graph).items())]
130-
# weight 기준으로 정렬
131-
# self.sorted_sent_rank = sorted(sent_rank, key=lambda k: k[2], reverse=True)
132-
133-
# (단어, index, 가중치) 리스트 생성
134-
word_rank_idx = [(word_vocab[index], index, weight) for index, weight in Rank.get_ranks(words_graph).items()]
135-
# weight 기준으로 정렬
136-
self.sorted_word_rank = sorted(word_rank_idx, key=lambda k: k[2], reverse=True)
121+
if nouns:
122+
# 가중치 그래프 객체 생성
123+
matrix = GraphMatrix(nouns)
124+
# 문장별 가중치 그래프 [문장수, 문장수], {index: 문장} 사전
125+
# sent_graph, sent_vocab = matrix.get_sent_graph_vocab()
126+
# 단어별 가중치 그래프 [단어수, 단어수], {index: 단어} 사전
127+
words_graph, word_vocab = matrix.get_words_graph_vocab()
128+
129+
# (단어, index, 가중치) 리스트 생성
130+
word_rank_idx = [(word_vocab[index], index, weight) for index, weight in Rank.get_ranks(words_graph).items()]
131+
# weight 기준으로 정렬
132+
self.sorted_word_rank = sorted(word_rank_idx, key=lambda k: k[2], reverse=True)
133+
else:
134+
self.sorted_word_rank = []
137135

138136
# sent_size 개의 문장 요약
139137
# def summarize(self, sent_size=3):

diary/views.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ def post(self, request):
2727
# Diary 객체 생성
2828
newDiary = Diary.objects.create(user=findUser, title=request.get('title'))
2929

30-
sentence = request.get('content')
31-
30+
content = request.get('content')
3231
# Sentences 객체 생성
33-
newSentence = Sentences.objects.create(sentence=sentence, diary=newDiary)
32+
newSentence = Sentences.objects.create(sentence=content, diary=newDiary)
3433

3534
# 키워드 추출
36-
memory = TextRank(content=sentence)
35+
memory = TextRank(content=content)
36+
37+
if memory is None:
38+
return ApiResponse.on_success(
39+
result=SentenceSimpleSerializer(newSentence).data,
40+
response_status=status.HTTP_201_CREATED
41+
)
42+
3743
# 키워드 추출 후 가중치가 높은 키워드 5개로 퀴즈 생성
3844
question, keyword = make_quiz(memory, keyword_size=5)
3945

@@ -71,13 +77,19 @@ def post(self, request):
7177
for sentence in sentences:
7278
sentence.keywords.all().delete()
7379
sentence.delete()
74-
75-
# Sentence 객체 생성
80+
7681
content = request.get('content')
7782
newSentence = Sentences.objects.create(sentence=content, diary=findDiary)
7883

7984
# 키워드 추출
8085
memory = TextRank(content=content)
86+
87+
if memory is None:
88+
return ApiResponse.on_success(
89+
result=SentenceSimpleSerializer(sentence).data,
90+
response_status=status.HTTP_201_CREATED
91+
)
92+
8193
# 키워드 추출 후 가중치가 높은 키워드 5개로 퀴즈 생성
8294
question, keyword = make_quiz(memory, keyword_size=5)
8395

0 commit comments

Comments
 (0)