Spaces:
Configuration error
Configuration error
Upload tool
Browse files- requirements.txt +1 -1
- tool.py +22 -13
requirements.txt
CHANGED
@@ -1,2 +1,2 @@
|
|
1 |
-
smolagents
|
2 |
pronouncing
|
|
|
|
|
|
1 |
pronouncing
|
2 |
+
smolagents
|
tool.py
CHANGED
@@ -1,14 +1,14 @@
|
|
1 |
from smolagents.tools import Tool
|
2 |
import pronouncing
|
|
|
3 |
import json
|
4 |
import string
|
5 |
-
import difflib
|
6 |
|
7 |
class ParodyWordSuggestionTool(Tool):
|
8 |
name = "parody_word_suggester"
|
9 |
description = """Suggests rhyming funny words using CMU dictionary pronunciations.
|
10 |
Returns similar-sounding words that rhyme, especially focusing on common vowel sounds."""
|
11 |
-
inputs = {'target': {'type': 'string', 'description': 'The word you want to find rhyming alternatives for'}, 'word_list_str': {'type': 'string', 'description': 'JSON string of word list (e.g. \'["word1", "word2"]\')'}, 'min_similarity': {'type': 'string', 'description': 'Minimum similarity threshold (0.0-1.0)', 'nullable': True, 'default': '0.5'}}
|
12 |
output_type = "string"
|
13 |
VOWEL_REF = "AH,AX|UH|AE,EH|IY,IH|AO,AA|UW|AY,EY|OW,AO|AW,AO|OY,OW|ER,AXR"
|
14 |
CONSONANT_REF = "M,N,NG|P,B|T,D|K,G|F,V|TH,DH|S,Z|SH,ZH|L,R|W,Y"
|
@@ -29,6 +29,16 @@ class ParodyWordSuggestionTool(Tool):
|
|
29 |
return groups
|
30 |
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
def _get_last_syllable(self, phones: list) -> tuple:
|
33 |
"""Extract the last syllable (vowel + remaining consonants)."""
|
34 |
last_vowel_idx = -1
|
@@ -173,7 +183,7 @@ class ParodyWordSuggestionTool(Tool):
|
|
173 |
}
|
174 |
|
175 |
|
176 |
-
def forward(self, target: str, word_list_str: str, min_similarity: str = "0.5") -> str:
|
177 |
import pronouncing
|
178 |
import string
|
179 |
import json
|
@@ -205,40 +215,38 @@ class ParodyWordSuggestionTool(Tool):
|
|
205 |
"suggestions": []
|
206 |
}, indent=2)
|
207 |
|
208 |
-
# Get target pronunciation
|
209 |
-
target_phones =
|
210 |
if not target_phones:
|
211 |
return json.dumps({
|
212 |
-
"error": f"Target word '{target}' not found in
|
213 |
"suggestions": []
|
214 |
}, indent=2)
|
215 |
|
216 |
-
# Filter word list
|
217 |
valid_words = []
|
218 |
invalid_words = []
|
219 |
for word in words:
|
220 |
word = word.lower().strip(string.punctuation)
|
221 |
-
if
|
222 |
valid_words.append(word)
|
223 |
else:
|
224 |
invalid_words.append(word)
|
225 |
|
226 |
if not valid_words:
|
227 |
return json.dumps({
|
228 |
-
"error": "No valid words found in
|
229 |
"invalid_words": invalid_words,
|
230 |
"suggestions": []
|
231 |
}, indent=2)
|
232 |
|
233 |
-
target_phones = target_phones[0]
|
234 |
target_phone_list = target_phones.split()
|
235 |
target_vowel, target_end = self._get_last_syllable(target_phone_list)
|
236 |
|
237 |
# Check each word
|
238 |
for word in valid_words:
|
239 |
-
|
240 |
-
if
|
241 |
-
word_phones = phones[0]
|
242 |
similarity_result = self._calculate_similarity(word, word_phones, target, target_phones)
|
243 |
|
244 |
if similarity_result["similarity"] >= min_similarity:
|
@@ -254,6 +262,7 @@ class ParodyWordSuggestionTool(Tool):
|
|
254 |
"phones": word_phones,
|
255 |
"last_vowel": word_vowel,
|
256 |
"ending": " ".join(word_end) if word_end else "",
|
|
|
257 |
"details": similarity_result["details"]
|
258 |
})
|
259 |
|
|
|
1 |
from smolagents.tools import Tool
|
2 |
import pronouncing
|
3 |
+
import difflib
|
4 |
import json
|
5 |
import string
|
|
|
6 |
|
7 |
class ParodyWordSuggestionTool(Tool):
|
8 |
name = "parody_word_suggester"
|
9 |
description = """Suggests rhyming funny words using CMU dictionary pronunciations.
|
10 |
Returns similar-sounding words that rhyme, especially focusing on common vowel sounds."""
|
11 |
+
inputs = {'target': {'type': 'string', 'description': 'The word you want to find rhyming alternatives for'}, 'word_list_str': {'type': 'string', 'description': 'JSON string of word list (e.g. \'["word1", "word2"]\')'}, 'min_similarity': {'type': 'string', 'description': 'Minimum similarity threshold (0.0-1.0)', 'nullable': True, 'default': '0.5'}, 'custom_phones': {'type': 'object', 'description': 'Optional dictionary of custom word pronunciations', 'nullable': True, 'default': None}}
|
12 |
output_type = "string"
|
13 |
VOWEL_REF = "AH,AX|UH|AE,EH|IY,IH|AO,AA|UW|AY,EY|OW,AO|AW,AO|OY,OW|ER,AXR"
|
14 |
CONSONANT_REF = "M,N,NG|P,B|T,D|K,G|F,V|TH,DH|S,Z|SH,ZH|L,R|W,Y"
|
|
|
29 |
return groups
|
30 |
|
31 |
|
32 |
+
def _get_word_phones(self, word, custom_phones=None):
|
33 |
+
"""Get phones for a word, checking custom dictionary first."""
|
34 |
+
if custom_phones and word in custom_phones:
|
35 |
+
return custom_phones[word]["primary_phones"]
|
36 |
+
|
37 |
+
import pronouncing
|
38 |
+
phones = pronouncing.phones_for_word(word)
|
39 |
+
return phones[0] if phones else None
|
40 |
+
|
41 |
+
|
42 |
def _get_last_syllable(self, phones: list) -> tuple:
|
43 |
"""Extract the last syllable (vowel + remaining consonants)."""
|
44 |
last_vowel_idx = -1
|
|
|
183 |
}
|
184 |
|
185 |
|
186 |
+
def forward(self, target: str, word_list_str: str, min_similarity: str = "0.5", custom_phones: dict = None) -> str:
|
187 |
import pronouncing
|
188 |
import string
|
189 |
import json
|
|
|
215 |
"suggestions": []
|
216 |
}, indent=2)
|
217 |
|
218 |
+
# Get target pronunciation using custom phones
|
219 |
+
target_phones = self._get_word_phones(target, custom_phones)
|
220 |
if not target_phones:
|
221 |
return json.dumps({
|
222 |
+
"error": f"Target word '{target}' not found in dictionary or custom phones",
|
223 |
"suggestions": []
|
224 |
}, indent=2)
|
225 |
|
226 |
+
# Filter word list checking both CMU and custom phones
|
227 |
valid_words = []
|
228 |
invalid_words = []
|
229 |
for word in words:
|
230 |
word = word.lower().strip(string.punctuation)
|
231 |
+
if self._get_word_phones(word, custom_phones):
|
232 |
valid_words.append(word)
|
233 |
else:
|
234 |
invalid_words.append(word)
|
235 |
|
236 |
if not valid_words:
|
237 |
return json.dumps({
|
238 |
+
"error": "No valid words found in dictionary or custom phones",
|
239 |
"invalid_words": invalid_words,
|
240 |
"suggestions": []
|
241 |
}, indent=2)
|
242 |
|
|
|
243 |
target_phone_list = target_phones.split()
|
244 |
target_vowel, target_end = self._get_last_syllable(target_phone_list)
|
245 |
|
246 |
# Check each word
|
247 |
for word in valid_words:
|
248 |
+
word_phones = self._get_word_phones(word, custom_phones)
|
249 |
+
if word_phones:
|
|
|
250 |
similarity_result = self._calculate_similarity(word, word_phones, target, target_phones)
|
251 |
|
252 |
if similarity_result["similarity"] >= min_similarity:
|
|
|
262 |
"phones": word_phones,
|
263 |
"last_vowel": word_vowel,
|
264 |
"ending": " ".join(word_end) if word_end else "",
|
265 |
+
"is_custom": word in custom_phones if custom_phones else False,
|
266 |
"details": similarity_result["details"]
|
267 |
})
|
268 |
|