word-phone / tool.py
patruff's picture
Upload tool
727cf89 verified
raw
history blame
1.34 kB
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