patruff commited on
Commit
19ee3db
·
verified ·
1 Parent(s): fba203b

Upload tool

Browse files
Files changed (1) hide show
  1. tool.py +73 -21
tool.py CHANGED
@@ -5,17 +5,27 @@ import string
5
 
6
  class ParodyWordSuggestionTool(Tool):
7
  name = "parody_word_suggester"
8
- description = """Suggests phonetically similar funny words using CMU dictionary pronunciations.
9
- Returns similar words from a curated list of funny words, along with their similarity scores."""
10
- inputs = {'target': {'type': 'string', 'description': 'The word you want to find funny 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}}
11
  output_type = "string"
12
 
13
- def forward(self, target: str, word_list_str: str, min_similarity: str = "0.7") -> str:
14
- """Get phonetically similar funny word suggestions."""
15
  import pronouncing
16
  import string
17
  import json
18
 
 
 
 
 
 
 
 
 
 
 
19
  target = target.lower().strip(string.punctuation)
20
  min_similarity = float(min_similarity)
21
  suggestions = []
@@ -40,6 +50,22 @@ class ParodyWordSuggestionTool(Tool):
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)
@@ -47,25 +73,51 @@ class ParodyWordSuggestionTool(Tool):
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({
 
5
 
6
  class ParodyWordSuggestionTool(Tool):
7
  name = "parody_word_suggester"
8
+ description = """Suggests rhyming funny words using CMU dictionary pronunciations.
9
+ Returns similar-sounding words that rhyme, especially focusing on common vowel sounds."""
10
+ 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}}
11
  output_type = "string"
12
 
13
+ def forward(self, target: str, word_list_str: str, min_similarity: str = "0.5") -> str:
14
+ """Get rhyming word suggestions."""
15
  import pronouncing
16
  import string
17
  import json
18
 
19
+ # Define vowel sound groups (common sounds that rhyme)
20
+ vowel_groups = {
21
+ 'UW1': ['UW0', 'UW1', 'UW2'], # oo sounds
22
+ 'UW0': ['UW0', 'UW1', 'UW2'],
23
+ 'UW2': ['UW0', 'UW1', 'UW2'],
24
+ 'AH1': ['AH0', 'AH1', 'AH2'], # uh sounds
25
+ 'AH0': ['AH0', 'AH1', 'AH2'],
26
+ 'AH2': ['AH0', 'AH1', 'AH2'],
27
+ }
28
+
29
  target = target.lower().strip(string.punctuation)
30
  min_similarity = float(min_similarity)
31
  suggestions = []
 
50
  target_phones = target_phones[0]
51
  target_phone_list = target_phones.split()
52
 
53
+ # Focus on the vowel sound and end consonant(s)
54
+ target_end = []
55
+ found_vowel = False
56
+ for i in range(len(target_phone_list) - 1, -1, -1):
57
+ current_phone = target_phone_list[i]
58
+ target_end.insert(0, current_phone)
59
+ # Check if current phone contains a vowel
60
+ has_vowel = False
61
+ for vowel in ['A', 'E', 'I', 'O', 'U']:
62
+ if vowel in current_phone:
63
+ has_vowel = True
64
+ found_vowel = True
65
+ break
66
+ if found_vowel:
67
+ break
68
+
69
  # Check each word
70
  for word in words:
71
  phones = pronouncing.phones_for_word(word)
 
73
  word_phones = phones[0]
74
  word_phone_list = word_phones.split()
75
 
76
+ # Get ending pattern (vowel + following consonants)
77
+ word_end = []
78
+ found_vowel = False
79
+ for i in range(len(word_phone_list) - 1, -1, -1):
80
+ current_phone = word_phone_list[i]
81
+ word_end.insert(0, current_phone)
82
+ # Check if current phone contains a vowel
83
+ has_vowel = False
84
+ for vowel in ['A', 'E', 'I', 'O', 'U']:
85
+ if vowel in current_phone:
86
+ has_vowel = True
87
+ found_vowel = True
88
+ break
89
+ if found_vowel:
90
+ break
91
+
92
+ # Calculate rhyme score
93
  matches = 0
94
+ total_checks = max(len(word_end), len(target_end))
95
 
96
+ for i in range(min(len(word_end), len(target_end))):
97
+ w_phone = word_end[i]
98
+ t_phone = target_end[i]
99
+
100
+ # Check for exact match
101
+ if w_phone == t_phone:
102
+ matches += 1
103
+ else:
104
+ # Check for vowel sound
105
+ has_vowel = False
106
+ for vowel in ['A', 'E', 'I', 'O', 'U']:
107
+ if vowel in w_phone:
108
+ has_vowel = True
109
+ break
110
+
111
+ if has_vowel:
112
+ # Check vowel groups
113
+ is_match = False
114
+ for base_vowel in vowel_groups:
115
+ if w_phone in vowel_groups[base_vowel] and t_phone in vowel_groups[base_vowel]:
116
+ is_match = True
117
+ matches += 1
118
+ break
119
 
120
+ similarity = matches / total_checks if total_checks > 0 else 0.0
121
 
122
  if similarity >= min_similarity:
123
  suggestions.append({