patruff commited on
Commit
33a37e4
·
verified ·
1 Parent(s): 69a6758

Upload tool

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -1
  2. tool.py +27 -8
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- smolagents
2
  pronouncing
 
 
 
1
  pronouncing
2
+ smolagents
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 ParodyWordSuggestionTool(Tool):
7
  name = "parody_word_suggester"
@@ -37,24 +37,42 @@ class ParodyWordSuggestionTool(Tool):
37
  "suggestions": []
38
  }, indent=2)
39
 
40
- target_phones = target_phones[0] # Change: Don't split here
 
41
 
42
  # Check each word
43
  for word in words:
44
  phones = pronouncing.phones_for_word(word)
45
  if phones: # Only process if word is in dictionary
46
- phones = phones[0] # Change: Don't split this either
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- # Calculate similarity using overlap (modifying to work with full phone strings)
49
- overlap = len(set(phones.split()) & set(target_phones.split()))
50
- total = min(len(phones.split()), len(target_phones.split()))
51
- similarity = overlap / total if total > 0 else 0.0
52
 
53
  if similarity >= min_similarity:
54
  suggestions.append({
55
  "word": word,
56
  "similarity": round(similarity, 3),
57
- "syllables": pronouncing.syllable_count(phones) # This should work now as phones is a string
 
58
  })
59
 
60
  # Sort by similarity score descending
@@ -63,6 +81,7 @@ class ParodyWordSuggestionTool(Tool):
63
  result = {
64
  "target": target,
65
  "target_syllables": pronouncing.syllable_count(target_phones),
 
66
  "suggestions": suggestions
67
  }
68
 
 
1
  from smolagents.tools import Tool
 
2
  import json
3
  import pronouncing
4
+ import string
5
 
6
  class ParodyWordSuggestionTool(Tool):
7
  name = "parody_word_suggester"
 
37
  "suggestions": []
38
  }, indent=2)
39
 
40
+ target_phones = target_phones[0]
41
+ target_phone_list = target_phones.split()
42
 
43
  # Check each word
44
  for word in words:
45
  phones = pronouncing.phones_for_word(word)
46
  if phones: # Only process if word is in dictionary
47
+ word_phones = phones[0]
48
+ word_phone_list = word_phones.split()
49
+
50
+ # Calculate position-aware similarity
51
+ matches = 0
52
+ length = max(len(word_phone_list), len(target_phone_list))
53
+
54
+ # Compare phones at each position
55
+ for i in range(length):
56
+ if i < len(word_phone_list) and i < len(target_phone_list):
57
+ # Get current phones
58
+ current_word_phone = word_phone_list[i]
59
+ current_target_phone = target_phone_list[i]
60
+
61
+ # Exact match at same position
62
+ if current_word_phone == current_target_phone:
63
+ matches += 1
64
+ # Partial match (just the consonant or vowel part)
65
+ elif current_word_phone.rstrip('012') == current_target_phone.rstrip('012'):
66
+ matches += 0.5
67
 
68
+ similarity = matches / length if length > 0 else 0.0
 
 
 
69
 
70
  if similarity >= min_similarity:
71
  suggestions.append({
72
  "word": word,
73
  "similarity": round(similarity, 3),
74
+ "syllables": pronouncing.syllable_count(word_phones),
75
+ "phones": word_phones,
76
  })
77
 
78
  # Sort by similarity score descending
 
81
  result = {
82
  "target": target,
83
  "target_syllables": pronouncing.syllable_count(target_phones),
84
+ "target_phones": target_phones,
85
  "suggestions": suggestions
86
  }
87