如何在数组中循环、打印和保存输出

2024-09-28 01:32:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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、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")

如果有人能提供一些见解,我将不胜感激。谢谢您!你知道吗


Tags: textposymltagsentencesdictdictssplitter

热门问题