我怎么能改变没有并写出价值?

时间:2017-09-09 21:52:33

标签: python-3.x variables nltk

此代码获取句子的所有pos_tag并计算每个单词的分数

def sentence_score(sentence_tokens, previous_token, acum_score):    
  if not sentence_tokens:
     return acum_score
  else:

    current_token = sentence_tokens[0]
    tags = current_token[2]
    token_score = sum([value(tag) for tag in tags])
    if previous_token is not None:
        previous_tags = previous_token[2]
        #print('token1', previous_tags)
        if 'inc' in previous_tags:
            token_score += 1.0
        elif 'dec' in previous_tags:
            token_score -= 1.0
        elif 'inv' in previous_tags:
            token_score -= 2
  return sentence_score(sentence_tokens[1:], current_token, acum_score + token_score)

我想在txt中写下你的值来改变None

def sentiment_score(text):
 return sum([sentence_score(sentence, None,0.0) for sentence in text])

1 个答案:

答案 0 :(得分:0)

你忘了return token_score。当任何函数在没有执行显式return命令的情况下结束时,使用隐式return None

(适用更新)

def sentence_score(sentence_tokens, previous_token, acum_score):    
    if not sentence_tokens:
        return acum_score
    else:
        current_token = sentence_tokens[0]
        tags = current_token[2]
        token_score = sum([value(tag) for tag in tags])
        if previous_token is not None:
            previous_tags = previous_token[2]
            #print('token1', previous_tags)
            if 'inc' in previous_tags:
                token_score += 1.0
            elif 'dec' in previous_tags:
                token_score -= 1.0
            elif 'inv' in previous_tags:
                token_score -= 2
        return token_score