Есть ли способ не ошибиться при определении слова с помощью словарного модуля? - PullRequest
0 голосов
/ 02 мая 2020

Я делаю сценарий, который берет случайное слово и получает определение слова, затем он преобразует его в речь и воспроизводит его, но по какой-то причине, когда я пытаюсь получить определение слова, он просто возвращает 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()

Спасибо, что прочитали это, и если вы пытались помочь, супер спасибо!

1 Ответ

0 голосов
/ 02 мая 2020

Что ж, получается, что словарь на самом деле нарушен, поскольку API, на который он опирается, чтобы получить от него поддержку определения сокращений, поэтому решение состоит в том, чтобы фактически использовать что-то еще, например, pydictionary, благодаря любому, кто пытался помочь, и имеет хороший день или ночь, в зависимости от того, где вы находитесь. Вот ссылка на место, где я получил информацию, спасибо Адриану за предоставленную мне информацию, 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()
...