0

I'm trying to test what I've written in analyze by running smile with a word and I'm getting this error msg and I don't understand it! It seems to indicate I need to provide the argument 'text' to tokenize in analyze, but 'text' is an argument to analyze (def analyze(self, text)) and in smile analyze is called with only one argument - argv[1] (so I called it with one arg in tweet too). I don't understand firstly why analyze can be called with only one argument when it has two (is it because 'self' is passed automatically somehow??!) and secondly why is tokenize asking for 'text' when smile already passes argv[1] to analyze (which is the arg that is supposed to correspond to 'text'?) I hope I have explained this clearly - I'm not posting code because the question is about the code that is provided already but I can post it if it would be helpful? Thank you!

0

This question:

...... why analyze can be called with only one argument when it has two (is it because 'self' is passed automatically somehow??!)

Is answered in the spec here:

Recall that methods are automatically passed that first reference to self so that they have a way of referring to objects' "instance variables."

analyze is an Analyze object as declared in smile here analyzer = Analyzer(positives, negatives)

This question:

why is tokenize asking for 'text' when smile already passes argv[1] to analyze (which is the arg that is supposed to correspond to 'text'?)

smile is passing argv[1] to a (local) analyze variable, which is called text. tokenize requires an argument (which happens to also be named text as per the error message); you need to call tokenize with analyze's text.

9
  • thank you! I'd guessed the spec you ref'd was the answer to that bit - I just can't get my head round the methods and instance vars bit but I see how it would work (I think) but I'm really sorry - I don't understand completely the other bit. I understand that smile has a local analyze variable but I've put tokenize inside def analyze - so if that's right why doesn't it just take the same 'text' arg that is passed to analyze? Do I have to pass it separately to tokenize, even tho tokenize is inside analyze? Sorry, I haven't grasped the structures v well yet in python... maybe I'm off track?!
    – CallyB
    Mar 4 '17 at 14:02
  • tokenize is a function that requires an argument, full stop. No different than strlen in C or load in pset5. It's called a "method" in python when it can only be accessed by an instance of a class object, but it's a function, and if it requires an argument, an argument must be included in the call ("passed to it"). This question isn't strictly about supplied code, because supplied code does not include a call to tokenize. Mar 4 '17 at 14:20
  • Thank you - I think I get it, that makes it a lot clearer - I've also just read a great article on oop in python that explains a lot - & you're right about the supplied code not including tokenize, I also think I've put tokenize in the wrong place (inside the Analyzer object) - it seems it should be in tweet instead where I'd have no choice but to pass it a separate var, but I'll have a try & see if that works better or not...thanks for clarifying stuff for me!
    – CallyB
    Mar 4 '17 at 14:24
  • I'm sorry, DinoCoderSaurus - I've spent 24 hrs trying to figure this out and I still can't see how to pass any other arg to tokenize than relying on the 'text' arg from the analyze method that tokenize is inside of. Here's my analyze code - pastebin.com/iZmZF0F5 - and I realised I did need the tokenize function in it (although I'm not entirely sure how the whole analyze method deals with argv[1] arg being passed to it....!) I don't know if I'm way off, but any more help would be much appreciated - I don't know if you'll see this but if not I'll repost it as a question if i can...
    – CallyB
    Mar 5 '17 at 19:34
  • I'd have to research it further to explain it better, but the error is because of this line tokenizer = nltk.tokenize.TweetTokenizer;. It should be tokenizer = nltk.tokenize.TweetTokenizer();, since TweetTokenizer is a method. Mar 5 '17 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .