3

Apologies in advance for what I know will be a simple problem. I'm a total beginner to Python, but have decided to use it to write a mapreduce doing sentiment analysis.

I have taken a python file from this link: http://www.alex-hanna.com/tworkshops/lesson-6-basic-sentiment-analysis/ to give me guidance and am trying to run it. The code for the particular thing I have problems with is:

…
if len(sys.argv) < 2:
   print "Usage: avgNReduce.py "
   sys.exit(0)
…

The error is:

   if len(sys.argv) < 2:
                       ^
   SyntaxError: invalid syntax

I'm assuming this is a basic problem to resolve, but despite googling it I don't really know how I'm meant to fix this. I've tried using a colon instead of a semi colon and have ensured the ampersand was correct from the copying over. Any ideas?

msw
  • 42,753
  • 9
  • 87
  • 112
Andrew Martin
  • 5,619
  • 10
  • 54
  • 92

1 Answers1

9

You're looking for the < operator there instead of lt. (lt stands for the less than operator in HTML, see this thread.)

if len(sys.argv) < 2:
    print "Usage: avgNReduce.py "
    sys.exit(0)
Community
  • 1
  • 1
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
  • Thanks. I did say I was a total beginner! – Andrew Martin Aug 07 '13 at 14:19
  • Glad to help. Happy Learning! And, feel free to accept the answer when the system allows you to. :) – Sukrit Kalra Aug 07 '13 at 14:20
  • It's literally `<` on the page he [copied it from](http://www.alex-hanna.com/tworkshops/lesson-6-basic-sentiment-analysis/). I filed a "bug" with the author. – msw Aug 07 '13 at 14:37
  • @msw : Yeah. The original author must have made some mistake. :P – Sukrit Kalra Aug 07 '13 at 14:38
  • Can I ask a quick follow up question then. From some basic reading I see that in the code I posted a dictionary, sentimentDict is created, with two keys that aren't mapped to any values. Later, two text files are opened and each line is stripped and mapped to these dictionaries. But what is the = 1 part for? Why is that necessary? – Andrew Martin Aug 07 '13 at 14:52
  • @AndrewMartin : I've not read the whole code, but maybe the author is trying to keep a dictionary with `positive` and `negative` sentiments both as empty dictionaries at first and mapping the lines in the text file to their occurrences (for counting the ratio). This assumes that each keyword occurs only once, since the author simply replaces the value if another occurrence is found (which may not be the case, since the files would already have only single keywords). – Sukrit Kalra Aug 07 '13 at 14:56
  • @SukritKalra That sounds very logical. Thanks! – Andrew Martin Aug 07 '13 at 14:59
  • @AndrewMartin : Seeing the pseudo-code, I really don't find it as necessary, the author could have simply made a list of `positive` and `negative` words, though the looking up would be O(n), whereas it is O(1) in the dictionary. – Sukrit Kalra Aug 07 '13 at 14:59
  • @AndrewMartin I suggest you open a new question and tag it `nltk`as you'll it warrants its own question *and* you'll get more NLTK users looking at it. – msw Aug 07 '13 at 15:00
  • @msw Thanks, just did that – Andrew Martin Aug 07 '13 at 15:10