@@ -49,7 +49,31 @@ def evaluation_function(response, answer, params):
4949 # "feedback": f"Cannot determine if the answer is correct. Please provide more details about '{keyword}"
5050 # }
5151
52-
52+ # params of the form {'keyphrase': ['phrase1', 'phrase2', ...]}
53+ if params is not None and "keyphrases" in params :
54+ keyphrases = params ["keyphrases" ]
55+ for keyphrase in keyphrases :
56+ response_tokens = preprocess_tokens (response )
57+ keyphrase_tokens = preprocess_tokens (keyphrase )
58+ window_size = len (keyphrase_tokens )
59+ i = 0
60+ found = False
61+ while i + window_size <= len (response_tokens ):
62+ response_substring = " " .join (response_tokens [i :i + window_size ])
63+ score = sentence_similarity_mean_w2v (response_substring , keyphrase )
64+ i += 1
65+ if score > 0.75 :
66+ found = True
67+ continue
68+ if not found :
69+ return {
70+ "is_correct" : False ,
71+ "result" : {
72+ "similarity_value" : w2v_similarity ,
73+ "Problematic_word" : keyphrase
74+ },
75+ "feedback" : f"Cannot determine if the answer is correct. Could not identify '{ keyphrase } "
76+ }
5377
5478 if w2v_similarity > 0.75 :
5579 return {
@@ -151,6 +175,8 @@ def sentence_similarity_mean_w2v(response: str, answer: str):
151175 answer = preprocess_tokens (answer )
152176 response_embeddings = [w2v [word ] for word in response if w2v .has_index_for (word )]
153177 answer_embeddings = [w2v [word ] for word in answer if w2v .has_index_for (word )]
178+ if len (response_embeddings ) == 0 or len (answer_embeddings ) == 0 :
179+ return 0
154180 response_vector = np .mean (response_embeddings , axis = 0 )
155181 answer_vector = np .mean (answer_embeddings , axis = 0 )
156182 return float (np .dot (response_vector , answer_vector ) / (np .linalg .norm (response_vector ) * np .linalg .norm (answer_vector )))
@@ -159,6 +185,6 @@ def sentence_similarity_mean_w2v(response: str, answer: str):
159185if __name__ == "__main__" :
160186 pass
161187 # print(time.process_time())
162- # print(evaluation_function("density, velocity,", "Density, Velocity, Viscosity, Length", None ))
188+ # print(evaluation_function("density, velocity,Visc ", "Density, Velocity, Viscosity, Length", {'keyphrases': ['Density', 'Velocity', 'Viscosity', 'Length']} ))
163189 # print(evaluation_function("test", "test", None))
164190 # print(time.process_time())
0 commit comments