used_words = set() 的作用是什么呢?
来源:15-2 es完成搜索建议-搜索建议字段保存 - 2
笑看风云号
2017-06-29
def gen_suggests(index, info_tuple): #根据字符串生成搜索建议数组 # 用 set 来去重 # 因为不同字段分词后可能有部分关键词重复,但是不同字段的权重又不同,所以这里用 set 保存,避免了覆盖。 used_words = set() suggests = [] for text, weight in info_tuple: if text: #调用es的analyze接口分析字符串 words = es.indices.analyze(index=index, analyzer="ik_max_word", params={'filter':["lowercase"]}, body=text) anylyzed_words = set([r["token"] for r in words["tokens"] if len(r["token"])>1]) new_words = anylyzed_words - used_words else: new_words = set() if new_words: suggests.append({"input":list(new_words), "weight":weight}) return suggests
代码中的 used_words = set() 一直是一个空数组吗?在代码中,并没有对这个数组 used_words 进行赋值操作呀。
写回答
1回答
-
def gen_suggests(index, info_tuple): used_words = set() suggests = [] for key, weight in info_tuple: if key: result = es.indices.analyze(index=index, analyzer='ik_max_word', params={'filter': ['lowercase']}, body=key) words = set([r['token'] for r in result['tokens'] if len(r['token']) > 1]) new_words = words - used_words used_words.update(words) else: new_words = set() if new_words: suggests.append({'input': list(new_words), 'weight': weight}) return suggests
亲代码应该是这样的
022017-07-27
相似问题