python - 有没有办法不让词汇模块的单词定义出错?
问题描述
我正在制作一个脚本,它接受一个随机单词并获取该单词的定义,然后它将其转换为语音并播放它,但由于某种原因,每当我尝试获取该单词的定义时,它只会返回 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()
感谢您阅读本文,如果您想提供帮助,非常感谢!
解决方案
好吧,事实证明,词汇表实际上被破坏了,因为它依赖于从中获得定义切割支持的 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()
推荐阅读
- javascript - 试图从一个字段中提取文本并将其放入按钮文本中
- python - blockSignals 不适用于 QGraphicsScene selectionChanged
- java - 在禁用的图像链接 (JSP) 上显示工具提示
- javascript - 未捕获的类型错误:无法读取带有 document.getElementById() 的 null 属性“addEventListener”
- javascript - Firebase UI:updateCurrentUser 失败:第一个参数“user”必须是 Firebase User 的实例或 null
- json - Parse (arbitrary) previously know data from JSON using GO
- lua - 如何挑选范围/区域中的每个玩家?
- flutter - Flutter 正在执行热重启而不是热重载 [Ubuntu]
- javascript - AWS Datastore 不查询应用程序首次打开的时间
- ruby-on-rails - Rails 模型 has_one 来自 has_many 关系