1
votes

Comment itérer certains caractères dans une chaîne et d'autres caractères restent sur la même position en python

J'ai un texte disant par exemple,

t = "Le climat est super génial"

en faisant,

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

ce que j'ai,

>>> words = ["Le", "le climat", "est", " super "," awesome "]

et j'ai plusieurs listes dans un dictionnaire ayant chacune une liste de synonymes. par exemple,

dict = {'climat': [météo, région, zone], 'super': [excellent, supérieur, exceptionnel], 'génial': [incroyable, génial, magnifique ]}

Comment écrire le code pour obtenir les combinaisons de permutation des synonymes dans la phrase. Supposons que nous ayons au moins ou exactement 3 synonymes identifiés pour chacun de nos mots. Alors totalement il y a 3 mots dans la première ligne sélectionnée de «t». Donc 3 à la puissance 3 phrases = 27 phrases sont possibles à générer.

et comment la sortie que je veux?

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

Toute aide à ce sujet, sera vraiment appréciable.


0 commentaires

4 Réponses :


1
votes

Si votre dictionnaire d'entrée est toujours sous la forme décrite ci-dessus, vous pouvez simplement faire:

for x in d["climate"]:
    for y in d["super"]:
        for z in d["awesome"]:
            print("The {} is {} {}".format(x, y, z))


0 commentaires

1
votes

J'ai essayé ceci et cela semble fonctionner:

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

Output:

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)))


1 commentaires

Je remarque maintenant que votre sortie n'incluait pas la phrase originale dans le sac de synonymes, mais peut-être que ce sera utile. :)



0
votes

En supposant que dict soit (plus correctement) nommé synonymes , et que vous nettoyez ses erreurs de syntaxe, cela devrait faire le travail:

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


0 commentaires

1
votes

Utilisation de itertools.product et str.replace:

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

Prints:

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)


0 commentaires