栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

按给定短语返回匹配项列表

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

按给定短语返回匹配项列表

我将按以下方式进行处理:

import itertoolsdef new_phrases(phrase, syns):    """Generate new phrases from a base phrase and synonyms."""    words = [syns.get(word, [word]) for word in phrase.split(' ')]    for t in itertools.product(*words):        yield ' '.join(t)def get_matches(phrase, syns, phrases):    """Generate acceptable new phrases based on a whitelist."""    phrases = set(phrases)    for new_phrase in new_phrases(phrase, syns):        if new_phrase in phrases: yield new_phrase

代码的根本是

words
in中的分配
new_phrases
,它将
phrase
syns
转换为更可用的形式,一个列表,其中每个元素都是该单词可接受的选择的列表:

>>> [syns.get(word, [word]) for word in phrase.split(' ')][['This'], ['is'], ['a'], ['small', 'tiny', 'little'], ['cottage', 'house']]

请注意以下几点:

  • 使用生成器更有效地处理大量组合(而不是一次构建整个列表);
  • 使用a
    set
    进行有效的(
    O(1)
    ,而
    O(n)
    不是列表)成员资格测试;
  • 使用
    itertools.product
    生成
    phrase
    基于的可能组合
    syns
    (您也可以
    itertools.ifilter
    在实现中使用);和
  • 符合风格指南。

正在使用:

>>> list(get_matches(phrase, syns, phrases))['This is a small cottage', 'This is a tiny house']

要考虑的事情:

  • 字符的情况如何(例如应如何
    "House of Commons"
    对待)?
  • 标点符号呢?


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/451843.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号