首页 > 解决方案 > 有没有办法不让词汇模块的单词定义出错?

问题描述

我正在制作一个脚本,它接受一个随机单词并获取该单词的定义,然后它将其转换为语音并播放它,但由于某种原因,每当我尝试获取该单词的定义时,它只会返回 false,是否存在有任何解决这个问题的方法吗?

注意:我正在使用词汇表来获取单词的定义,并使用随机词来获取将要定义的随机词。

脚本:

from gtts import gTTS
import subprocess
import time
import os
from random_word import RandomWords
from vocabulary.vocabulary import Vocabulary as vb
#Defining some variables and stuff
r = RandomWords()
#this gets the random word and checks if it has a dictionary definition, and makes the audio file
randword = r.get_random_word(hasDictionaryDef="true")
file = randword+' Definition.mp3'
#this part gets the meaning and converts the meaning into a string so it can say the text
todefine = vb.meaning(randword)
texty = str(todefine)
#What makes the text, then plays it
def PlayText():
        #this part makes the file
        language = 'en'
        myobj = gTTS(text=texty, lang=language, slow=False) 
        myobj.save(file)
        time.sleep(1)
        #these prints are for debugging, randword is the random word, todefine is the definition
        print(randword)
        #texty is probably false, unless this has been fixed, and actaully define sthe word.
        print(texty)
        subprocess.call(file, shell=True)

#this just calls the function where everything is happening
PlayText()

感谢您阅读本文,如果您想提供帮助,非常感谢!

标签: pythonpython-3.xvocabulary

解决方案


好吧,事实证明,词汇表实际上被破坏了,因为它依赖于从中获得定义切割支持的 API,所以解决方案是实际使用其他东西,比如 pydictionary,感谢任何试图提供帮助的人,祝你有美好的一天,或晚上,取决于你在哪里。这是我获得信息的地方的链接,感谢 Adriaan 给我这个,https://github.com/tasdikrahman/vocabulary/issues/70

这是固定代码:

#The things i import
from gtts import gTTS
import subprocess
import time
import os
from random_word import RandomWords
from PyDictionary import PyDictionary
#Defining some variables and stuff
r = RandomWords()
#this gets the random word and checks if it has a dictionary definition, and makes the audio file
dictionary=PyDictionary()
randword = r.get_random_word(hasDictionaryDef="true")
file = randword+' Definition.mp3'
#this part gets the meaning and converts the meaning into a string so it can say the text
todefine = dictionary.meaning(randword)
texty = str(todefine)
#What makes the text, then plays it
def PlayText():
        #this part makes the file
        language = 'en'
        myobj = gTTS(text=texty, lang=language, slow=False) 
        myobj.save(file)
        time.sleep(1)
        #these prints are for debugging, randword is the random word, todefine is the definition
        print(randword)
        #texty is probably false, unless this has been fixed, and actaully define sthe word.
        print(texty)
        subprocess.call(file, shell=True)

#this just calls the function where everything is happening
PlayText()

推荐阅读