Как перебрать некоторые символы в строке, а другие символы остаются в том же положении в Python - PullRequest
1 голос
/ 18 июня 2019

У меня есть текст, скажем, например,

t = "The climate is super awesome"

делая,

from nltk.tokenize import word_tokenize words = word_tokenize(t)

что я получил,

>>>words = ["The","climate","is","super","awesome"]

и у меня есть несколько списков в словаре, каждый из которых имеет список синонимов. например,

dict = {'climate' : [weather,region,zone], 'super' : [excellent, superior, outstanding], 'awesome' : [amazing,great,stunning]}

Как написать код для получения комбинаций перестановок синонимов в предложении. Предположим, что у нас есть по крайней мере или точно 3 идентифицированных синонима для каждого из наших слов. Тогда всего в выбранной первой строке 't' есть 3 слова. Следовательно, от 3 до 3 предложений = 27 предложений можно сгенерировать.

а как вывод я хочу?

The weather is excellent amazing
The weather is excellent great
The weather is excellent stunning
The weather is superior amazing
The weather is superior great
The weather is superior stunning
The weather is outstanding amazing
The weather is outstanding great
The weather is outstanding stunning
The region is excellent amazing
The region is excellent great
The region is excellent stunning
The region is superior amazing
The region is superior great
The region is superior stunning
The region is outstanding amazing
The region is outstanding great
The region is outstanding stunning
The zone is excellent amazing
The zone is excellent great
The zone is excellent stunning
The zone is superior amazing
The zone is superior great
The zone is superior stunning
The zone is outstanding amazing
The zone is outstanding great
The zone is outstanding stunning

Любая помощь по этому поводу, будет действительно ощутима.

Ответы [ 4 ]

1 голос
/ 18 июня 2019

Использование itertools.product и str.replace:

words = ["The","climate","is","super","awesome"]
synonyms = {'climate' : ['weather','region','zone'],
            'super' : ['excellent', 'superior', 'outstanding'],
            'awesome' : ['amazing','great','stunning']}

from itertools import product

s = ' '.join(words)

for val in product(*[[(k, i) for i in v] for k, v in synonyms.items()]):
    new_s = s
    for (orig, new_one) in val:
        new_s = new_s.replace(orig, new_one)
    print(new_s)

Печать:

The weather is excellent amazing
The weather is excellent great
The weather is excellent stunning
The weather is superior amazing
The weather is superior great
The weather is superior stunning
The weather is outstanding amazing
The weather is outstanding great
The weather is outstanding stunning
The region is excellent amazing
The region is excellent great
The region is excellent stunning
The region is superior amazing
The region is superior great
The region is superior stunning
The region is outstanding amazing
The region is outstanding great
The region is outstanding stunning
The zone is excellent amazing
The zone is excellent great
The zone is excellent stunning
The zone is superior amazing
The zone is superior great
The zone is superior stunning
The zone is outstanding amazing
The zone is outstanding great
The zone is outstanding stunning
1 голос
/ 18 июня 2019

Я попробовал это, и, кажется, работает:

import re
words = ["The","climate","is","super","awesome"]
synons = {'climate' : ['weather','region','zone'], 'super' : ['excellent', 'superior', 'outstanding'], 'awesome' : ['amazing','great','stunning']}

phrases = {' '.join(words)}
for word, synonyms in synons.items():
    for syn in synonyms:
        for p in list(phrases):
            phrases.add(re.sub(r"\b%s\b" % word, syn, p))

print('\n'.join(sorted(phrases)))

Выход:

The climate is excellent amazing
The climate is excellent awesome
The climate is excellent great
The climate is excellent stunning
The climate is outstanding amazing
The climate is outstanding awesome
The climate is outstanding great
The climate is outstanding stunning
The climate is super amazing
The climate is super awesome
The climate is super great
The climate is super stunning
The climate is superior amazing
The climate is superior awesome
The climate is superior great
The climate is superior stunning
The region is excellent amazing
The region is excellent awesome
The region is excellent great
The region is excellent stunning
The region is outstanding amazing
The region is outstanding awesome
The region is outstanding great
The region is outstanding stunning
The region is super amazing
The region is super awesome
The region is super great
The region is super stunning
The region is superior amazing
The region is superior awesome
The region is superior great
The region is superior stunning
The weather is excellent amazing
The weather is excellent awesome
The weather is excellent great
The weather is excellent stunning
The weather is outstanding amazing
The weather is outstanding awesome
The weather is outstanding great
The weather is outstanding stunning
The weather is super amazing
The weather is super awesome
The weather is super great
The weather is super stunning
The weather is superior amazing
The weather is superior awesome
The weather is superior great
The weather is superior stunning
The zone is excellent amazing
The zone is excellent awesome
The zone is excellent great
The zone is excellent stunning
The zone is outstanding amazing
The zone is outstanding awesome
The zone is outstanding great
The zone is outstanding stunning
The zone is super amazing
The zone is super awesome
The zone is super great
The zone is super stunning
The zone is superior amazing
The zone is superior awesome
The zone is superior great
The zone is superior stunning
1 голос
/ 18 июня 2019

Если ваш входной словарь всегда находится в форме, как описано выше, вы можете просто сделать:

for x in d["climate"]:
    for y in d["super"]:
        for z in d["awesome"]:
            print("The {} is {} {}".format(x, y, z))
0 голосов
/ 18 июня 2019

Предполагая, что dict (более подходящим образом) имеет имя synonyms, и вы исправляете его синтаксические ошибки, это должно сделать работу:

import itertools
for x in itertools.product(*[synonyms.get(x,[x]) for x in words]):
    print(" ".join(x))
...