【发布时间】:2017-06-18 03:12:23
【问题描述】:
我是 Python 的新用户,正在尝试循环以下内容:
text = open('filename.txt', 'rU').read()
splitter = Splitter()
postagger = POSTagger()
splitted_sentences = splitter.split(text)
pos_tagged_sentences = postagger.pos_tag(splitted_sentences)
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml'])
dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
scoreposneg = sentiment_score(dict_tagged_sentences)
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml', 'dicts/inc.yml', 'dicts/dec.yml', 'dicts/inv.yml'])
dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
scoretotal = sentiment_total(dict_tagged_sentences)
print scoretotal
在此之前,我已经为 Splitter、POSTagger、DictionaryTagger、sentiment_total 设置了类,还创建了 5 个不同的字典。当我在 Python 上为 1 个文件运行它时,这很有效(我从 print scoretotal 中得到了一个数字)。但是,当我尝试创建一个循环并打印所有输出(我的目录中有 650 个文件)时,它不起作用(没有打印任何内容)并且 scoretext.txt 文件中有 0.0。
path = '/mydirectory'
files = glob.glob(path)
for file in files:
text = open(file, 'rU').read()
splitter = Splitter()
postagger = POSTagger()
splitted_sentences = splitter.split(text)
pos_tagged_sentences = postagger.pos_tag(splitted_sentences)
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml'])
dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
scoreposneg = sentiment_score(dict_tagged_sentences)
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml', 'dicts/inc.yml', 'dicts/dec.yml', 'dicts/inv.yml'])
dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
scoretotal = sentiment_total(dict_tagged_sentences)
print scoretotal
scoretotal = np.zeros((1,650))
scoretotal_no = 0
scoretotal_no = scoretotal_no + 1
np.savetxt("scoretext.txt", scoretotal, delimiter=" ", fmt="%s")
如果有人能就此提供一些见解,我将不胜感激。谢谢!
【问题讨论】:
-
我不确定我理解这应该做什么(
scoretotal在循环外被重置?)但是在for file in files:之前从print(files)得到什么?另外,file是 Python 2.x 中的内置函数,所以我会选择其他名称。 -
嗨 Roganjosh,我得到的是 [] 。我已将文件更改为文件名。我以为我正在将 650 分数保存到一个数组中,所以它必须脱离循环
-
好的,首先循环本身不起作用,因为
path = '/mydirectory'在files = glob.glob(path)中为您提供了一个空列表(没有什么可以迭代的)。这孤立了一个问题。但是scoretotal = np.zeros((1,650))完全破坏了您在scoretotal的循环期间可能积累的任何数据。事实上,每个循环只会破坏上一次迭代的数据(我假设,但我不知道sentiment_total()做了什么)。 -
我已经定义了sentiment_total (def sentence_total(review): return sum([sentence_score(sentence, None, 0.0) for sentence in review])) 返回文本的分数。你会推荐使用 i 和 range 来循环吗?
-
实际上不可能知道您应该做什么,但是您的循环无法工作,因为
files = glob.glob(path)返回一个空列表。你的第一个问题是之前你的for循环,所以在你进入循环之前需要修复。
标签: python arrays loops numpy glob