1

I taking a file at standard input which looks like

12    125      "neg"       Won the match #getin . P

and then doing word analysis of the sentence.

I dont know why but the loop is not getting incremented in the function "unigrams_nrc". i value is still 0

Here is the code:

def unigrams_nrc(file):
      for line in file:
      (term,score,numPos,numNeg) = re.split("\t", line.strip())
      print sentence[i] #=> prints all 0's i does not increment
      if re.match(sentence[i],term.lower()):
         wordanalysis["unigram"] = found
      else:
         found = False
      if found:
         wordanalysis["trail_unigram"] = found if re.match(sentence[(len(sentence)-1)],term.lower()) else not(found)
         wordanalysis["lead_unigram"] = found  if re.match(sentence[0],term.lower()) else not(found)
         wordanalysis["nonzero_sscore"] = float(score) if (float(score) != 0) else 0             
         wordanalysis["sscore>0"] = (float(score) > 0)
         wordanalysis["sscore"] = (float(score) != 0)

      if re.match(tweet[len(sentence)-1],term.lower()):
         wordanalysis["sscore !=0 last token"] = (float(score) != 0)


for line in sys.stdin:
    #12    125    "neg"       Won the match #getin . P
   (tweetid,num,senti,tweets) = re.split("\t+",line.strip())
   sentence = re.split("\s+", tweets.strip())
   for i in range(0,len(sentence)):
      unigrams_nrc(file)

Even if I pass i in parameter to the function.. still no change.

11
  • unigrams_nrc is the function Commented Dec 12, 2013 at 12:20
  • It's not i that's 0, it's sentence[i]. Commented Dec 12, 2013 at 12:24
  • How can it, I mean you haven't even opened the file in your code sample. Commented Dec 12, 2013 at 12:25
  • @GamesBrainiac i havent posted that part of the code. I thought it was obvious Commented Dec 12, 2013 at 12:26
  • 1
    You also currently have indentation errors. For instance, the code after for line in file: is not indented, so we don't know what it is in that block and what isn't. Commented Dec 13, 2013 at 6:57

2 Answers 2

2

The indentation is not correct, but assuming it supposed to be like in your other question, print sentence[i] is inside the for line in file: loop. Since i is not incremented inside the loop, you see the same thing printed many times.

When you call unigrams_nrc the second time, there are no more lines to read in file, so the loop executes zero times. To read the file again you need to close and open it, or seek to the beginning. Though, reading the data into eg. a dictionary, or even just a list, would speed up your program.

A quick fix would be to add file = file.readlines() after opening file; the rest of your code should work as is then.

Sign up to request clarification or add additional context in comments.

2 Comments

Only prints Won 'n' number of times
yes I know i is not incremented but why is that happening. how can i solve this issue.
0

You want to do something like this

def foo1():
    print something[i]

def foo0():
    for i in range(0,number):
        foo1(somethingElse)

This is not possible!
How should foo1 know the value of i and sentence? You have to pass the values if you want to use them.

def foo1(something,i):
    print something[i]

def foo0():
    for i in range(0,number):
        foo1(something,i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.