2015-07-10 73 views
3

我試圖編譯這段代碼:無法下載WORDNET錯誤

from collections import OrderedDict 
import pdb 
pdb.set_trace() 
def alphaDict(words): 
    alphas = OrderedDict() 
    words = sorted(words, key = str.lower) 
    words = filter(None, words); 
    for word in words: 
     if word[0].upper() not in alphas: 
      alphas[word[0].upper()] = [] 
      alphas[word[0].upper()].append(word.lower()) 
    return alphas 

def listConvert(passage): 
    alphabets = " abcdefghijklmnopqrstuvwxyz" 
    for char in passage: 
     if char.lower() not in alphabets: 
      passage = passage.replace(char, "") 
      listConvert(passage) 
    passage = rDup(passage.split(" ")) 
    return passage 




def rDup(sequence): 
    unique = [] 
    [unique.append(item) for item in sequence if item not in unique] 
    return unique 



def otherPrint(word): 
    base = "http://dictionary.reference.com/browse/" 
    end = "?s=t" 
    from nltk.corpus import wordnet as wn 
    data = [s.definition() for s in wn.synsets(word)] 
    print("<li>") 
    print("<a href = '" +base+word+end+"' target = '_blank'><h2 class = 'dictlink'>" +(word.lower())+":</h2></a>") 
    if not data: 
     print("Sorry, we could not find this word in our data banks. Please click the word to check <a target = '_blank' class = 'dictlink' href = 'http://www.dictionary.com'>Dictionary.com</a>") 
     return 
    print("<ul>") 
    for key in data: 
    print("<li>"+key+"</li>") 
    print("</ul>") 
    print("</ol>") 
    print("</li>") 




def formatPrint(word): 
    base = "http://dictionary.reference.com/browse/" 
    end = "?s=t" 

    from PyDictionary import PyDictionary 
    pd = PyDictionary() 
    data = pd.meaning(word) 

    print "<li>" 
    print "<a href = '" +base+word+end+"' target = '_blank'><h2 class = 'dictlink'>" +(word.lower())+":</h2></a>" 

    if not data: 
    print "Sorry, we could not find this word in our data banks. Please click the word to check <a target = '_blank' class = 'dictlink' href = 'http://www.dictionary.com'>Dictionary.com</a>" 
    return 
    print "<ol type = 'A'>" 
    for key in data: 
    print "<li><h3 style = 'color: red;' id = '" +word.lower()+ "'>"+key+"</h3><ul type = 'square'>" 
    for item in data[key]: 
      print "<li>" +item+"</li>" 
    print "</ul>" 
    print "</li>" 
    print "</ol>" 
    print "</li>" 




def specPrint(words): 
    print "<ol>" 
    for word in words: 
     otherPrint(word) 
    print "</ol>" 
    print "<br/>" 
    print "<br/>" 
    print "<a href = '#list'> Click here</a> to go back to choose another letter<br/>" 
    print "<a href = '#sentence'>Click here</a> to view your sentence.<br/>" 
    print "<a href = '#header'>Click here</a> to see the owner's information.<br/>" 
    print "<a href = '../html/main.html'>Click here</a> to go back to the main page." 
    print "</div>" 
    for x in range(0, 10): 
     print "<br/>" 

所有那些誰回答我剛纔的問題,謝謝。它的工作,我很快就會接受答案。但是,我有另一個問題。當我嘗試在shell中導入wordnet時(通過編譯和IDLE命令),該過程正常工作。然而,在xampp上,我得到這個錯誤: enter image description here

有人可以請解釋一下嗎?謝謝!

+0

一切工作的時候我沒有足夠的otherPrint()方法,這是關於Python 2.7 – ytpillai

回答

1

您的循環沒有縮進其他環路 -

for key in data: 
print("<li>"+key+"</li>") 
print("</ul>") 
print("</ol>") 
print("</li>") 

這是最有可能的問題。嘗試縮進IT-

​​

另外,請大家明白,蟒蛇治療tabsspaces不同,所以假設你使用tab縮進一個線,然後用4 spaces(手動空格)會引起縮進錯誤在Python下一行。你必須使用所有的空格或所有的標籤,你不能混用兩者(即使它們看起來一樣)。

+0

OK謝謝,我覺得我沒做幾次。這有助於一個巨大的噸。這是否是因爲python將這個標籤指向了escape,而4個空格是指4個空格? – ytpillai

+0

是的,我相信是這樣的,python的內部標籤看起來像''\ t'',而4個空格看起來像''''',所以它們不等於根據python縮進。 –

+0

@ytpillai請參閱:http://stackoverflow.com/questions/23415525/why-does-python-see-a-tab-as-8-spaces和https://www.python.org/dev/peps/pep -0008 /#tabs-or-spaces – metatoaster

1

一些事情。首先是第一行的縮進。這可能只是在這裏複製。

然後每次有冒號時,都需要將下一行縮進。所以在其他打印功能中,您可以這樣做:

for key in data: 
print("<li>"+key+"</li>") 
print("</ul>") 
print("</ol>") 
print("</li>") 

至少第一行需要縮進。如果你打算所有的打印在循環中,那麼你需要縮進所有的打印。

如果語句在formatPrint函數中,您也有同樣的問題。嘗試在循環和條件下縮進它們,並且這應該清除它。如果您仍然在發現問題,請檢查並確保您有正確數量的括號和括號來關閉語句。將其中一個關閉會導致其餘代碼變得不可靠。

此外,您正在使用print語句,而不是print()函數。 print聲明不再適用於Python 3.x ...您必須將所有這些都括在括號中。

def formatPrint(word): 
    base = "http://dictionary.reference.com/browse/" 
    end = "?s=t" 

    from PyDictionary import PyDictionary 
    pd = PyDictionary() 
    data = pd.meaning(word) 

    print("<li>") 
    print(
      "<a href = '" +base+word+end+"' target = '_blank'> 
      <h2 class = 'dictlink'>" +(word.lower())+":</h2></a>" 
     ) 

    if not data: 
     print(
       "Sorry, we could not find this word in our data banks. 
       Please click the word to check <a target = '_blank' 
       class = 'dictlink' href 
       ='http://www.dictionary.com'>Dictionary.com</a>" 
       ) 
    return 
    print("<ol type = 'A'>") 
    for key in data: 
     print(
       "<li><h3 style = 'color: red;' id = '" +word.lower()+ 
       "'>"+key+"</h3><ul type = 'square'>" 
      ) 
    for item in data[key]: 
     print("<li>" +item+"</li>") 
     print("</ul>") 
     print("</li>") 
     print("</ol>") 
     print("</li>")