3

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!

TuringTux
  • 559
  • 1
  • 12
  • 26
rassa45
  • 3,482
  • 1
  • 29
  • 43

2 Answers2

1

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

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • OK thanks I think I did do that a few times. That helps a huge ton. Is this because python refers to the tab as escaped and 4 spaces as, well, 4 spaces? – rassa45 Jul 10 '15 at 02:16
  • yes , I believe so , internally tab for python looks like - `'\t'` , whereas - 4 spaces would look like - `' '` , so they are not equal indentation according to python. – Anand S Kumar Jul 10 '15 at 02:17
  • @ytpillai see: http://stackoverflow.com/questions/23415525/why-does-python-see-a-tab-as-8-spaces and https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces – metatoaster Jul 10 '15 at 02:18
  • I just used select all and untabify region. Should this fix everything or could there be hidden errors? – rassa45 Jul 10 '15 at 02:20
  • Not sure what untabify region is, but indentation is very important in python, it is used to identify blocks in python. But if the indentation is now correct and you are using same indentation throughout, your indentation error should go away – Anand S Kumar Jul 10 '15 at 02:21
  • My errors did on Shell, but not on the server as I edited in my question – rassa45 Jul 10 '15 at 02:28
  • I think you can ask that as another question in SO, I am not that experienced with nltk :) – Anand S Kumar Jul 10 '15 at 02:30
  • OK Thanks! Can I just keep this question as it is instead? I don't want to have to copy everything – rassa45 Jul 10 '15 at 02:30
  • yea sure, you can. But you should copy the important parts (like the one you added in your edit) to the new question – Anand S Kumar Jul 10 '15 at 02:34
1

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>")
AMR
  • 584
  • 1
  • 6
  • 16