【问题标题】:How to find all word combinations from a string and a list of synonyms如何从字符串和同义词列表中查找所有单词组合
【发布时间】:2020-06-05 21:09:23
【问题描述】:

我有一个单词列表,其中一些单词有同义词

words = ['cat','under','table']

feline;cat

below;down;under;beneath

table;bench;board

我需要找到这些同义词的不同列表组合。

该示例将返回:

['cat','under','table'],['feline','under','table'],['cat','below','table'],['feline','below','table'] ...

我想知道是否有解决这个问题的算法。或者我该如何处理?我尝试使用嵌套循环方法,但我遇到了问题,因为单词列表和同义词列表的长度是可变的

【问题讨论】:

标签: python nlp


【解决方案1】:

您可以使用 Python 的itertools.product 解决该问题。这是我解决它的方法。

from itertools import product

def solution(words, syn):
    new_words_list = []
    for w in words:
        if w in syn.keys():
            new_words_list.append(syn[w] + [w])
        else:
            new_words_list.append([w])
    answer = list(product(*new_words_list))
    return answer

鉴于您的 words 以字符串列表的形式给出,syn 以字典的形式给出,其中键是 words 中的单词,值是同义词列表,请采用 @987654326 @ 和 syn 作为输入,并为每个单词生成一个名为 new_words_list 的嵌套列表。

[['feline', 'cat'], ['below', 'down', 'beneath', 'under'], ['bench', 'board', 'table']]

由于wordssyn 的长度是变量,因此使用列表推导运算的* 将嵌套列表new_words_list 传递给itertools.product()itertools.product() 计算给定迭代的笛卡尔积。

这段代码 sn-p 的输出如下。

['feline', 'below', 'bench']
['feline', 'below', 'board']
['feline', 'below', 'table']
['feline', 'down', 'bench']
['feline', 'down', 'board']
['feline', 'down', 'table']
['feline', 'beneath', 'bench']
['feline', 'beneath', 'board']
['feline', 'beneath', 'table']
['feline', 'under', 'bench']
['feline', 'under', 'board']
['feline', 'under', 'table']
['cat', 'below', 'bench']
['cat', 'below', 'board']
['cat', 'below', 'table']
['cat', 'down', 'bench']
['cat', 'down', 'board']
['cat', 'down', 'table']
['cat', 'beneath', 'bench']
['cat', 'beneath', 'board']
['cat', 'beneath', 'table']
['cat', 'under', 'bench']
['cat', 'under', 'board']
['cat', 'under', 'table']

【讨论】:

    【解决方案2】:

    使用内置库 (itertools.product)

    from itertools import product
    
    for a, b, c in product(
        ["feline", "cat"],
        ["below", "down", "under", "beneth"],
        ["table", "bench", "board"],
    ):
        print(a, b, c) # feline below table (and so on)
    

    Python 实现

    itertools.product是用C实现的,但是等价于下面的。

    def my_product(*args):
    
        result = [[]]
    
        for _list in args:
            result = [x + [y] for x in result for y in _list]
    
        for item in result:
            yield item
    
    
    for a, b, c in my_product(
        ["feline", "cat"],
        ["below", "down", "under", "beneth"],
        ["table", "bench", "board"],
    ):
        print(a, b, c)
    

    输出

    两者产生相同的输出

    feline below table
    feline below bench
    feline below board
    feline down table
    feline down bench
    feline down board
    feline under table
    feline under bench
    feline under board
    feline beneth table
    feline beneth bench
    feline beneth board
    cat below table
    cat below bench
    cat below board
    cat down table
    cat down bench
    cat down board
    cat under table
    cat under bench
    cat under board
    cat beneth table
    cat beneth bench
    cat beneth board
    

    【讨论】:

      猜你喜欢
      • 2017-12-15
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      相关资源
      最近更新 更多