Python Forum
how to get all the possible permutation and combination of a sentence in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get all the possible permutation and combination of a sentence in python
#1
Hi,

I have a text say for example,

t = "life is amazing"

by doing,

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

what I got,


>>>words = ["life","is","amazing"]


and I have multiple lists in a dictionary each having a list of synonyms.
for example,

{'life' : [life,living,aliveness,spirit,...], 'is' : [be, exist, follow,..],
'amazing' : [vex,pose,flummox,..], ... }

with the help of the following code,

for i in range(0,len(dict)):
    for j in range(0,len(words)):
        if words[j] == (list(dict)[i]):
            for k in range(0,len((dict)[words[j]])):
                z = t.replace(words[j],(dict)[words[j]][k])
                print(z)
how the output I am getting is,

life is amazing
living is amazing
aliveness is amazing
spirit is amazing
...
life be amazing
life exist amazing
life follow amazing
...
life is vex
life is pose
life is flummox
...

and how the output I want ?

I want all the permutation combinations of the synonyms in the sentence.
Assume that we have at least or exactly 5 identified synonyms for each of our words.
Then totally there are 3 words in the selected first line of 't'.
Therefore 5 to the power of 3 sentences = 125 sentences are possible to generate.
Any help regarding this, will be really appreciable.
Reply
#2
(Jun-13-2019, 06:53 AM)sodmzs Wrote: I want all the permutation combinations

There is built-in itertools module in Python which have permutations and combinations
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,730 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  while sentence kimyyya 3 2,942 Mar-20-2021, 06:00 AM
Last Post: Pedroski55
  Group List Elements according to the Input with the order of binary combination quest_ 19 6,412 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  Even/Odd permutation braankoo 9 11,595 Jan-10-2021, 01:19 AM
Last Post: Larz60+
  List / arrays putting in sentence Kurta 3 2,556 Dec-25-2020, 11:29 AM
Last Post: Larz60+
  How to make a telegram bot respond to the specific word in a sentence? Metodolog 2 6,343 Dec-22-2020, 07:30 AM
Last Post: martabassof
  How to match partial sentence in long sentence Mekala 1 1,522 Jul-22-2020, 02:21 PM
Last Post: perfringo
  Remove a sentence if it contains a word. lokhtar 6 5,894 Feb-11-2020, 04:43 PM
Last Post: stullis
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 3,870 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  Regex Help for clubbing similar sentence segments regstuff 3 2,152 Nov-20-2019, 06:46 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020