patruff commited on
Commit
71d9835
·
verified ·
1 Parent(s): 8d310b9

Upload tool

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -1
  2. tool.py +25 -7
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- pronouncing
2
  smolagents
 
 
 
1
  smolagents
2
+ pronouncing
tool.py CHANGED
@@ -1,8 +1,8 @@
1
  from smolagents.tools import Tool
 
2
  import pronouncing
3
  import difflib
4
  import string
5
- import json
6
 
7
  class ParodyWordSuggestionTool(Tool):
8
  name = "parody_word_suggester"
@@ -46,6 +46,14 @@ class ParodyWordSuggestionTool(Tool):
46
  return last_vowel, remaining
47
 
48
 
 
 
 
 
 
 
 
 
49
  def _vowels_match(self, v1: str, v2: str) -> bool:
50
  """Check if two vowels are in the same group."""
51
  v1 = v1.rstrip('012')
@@ -110,12 +118,17 @@ class ParodyWordSuggestionTool(Tool):
110
  # 1. Rhyme score (most important - 60%)
111
  rhyme_score = 0.0
112
  if word_vowel and target_vowel:
113
- # Check vowel match
114
  if self._vowels_match(word_vowel, target_vowel):
115
- # Perfect rhyme if endings match too
116
- if word_end == target_end:
 
 
 
117
  rhyme_score = 1.0
118
- # Partial rhyme if just the vowel matches
 
 
119
  else:
120
  rhyme_score = 0.6
121
 
@@ -127,7 +140,7 @@ class ParodyWordSuggestionTool(Tool):
127
  # 3. Overall similarity (15%)
128
  string_similarity = SequenceMatcher(None, target, word).ratio()
129
 
130
- # Combined score
131
  similarity = (rhyme_score * 0.6) + (syllable_score * 0.25) + (string_similarity * 0.15)
132
 
133
  if similarity >= min_similarity:
@@ -141,7 +154,12 @@ class ParodyWordSuggestionTool(Tool):
141
  "syllables": word_syl,
142
  "phones": word_phones,
143
  "last_vowel": word_vowel,
144
- "ending": " ".join(word_end) if word_end else ""
 
 
 
 
 
145
  })
146
 
147
  # Sort by similarity score descending
 
1
  from smolagents.tools import Tool
2
+ import json
3
  import pronouncing
4
  import difflib
5
  import string
 
6
 
7
  class ParodyWordSuggestionTool(Tool):
8
  name = "parody_word_suggester"
 
46
  return last_vowel, remaining
47
 
48
 
49
+ def _strip_stress(self, phones: list) -> list:
50
+ """Remove stress markers from phones."""
51
+ result = []
52
+ for phone in phones:
53
+ result.append(phone.rstrip('012'))
54
+ return result
55
+
56
+
57
  def _vowels_match(self, v1: str, v2: str) -> bool:
58
  """Check if two vowels are in the same group."""
59
  v1 = v1.rstrip('012')
 
118
  # 1. Rhyme score (most important - 60%)
119
  rhyme_score = 0.0
120
  if word_vowel and target_vowel:
121
+ # Check if the vowels are similar
122
  if self._vowels_match(word_vowel, target_vowel):
123
+ # Check if endings match (ignoring stress numbers)
124
+ word_end_clean = self._strip_stress(word_end)
125
+ target_end_clean = self._strip_stress(target_end)
126
+
127
+ if word_end_clean == target_end_clean:
128
  rhyme_score = 1.0
129
+ # Extra boost for exact match
130
+ if len(word) == len(target):
131
+ rhyme_score = 1.2
132
  else:
133
  rhyme_score = 0.6
134
 
 
140
  # 3. Overall similarity (15%)
141
  string_similarity = SequenceMatcher(None, target, word).ratio()
142
 
143
+ # Combined score with phonetic similarity bonus
144
  similarity = (rhyme_score * 0.6) + (syllable_score * 0.25) + (string_similarity * 0.15)
145
 
146
  if similarity >= min_similarity:
 
154
  "syllables": word_syl,
155
  "phones": word_phones,
156
  "last_vowel": word_vowel,
157
+ "ending": " ".join(word_end) if word_end else "",
158
+ "debug_info": {
159
+ "word_end_clean": word_end_clean,
160
+ "target_end_clean": target_end_clean,
161
+ "exact_match": word_end_clean == target_end_clean
162
+ }
163
  })
164
 
165
  # Sort by similarity score descending