patruff commited on
Commit
6280809
·
verified ·
1 Parent(s): 62a91fd

Upload tool

Browse files
Files changed (1) hide show
  1. tool.py +23 -20
tool.py CHANGED
@@ -1,7 +1,7 @@
1
  from smolagents.tools import Tool
2
  import pronouncing
3
- import json
4
  import string
 
5
 
6
  class ParodyWordSuggestionTool(Tool):
7
  name = "parody_word_suggester"
@@ -52,6 +52,16 @@ class ParodyWordSuggestionTool(Tool):
52
  max_length = max(len(word_phone_list), len(target_phone_list))
53
  min_length = min(len(word_phone_list), len(target_phone_list))
54
 
 
 
 
 
 
 
 
 
 
 
55
  for i in range(max_length):
56
  if i >= min_length:
57
  break
@@ -65,30 +75,21 @@ class ParodyWordSuggestionTool(Tool):
65
  # Match without stress numbers
66
  elif w_phone.rstrip('012') == t_phone.rstrip('012'):
67
  phonetic_matches += 0.8
68
- # Consonant match at start
69
- else:
70
- # Check if current phone is a consonant
71
- has_vowel = False
72
- vowels = ['A', 'E', 'I', 'O', 'U']
73
- for vowel in vowels:
74
- if vowel in w_phone:
75
- has_vowel = True
76
- break
77
-
78
- if i == 0 and not has_vowel:
79
- if w_phone.rstrip('012') == t_phone.rstrip('012'):
80
- phonetic_matches += 0.5
81
 
82
- phonetic_similarity = phonetic_matches / max_length
83
 
84
- # Calculate rhyme similarity (focusing on end phones)
85
- rhyme_score = 1.0 if word_phone_list[-1] == target_phone_list[-1] else 0.0
86
  if len(word_phone_list) > 1 and len(target_phone_list) > 1:
87
- if word_phone_list[-2] == target_phone_list[-2]:
 
88
  rhyme_score += 1.0
 
 
89
 
90
- # Combined score (weighing both phonetic similarity and rhyming)
91
- similarity = (phonetic_similarity * 0.6) + (rhyme_score * 0.4)
 
92
 
93
  if similarity >= min_similarity:
94
  suggestions.append({
@@ -96,6 +97,8 @@ class ParodyWordSuggestionTool(Tool):
96
  "similarity": round(similarity, 3),
97
  "phonetic_similarity": round(phonetic_similarity, 3),
98
  "rhyme_score": round(rhyme_score, 3),
 
 
99
  "syllables": pronouncing.syllable_count(word_phones),
100
  "phones": word_phones,
101
  })
 
1
  from smolagents.tools import Tool
2
  import pronouncing
 
3
  import string
4
+ import json
5
 
6
  class ParodyWordSuggestionTool(Tool):
7
  name = "parody_word_suggester"
 
52
  max_length = max(len(word_phone_list), len(target_phone_list))
53
  min_length = min(len(word_phone_list), len(target_phone_list))
54
 
55
+ # Initial consonant bonus
56
+ initial_match_score = 0
57
+ if word_phone_list[0].rstrip('012') == target_phone_list[0].rstrip('012'):
58
+ initial_match_score = 1.0 # Reduced bonus for matching first consonant
59
+
60
+ # Check if syllable counts match
61
+ target_syl = pronouncing.syllable_count(target_phones)
62
+ word_syl = pronouncing.syllable_count(word_phones)
63
+ syllable_match = 1.0 if target_syl == word_syl else 0.0
64
+
65
  for i in range(max_length):
66
  if i >= min_length:
67
  break
 
75
  # Match without stress numbers
76
  elif w_phone.rstrip('012') == t_phone.rstrip('012'):
77
  phonetic_matches += 0.8
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
+ phonetic_similarity = (phonetic_matches / max_length) + initial_match_score
80
 
81
+ # Rhyme score (focusing on vowel and end consonant)
82
+ rhyme_score = 0
83
  if len(word_phone_list) > 1 and len(target_phone_list) > 1:
84
+ # Check final syllable (vowel + final consonant)
85
+ if word_phone_list[-1] == target_phone_list[-1]: # End consonant match
86
  rhyme_score += 1.0
87
+ if word_phone_list[-2] == target_phone_list[-2]: # Vowel match
88
+ rhyme_score += 2.0 # Higher weight for vowel match
89
 
90
+ # Combined score with new weights prioritizing rhyme
91
+ # 50% rhyme, 30% syllable match, 20% phonetic similarity
92
+ similarity = (rhyme_score * 0.5) + (syllable_match * 0.3) + (phonetic_similarity * 0.2)
93
 
94
  if similarity >= min_similarity:
95
  suggestions.append({
 
97
  "similarity": round(similarity, 3),
98
  "phonetic_similarity": round(phonetic_similarity, 3),
99
  "rhyme_score": round(rhyme_score, 3),
100
+ "syllable_match": syllable_match,
101
+ "initial_match": initial_match_score > 0,
102
  "syllables": pronouncing.syllable_count(word_phones),
103
  "phones": word_phones,
104
  })