Skip to content

Commit

Permalink
aiレビュー対応
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubosaka committed Nov 15, 2024
1 parent fd21f9c commit a8ce786
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Binary file modified simirality/__pycache__/main.cpython-311.pyc
Binary file not shown.
23 changes: 14 additions & 9 deletions simirality/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List

import nltk
from fastapi import FastAPI
from fastapi import FastAPI, HTTPException
from nltk.corpus import wordnet as wn
from pydantic import BaseModel
from starlette.middleware.cors import CORSMiddleware
Expand Down Expand Up @@ -36,10 +36,10 @@ async def similarity(reqWords: Words):

try:
highscore, highscore_word = calcuSimilarity(assignmentWord, words)
except:
except Exception as e:
# 例外処理
print("Error: 類似度の計算に失敗しました。課題が存在しない可能性があります。")
return {"similarity": 0}
print(f"Error: 類似度の計算に失敗しました。課題が存在しない可能性があります。詳細: {str(e)}")
raise HTTPException(status_code=400, detail="類似度の計算に失敗しました。")

return {"similarity": highscore, "highscoreWord": highscore_word}

Expand All @@ -60,8 +60,8 @@ def calcuSimilarity(assignmentWord, words):
if similarity > highscore:
highscore = similarity
highscore_word = word
except:
print(f"Error: '{word}' はWordNetに存在しません。")
except Exception as e:
print(f"Error: '{e}'")

# WordNet にない形式を検索
wordByMorphy = wn.morphy(word)
Expand All @@ -76,16 +76,21 @@ def calcuSimilarity(assignmentWord, words):
highscore = similarityByMorphy
highscore_word = word

except:
print(f"Error: '{word}' はWordNetに存在しません。")
except Exception as e:
print(f"Error: '{e}'")

return highscore, highscore_word

def calculate(assignmentWord_synset, word, assignmentWord):
if assignmentWord == word:
return 1.0

word_synset = wn.synset(f"{word}.n.01")
# word_synset = wn.synset(f"{word}.n.01")
try:
word_synset = wn.synset(f"{word}.n.01")
except Exception as e:
print(f"Error: WordNetに '{word}' のsynsetが見つかりません。詳細: {str(e)}")
return 0

similarity = assignmentWord_synset.wup_similarity(word_synset)

Expand Down

0 comments on commit a8ce786

Please sign in to comment.