File size: 1,337 Bytes
727cf89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from smolagents.tools import Tool
import pronouncing
import string
import json

class WordPhoneTool(Tool):
    name = "word_phonetic_analyzer"
    description = "Analyzes the pronunciation of a word using the CMU dictionary to get its phonemes, syllable count and stress pattern"
    inputs = {'word': {'type': 'string', 'description': 'The word to analyze for pronunciation patterns'}}
    output_type = "string"

    def forward(self, word: str) -> str:
        import pronouncing
        import string
        import json
    
        word = word.lower().strip(string.punctuation)
        phones = pronouncing.phones_for_word(word)
    
        if not phones:
            result = {
                'word': word, 
                'found': False,
                'error': 'Word not found in dictionary'
            }
        else:
            primary_phones = phones[0]
            result = {
                'word': word,
                'found': True,
                'syllable_count': pronouncing.syllable_count(primary_phones),
                'phones': primary_phones.split(),
                'stresses': pronouncing.stresses(primary_phones)
            }
        
        return json.dumps(result, indent=2)


    def __init__(self, *args, **kwargs):
        self.is_initialized = False