patruff commited on
Commit
85a2c64
·
verified ·
1 Parent(s): ae367ca

Upload tool

Browse files
Files changed (1) hide show
  1. tool.py +24 -19
tool.py CHANGED
@@ -1,7 +1,7 @@
1
  from smolagents.tools import Tool
2
- import string
3
- import json
4
  import pronouncing
 
 
5
 
6
  class WordPhoneTool(Tool):
7
  name = "word_phonetic_analyzer"
@@ -94,9 +94,6 @@ class WordPhoneTool(Tool):
94
  def _calculate_similarity(self, word1, phones1, word2, phones2):
95
  import pronouncing
96
 
97
- phone_list1 = phones1.split()
98
- phone_list2 = phones2.split()
99
-
100
  # Initialize variables before use
101
  last_vowel1 = None
102
  last_vowel2 = None
@@ -108,12 +105,20 @@ class WordPhoneTool(Tool):
108
  end2_clean = []
109
  i = 0 # Initialize i for loop variable
110
 
 
 
 
111
  # Get last syllable components
112
  result1 = self._get_last_syllable(phone_list1)
113
  result2 = self._get_last_syllable(phone_list2)
114
  last_vowel1, word1_end = result1
115
  last_vowel2, word2_end = result2
116
 
 
 
 
 
 
117
  # Calculate rhyme score (most important)
118
  rhyme_score = 0.0
119
  if last_vowel1 and last_vowel2:
@@ -122,37 +127,37 @@ class WordPhoneTool(Tool):
122
  end2_clean = self._strip_stress(word2_end)
123
 
124
  if end1_clean == end2_clean:
125
- rhyme_score = 1.0 # Perfect rhyme
126
  else:
 
127
  common_length = min(len(end1_clean), len(end2_clean))
128
  matched = 0
129
  for i in range(common_length):
130
  if end1_clean[i] == end2_clean[i]:
131
  matched += 1
132
- rhyme_score = 0.7 + (0.3 * (matched / max(len(end1_clean), len(end2_clean))))
133
-
134
- # Calculate syllable pattern similarity
135
- syl1 = pronouncing.syllable_count(phones1)
136
- syl2 = pronouncing.syllable_count(phones2)
137
- syllable_score = 1.0 if syl1 == syl2 else 1 - (abs(syl1 - syl2) / max(syl1, syl2))
138
-
139
  # Calculate stress pattern similarity
140
  stress1 = pronouncing.stresses(phones1)
141
  stress2 = pronouncing.stresses(phones2)
142
  stress_score = 1.0 if stress1 == stress2 else 0.5
143
 
144
- # Weighted combination prioritizing rhyming
145
  total_similarity = (
146
- (rhyme_score * 0.8) + # Rhyming is most important (80%)
147
- (syllable_score * 0.15) + # Syllable count has some importance (15%)
148
- (stress_score * 0.05) # Stress pattern has minimal weight (5%)
149
  )
150
 
 
 
 
151
  return {
152
  "similarity": round(total_similarity, 3),
153
  "rhyme_score": round(rhyme_score, 3),
154
- "syllable_score": round(syllable_score, 3),
155
- "stress_score": round(stress_score, 3)
 
156
  }
157
 
158
 
 
1
  from smolagents.tools import Tool
 
 
2
  import pronouncing
3
+ import json
4
+ import string
5
 
6
  class WordPhoneTool(Tool):
7
  name = "word_phonetic_analyzer"
 
94
  def _calculate_similarity(self, word1, phones1, word2, phones2):
95
  import pronouncing
96
 
 
 
 
97
  # Initialize variables before use
98
  last_vowel1 = None
99
  last_vowel2 = None
 
105
  end2_clean = []
106
  i = 0 # Initialize i for loop variable
107
 
108
+ phone_list1 = phones1.split()
109
+ phone_list2 = phones2.split()
110
+
111
  # Get last syllable components
112
  result1 = self._get_last_syllable(phone_list1)
113
  result2 = self._get_last_syllable(phone_list2)
114
  last_vowel1, word1_end = result1
115
  last_vowel2, word2_end = result2
116
 
117
+ # Calculate length similarity score first
118
+ phone_diff = abs(len(phone_list1) - len(phone_list2))
119
+ max_phones = max(len(phone_list1), len(phone_list2))
120
+ length_score = 1.0 if phone_diff == 0 else 1.0 - (phone_diff / max_phones)
121
+
122
  # Calculate rhyme score (most important)
123
  rhyme_score = 0.0
124
  if last_vowel1 and last_vowel2:
 
127
  end2_clean = self._strip_stress(word2_end)
128
 
129
  if end1_clean == end2_clean:
130
+ rhyme_score = 1.0 # Perfect rhyme, capped at 1.0
131
  else:
132
+ # Partial rhyme based on ending similarity
133
  common_length = min(len(end1_clean), len(end2_clean))
134
  matched = 0
135
  for i in range(common_length):
136
  if end1_clean[i] == end2_clean[i]:
137
  matched += 1
138
+ rhyme_score = 0.6 * (matched / max(len(end1_clean), len(end2_clean)))
139
+
 
 
 
 
 
140
  # Calculate stress pattern similarity
141
  stress1 = pronouncing.stresses(phones1)
142
  stress2 = pronouncing.stresses(phones2)
143
  stress_score = 1.0 if stress1 == stress2 else 0.5
144
 
145
+ # Weighted combination prioritizing rhyming and length
146
  total_similarity = (
147
+ (rhyme_score * 0.6) + # Rhyming most important (60%)
148
+ (length_score * 0.3) + # Length similarity next (30%)
149
+ (stress_score * 0.1) # Stress pattern least important (10%)
150
  )
151
 
152
+ # Ensure total similarity is capped at 1.0
153
+ total_similarity = min(1.0, total_similarity)
154
+
155
  return {
156
  "similarity": round(total_similarity, 3),
157
  "rhyme_score": round(rhyme_score, 3),
158
+ "length_score": round(length_score, 3),
159
+ "stress_score": round(stress_score, 3),
160
+ "phone_length_difference": phone_diff
161
  }
162
 
163