Update utils.py
Browse files
utils.py
CHANGED
@@ -110,19 +110,20 @@ YOUTUBE_URL_2 = "https://www.youtube.com/watch?v=hdhZwyf24mE"
|
|
110 |
# Function to determine if the response is similar to predefined responses
|
111 |
def is_response_similar(response, threshold=0.7):
|
112 |
if (len(response) < 160): #nur bei sehr kurzen Antworten prüfen, ob eine Art 2ich weiß nicht dabei ist
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
126 |
return False
|
127 |
|
128 |
##################################################
|
|
|
110 |
# Function to determine if the response is similar to predefined responses
|
111 |
def is_response_similar(response, threshold=0.7):
|
112 |
if (len(response) < 160): #nur bei sehr kurzen Antworten prüfen, ob eine Art 2ich weiß nicht dabei ist
|
113 |
+
# Combine the standard responses with the user's response
|
114 |
+
combined_responses = ANTWORT_WEISS_NICHT + [response]
|
115 |
+
|
116 |
+
# Convert text to TF-IDF feature vectors
|
117 |
+
vectorizer = TfidfVectorizer()
|
118 |
+
tfidf_matrix = vectorizer.fit_transform(combined_responses)
|
119 |
+
|
120 |
+
# Compute cosine similarity between user's response and standard responses
|
121 |
+
cosine_similarities = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1])
|
122 |
+
|
123 |
+
# Check if any of the standard responses are similar to the user's response
|
124 |
+
if np.max(cosine_similarities) > threshold:
|
125 |
+
return True
|
126 |
+
return False
|
127 |
return False
|
128 |
|
129 |
##################################################
|