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

Life of Brian: Conjugate the verb, "to go" !

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  memory error using permutation list of 11 elements kikidog 1 111 Sep-10-2019, 08:22 PM
Last Post: ichabod801
  while + if + elif + else combination ClassicalSoul 1 292 Mar-27-2019, 03:54 PM
Last Post: ichabod801
  Calling list() on permutation trevorkavanaugh 2 281 Mar-01-2019, 06:00 AM
Last Post: trevorkavanaugh
  Sentence maker help bidoofis 2 374 Feb-08-2019, 03:59 AM
Last Post: bidoofis
  wont print last sentence.. mitmit293 2 294 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,060 Nov-18-2018, 08:54 PM
Last Post: AFoeee
  Generating all simple sentence possibilities Max_77 3 621 Oct-10-2018, 11:35 AM
Last Post: Max_77
  Python - Limit Sentence Length to 10 Words - Text file dj99 2 1,153 Jul-21-2018, 02:24 PM
Last Post: dj99
  Even/Odd permutation braankoo 7 1,912 Jul-18-2018, 12:27 PM
Last Post: braankoo
  Permutation help. jarrod0987 1 517 Jun-28-2018, 04:44 PM
Last Post: ichabod801

Forum Jump:

Users browsing this thread: 1 Guest(s)