代码之家  ›  专栏  ›  技术社区  ›  Alonso Garcia

我如何更改none并写入值?

  •  0
  • Alonso Garcia  · 技术社区  · 7 年前

    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中写入您的值无

    def sentiment_score(text):
     return sum([sentence_score(sentence, None,0.0) for sentence in text])
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   pepr    7 年前

    你忘了 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