多次返回方法和元组赋值

时间:2017-07-27 11:59:24

标签: python methods tuples multiple-return-values

长话短说:

此方法位于另一个文件中的类Analyzer中:

def analyze(self, text):
    # Access tools of nltk
    tokenizer = nltk.tokenize.TweetTokenizer()
    # Defining couning score variable
    score_positive = 0
    score_negative = 0
    score_nuteral = 0
    # since the text is a list we itterate of the list element by element
    for tweet in text:
        tokens = tokenizer.tokenize(tweet)
        # Checking each token
        for token in tokens:
            if token.lower() in self.positives:
                score_positive += 1
            elif token.lower() in self.negatives:
                score_negative += 1
            else:
                score_nuteral += 1
        return score_positive, score_negative, score_nuteral

函数调用方法

def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)

    # pass to analyze
    tweets = Analyzer("positive-words.txt", "negative-words.txt")


    positive, negative, neutral = tweets.analyze()

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name) 

问题是如何通过元组分配多个回报:

positive, negative, neutral = tweets.analyze()

0 个答案:

没有答案