Bottom Page

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

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])
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.
(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.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Remove a sentence if it contains a word. lokhtar 6 321 Feb-11-2020, 04:43 PM
Last Post: stullis
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 475 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  memory error using permutation list of 11 elements kikidog 1 538 Sep-10-2019, 08:22 PM
Last Post: ichabod801
  while + if + elif + else combination ClassicalSoul 1 499 Mar-27-2019, 03:54 PM
Last Post: ichabod801
  Calling list() on permutation trevorkavanaugh 2 500 Mar-01-2019, 06:00 AM
Last Post: trevorkavanaugh
  Sentence maker help bidoofis 2 623 Feb-08-2019, 03:59 AM
Last Post: bidoofis
  wont print last sentence.. mitmit293 2 476 Jan-27-2019, 05:38 PM
Last Post: aakashjha001
  Is there a datepicker which can be used in combination with the python library urwid? AFoeee 5 1,746 Nov-18-2018, 08:54 PM
Last Post: AFoeee
  Generating all simple sentence possibilities Max_77 3 833 Oct-10-2018, 11:35 AM
Last Post: Max_77
  Python - Limit Sentence Length to 10 Words - Text file dj99 2 1,682 Jul-21-2018, 02:24 PM
Last Post: dj99

Forum Jump:

Users browsing this thread: 1 Guest(s)