【发布时间】:2013-12-12 12:18:22
【问题描述】:
我在标准输入中获取一个文件,看起来像
12 125 "neg" Won the match #getin . P
然后对句子进行单词分析。
我不知道为什么,但是函数“unigrams_nrc”中的循环没有增加。
i value is still 0
代码如下:
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)
即使我将参数中的i 传递给函数.. 仍然没有变化。
【问题讨论】:
-
unigrams_nrc 是函数
-
不是
i是0,是sentence[i]。 -
怎么可能,我的意思是你甚至没有打开你的代码示例中的文件。
-
@GamesBrainiac 我还没有发布那部分代码。我认为这很明显
-
您目前也有缩进错误。例如,
for line in file:之后的代码没有缩进,所以我们不知道它在那个块中是什么,什么不是。
标签: python file for-loop file-io