【问题标题】:Cannot download Wordnet Error无法下载 Wordnet 错误
【发布时间】:2015-07-10 02:09:02
【问题描述】:

我正在尝试编译这段代码:

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 上,我收到此错误:

有人也可以解释一下吗?谢谢!

【问题讨论】:

  • 当我没有 otherPrint() 方法时一切正常,这是在 Python 2.7 上

标签: python python-2.7 python-3.x


【解决方案1】:

你的 for 循环没有在其他循环中缩进 -

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

这很可能是问题所在。试试缩进 -

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

另外,请理解 python 处理 tabs 和 spaces 的方式不同,因此假设您使用 tab 缩进一行,然后使用 4 spaces(手动空格)缩进下一行,这将导致 Python 中的缩进错误。您必须使用所有空格或所有制表符,不能同时使用两者(即使它们看起来相同)。

【讨论】:

  • 好的,谢谢,我想我确实做过几次。这有很大帮助。这是因为 python 将制表符称为转义,4 个空格称为 4 个空格?
  • 是的,我相信是这样,python 的内部选项卡看起来像 - '\t',而 - 4 个空格看起来像 - ' ',所以根据 python,它们不等于缩进。
  • 我只是使用了全选和取消标签区域。这应该解决所有问题还是可能存在隐藏错误?
  • 不确定 untabify 区域是什么,但缩进在 python 中非常重要,它用于识别 python 中的块。但是,如果现在缩进是正确的并且您始终使用相同的缩进,那么您的缩进错误应该会消失
【解决方案2】:

有几件事。首先是第一行的缩进。那可能只是在这里复制。

然后,每次有冒号时,都需要将下一行缩进。所以在 otherPrint 函数中你有这个:

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

至少第一行需要缩进。如果您希望所有打印都在循环中,那么您需要将所有打印都缩进。

formatPrint 函数中的 if 语句也有同样的问题。尝试在循环和条件下缩进它们,这应该可以清除它。如果您仍然发现问题,请检查以确保您有正确数量的括号和括号来关闭语句。留下一个会导致其余代码变得不稳定。

您还使用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>")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    相关资源
    最近更新 更多