Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

類似度計算リファクタリング #60

Merged
merged 7 commits into from
Nov 15, 2024

Conversation

Kubosaka
Copy link
Collaborator

@Kubosaka Kubosaka commented Nov 15, 2024

User description

概要

  • 類似度を計算するときに、複数形などがある場合にエラーが発生することがありました
    • WordNetの Morphyを使って解決しました
  • ハイスコアの単語も返します
  • {"similarity":1.0,"highscoreWord":"bananas"}

bananasbanana
denieddeny
みたいな感じにできる

リファクタリングでは、全ての組み合わせで類似度出して、最も高いものを最高類似度として出してます
{"similarity":1.0,"highscoreWord":"bananas"}

動作確認

  • ローカル立ち上げてcurlコマンドでAPI叩いてください
curl -X POST "http://localhost:9004/similarity" \ -H "Content-Type: application/json" \ -d '{"assignmentWord": "banana", "words": ["bananas", "play", "aaaaa"]}'

※本番動作確認済
スクリーンショット 2024-11-15 18 10 46


PR Type

enhancement, tests


Description

  • 類似度計算のロジックをリファクタリングし、calcuSimilaritycalculate関数を追加しました。
  • 例外処理を追加し、類似度計算が失敗した場合にエラーメッセージを返すようにしました。
  • WordNetのMorphyを使用して、単語の異なる形態を考慮するようにしました。
  • 新しいGitHub Actionsワークフローを追加し、PRエージェントを使用してPRの自動レビューを設定しました。

Changes walkthrough 📝

Relevant files
Enhancement
main.py
類似度計算のリファクタリングと例外処理の追加                                                                     

simirality/main.py

  • 例外処理を追加して、類似度計算の失敗時にエラーメッセージを返すようにした。
  • calcuSimilarity関数を新たに作成し、類似度計算のロジックを整理。
  • calculate関数を追加し、個別の単語の類似度計算を行う。
  • WordNetのMorphyを使用して、単語の異なる形態を考慮。
  • +47/-10 
    Tests
    ai-review.yml
    GitHub Actionsワークフローの追加と設定                                                             

    .github/workflows/ai-review.yml

  • 新しいGitHub Actionsワークフローを追加。
  • PRエージェントを使用してPRの自動レビューを設定。
  • 日本語での指示を追加。
  • +38/-0   

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling
    例外処理が不十分であり、特定のエラーに対して適切なメッセージや処理が行われていない可能性があります。

    Code Duplication
    WordNetに存在しない単語のエラーハンドリングのコードが重複しています。これを関数化することでコードの重複を減らし、保守性を向上させることができます。

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    HTTP例外を使用して、エラー発生時に適切なレスポンスをクライアントに返します。

    similarity 関数の例外処理で HTTPException を使用して、クライアントに適切なHTTPステータスコードを返すようにしてください。

    simirality/main.py [40-42]

    -except:
    -    print("Error: 類似度の計算に失敗しました。課題が存在しない可能性があります。")
    -    return {"similarity": 0}
    +except Exception as e:
    +    print(f"Error: 類似度の計算に失敗しました。課題が存在しない可能性があります。詳細: {str(e)}")
    +    raise HTTPException(status_code=400, detail="類似度の計算に失敗しました。")
    Suggestion importance[1-10]: 8

    Why: Using HTTPException to handle errors in a web API is a best practice as it allows the server to send appropriate HTTP status codes and error messages to the client, improving the API's reliability and client-side error handling.

    8
    類似度計算ができない場合のエラーメッセージを詳細化します。

    calculate 関数で similarityNone の場合の処理を改善して、エラーメッセージを詳細化してください。

    simirality/main.py [92-94]

     if similarity is None:
    -    print(f"'{assignmentWord}' と '{word}' の類似度を計算できません。")
    +    print(f"'{assignmentWord}' と '{word}' の類似度を計算できません。WordNetのデータが不足している可能性があります。")
         return 0
    Suggestion importance[1-10]: 3

    Why: While the suggestion improves the detail of the error message, it's a minor enhancement as it does not fundamentally change the logic or functionality of the code.

    3
    Best practice
    例外の種類を特定して、エラーハンドリングを改善します。

    calcuSimilarity 関数内の例外処理で具体的な例外タイプを指定してください。

    simirality/main.py [64]

    -except:
    -    print(f"Error: '{word}' はWordNetに存在しません。")
    +except Exception as e:
    +    print(f"Error: '{word}' はWordNetに存在しません。詳細: {str(e)}")
    Suggestion importance[1-10]: 7

    Why: Adding a specific exception type improves error handling and debugging. This suggestion enhances the robustness of the code by providing more detailed error information.

    7
    Possible bug
    WordNetのsynset検索失敗時のエラーハンドリングを追加します。

    calculate 関数内で wn.synset の呼び出しに失敗した場合のエラーハンドリングを追加してください。

    simirality/main.py [88]

    -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
    Suggestion importance[1-10]: 6

    Why: Adding error handling for wn.synset calls prevents runtime errors and improves the stability of the function, especially when dealing with potentially unavailable data.

    6

    @Kubosaka Kubosaka self-assigned this Nov 15, 2024
    Copy link
    Contributor

    @harata-t harata-t left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    LLGGTTMM

    @Kubosaka Kubosaka merged commit 40a3b43 into main Nov 15, 2024
    1 check passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants