rassa45
rassa45

Reputation: 3560

Cannot download Wordnet Error

I am trying to compile this code:

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/>"

To all those who answered my previous question, thank you. It worked, I will be accepting an answer soon. However, I have another problem. When I try to import wordnet in a shell (by compiling and IDLE commands), the process works fine. However, on xampp, I get this error: enter image description here

Can someone please explain this as well? Thanks!

Upvotes: 3

Views: 228

Answers (2)

AMR
AMR

Reputation: 604

A couple of things. First is the indent of line one. That may just be copying here.

Then every time you have a colon, you need to have the next line indented. So in the otherPrint function you have this:

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

At least the first line needs to be indented. If you intend all of the prints to be in the loop then you need to indent all of them.

You also have the same issue with you if statements in formatPrint function. Try indenting them under the loops and conditionals and this should clear it up. If you are still finding a problem, then check to make sure you have the correct number of parentheses and brackets closing out statements. Leaving one off will cause the rest of the code to go wonky.

Also your are using print statements instead of the print() function. The print statement no longer works in Python 3.x... you have to enclose all of that in parentheses.

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>")

Upvotes: 1

Anand S Kumar
Anand S Kumar

Reputation: 90979

Your for loop is not indented in other loop -

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

This is most probably the issue. Try indenting it-

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

Also, please understand that python treats tabs and spaces differently, so assuming you indent one line using tab and then next line using 4 spaces (manual spaces) it would cause indentation error in Python. You have to either use all spaces or all tabs , you cannot use a mixture of both (even though they look the same).

Upvotes: 1

Related Questions