2017-02-20 49 views
0

我在macOS Sierra上運行Python 3,需要創建由特定單詞的同義詞組成的句子。爲此,我使用PyDictionary。然而,當運行我的代碼(下面給出)時,我得到一個錯誤(Python解釋器)和一個警告(BeautifulSoup)。PyDictionary/BeautifulSoup問題

輸出:

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/beautifulsoup4-4.5.3-py3.5.egg/bs4/__init__.py:181: UserWarning: No parser was e 
xplicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on an 
other system, or in a different virtual environment, it may use a different parser and behave differently. 

The code that caused this warning is on line 53 of the file main.py. To get rid of this warning, change code that looks like this: 

BeautifulSoup([your markup]) 

to this: 

BeautifulSoup([your markup], "html.parser") 

    markup_type=markup_type)) 
Traceback (most recent call last): 
    File "main.py", line 53, in <module> 
    edison() 
    File "main.py", line 29, in edison 
    say(respond(["I", "am", "happy", "to", "hear", "that", "html.parser"]) + "!") 
    File "/path/to/code/respond.py", line 9, in respond 
    output = (output + " " + (random.choice(dictionary.synonym(word, "html.parser")))) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/random.py", line 265, in choice 
    return seq[i] 
KeyError: 0 

main.py:

from respond import * 


def edison(): 
    mood = input("Hi, " + username + "! How are you today? ") 
    if mood.lower() in definitions.positive: 
     print(respond(["I", "am", "happy", "to", "hear", "that", "html.parser"]) + "!") #This is line 29 
    elif mood.lower() in definitions.negative: 
     print(respond(["I", "am", "sorry", "to", "hear", "that", "html.parser"]) + "!") 



edison() #This is line 53 

respond.py:

import random 
from PyDictionary import PyDictionary 
dictionary = PyDictionary() 

def respond(wordList): 
    output = "" 
    for word in wordList: 
     output = (output + " " + (random.choice(dictionary.synonym(word, "html.parser")))) 
    return output 
+0

爲什麼地球上會有錯誤的美麗湯?你沒有包括什麼嗎? – Elodin

+0

我沒有使用BeautifulSoup自己 - 但是根據Python包索引PyDictionary使用它。 https://pypi.python.org/pypi/PyDictionary –

回答

0

我發現你的準確同樣的問題,但它解決了在https://github.com/VitaliyRodnenko/geeknote/issues/305

也許嘗試去那,我相信你會找到你的答案。我不能直接幫助你,因爲我沒有做PyDictionary,但我希望你找到你的答案。 我希望它有幫助。

+0

回過頭來看,我沒有直接在我的代碼中引用BeautifulSoup,並且我不想更改PyDictionary的源代碼。你提到的另一個答案是? –

+0

沒有抱歉。這是我能找到的唯一的東西。 – Elodin