1

我正在学习 coursera 数据科学课程,并首次涉足 python,虽然尊重空格让我发疯,但这还不错。

即使这里没有足够的信息来诊断我的问题,也许一个精明的 python 程序员可以让我看到我什至对谷歌还不够了解的问题。

我一直在尝试调试我的脚本一段时间无济于事。所有文件等都加载得很好,但是当我尝试处理一堆存储的推文并使用情感字典对它们进行评分时,我在使用 python 解释器时得到分数,但我的脚本全为零。不同之处在于,在脚本中,我将执行链放在 main() 中,但在 CLI 中,我正在逐行执行(尽管我确实设置了相关的 get_score 函数)。

我相信的是相关的线路:

import sys
import json
import re

parse_words = re.compile("\W")
data = []
dictionary = {}

...

def get_score(tweet_text):
    words = parse_words.split(tweet_text.lower())
    word_scores = map(lambda word: dictionary.get(word, 0), words)
    score = sum(word_scores)
    return score


def main():
    """set up the data files"""
    ...
    for i in range(len(data)):
        try: # because not each line contains a tweet
            tweet_text = data[i]["text"].encode('utf-8')
        except:
            tweet_text = ""
        print("%6.2f %s" % (get_score(tweet_text), tweet_text))
4

0 回答 0