Python Wordlist generation problem (2.7)

Asked

Viewed 106 times

2

I’m trying to write a python script that takes n keywords and a number as input. This number will be my maximum range of string that will be written to the Wordlist.

1° Problem: As many combinations as possible have to be made between these input strings, without losing any possible combination or string in the middle of the path;

2nd Problem: If you exceed the range defined in the input, the string cannot be cut, simply the word that does not fit into the default "space" is outside the string of the process in question, but words left out also need to go through the process of having as many combinations as possible with all other words.

My code has some screen outputs because I am debugging but the idea is that the output at the end goes to a text file.

I’m almost there, but the strings still come out with a letter or two that shouldn’t.

My code is as follows (python 2.7):

import sys
import itertools

maxi = sys.argv[1]

  if int(maxi) > 24:
    print ">>For secure, the max of range of string was setup as 24<<"
    print "Old value: " + maxi
    maxi = 24

maxi = int(maxi)

words = sys.argv[2:]
wn = len(words)
print words
print "Inserted Words: " + str(len(words))

cw = 0
cutd = 0
nwords = []
tmp = ""
for word in itertools.imap(''.join, itertools.permutations(words, r=wn)):
    if len(word) <= maxi:
        nwords.append(word)
        nwords = list(set(nwords))
        cw = cw+1
                #Permutations, slicing of the big ones
    else: 
        word = word[:maxi]
        for inp in words:
            if word.find(inp) != -1: # if word has inp do, returning != ==  True:
                tmp = word.replace(inp, "")

        word = word.replace(tmp,"")
        nwords.append(word)
        nwords = list(set(nwords))
        cutd = cutd+1

print nwords
print "\nWords Count: " + str(cw)
print "\nCutted Words Count: " + str(cutd)
print "\nFinal Words in list: " + str(len(nwords))
  • 3

    Hard to understand what is the purpose of the code. It has how to give an example of an input and what would be the expected output?

  • Really hard to understand what the goal is, I tried to run this code in python3, several errors.

  • @Andersoncarloswoss How the Code should be working: Let’s say the input is: 8 "guy" "candle" "pineapple" "Berta" The output should be: caravel, velacara, pineapple, Berta OU input: 5 "Face" "candle" "pineapple" "Crossbow" Outlet: face, candle, Crossbow

  • @Andersoncarloswoss The point is that itertools.product() makes all the possible combinations between strings, so what I thought was to take this string generated by the function and see if it fits the range that is the maxi variable, defined in the input, fits, goes to nwords[] list, if it doesn’t fit she goes through a treatment. First it is cut well in the position defined by the range with word = word[:maxi] then, and this is what is giving error, it should be checked in this string if when it is cut and there is a syllable or letter left of the string before the treatment

  • @Sidon the code was written in 2.7, I forgot to tell you before

  • @Gmattos but the itertools.product does not return a list? I did not understand this detail of remaining syllable.

  • @Andersoncarloswoss Yes but for every word generated by itertools.product and inserted in the list by if len(word) <= maxi: that checks if the generated word is within the limit, if it is, goes untreated, if it is not, that’s where the problem lives. I cut the string according to the limit word = word[:maxi], after that, depending on the combination, one syllable or letter remains, the logic would be to check which complete words exist in this cut string, delete those words until you identify the rest, delete that rest of the string that will actually end up in the list.

Show 2 more comments

1 answer

1


I managed to develop the proposed problem, in my git I will put the full version and also continue working on its improvement.

import sys, os
import itertools


def clnw(modelos, palavra):

    for a in modelos:

        palavra = palavra.replace(a, "")

    lx = palavra

    if lx != "":
        return lx 
    else: return "" 

def fixw(rst, plvr,total):
    if total == plvr.rfind(rst):
        return plvr
    else:
        st = plvr.rfind(rst)
        fn = len(plvr) -1

        plvrl = list(plvr)
        rstl = list(rst)

        while fn >= st:

            del plvrl[fn]

            fn = fn-1
        fix = ''.join(plvrl)

        return fix

maxi = sys.argv[1]

if int(maxi) > 24:
    print(">>For secure, the max of range of string was setup as 24<<")
    print("Old value: " + maxi)
    maxi = 24

maxi = int(maxi)

words = sys.argv[2:]
words.sort()
words.sort(key=len, reverse=True)

wn = len(words)
print(words)
print("Inserted Words: " + str(len(words)))
cw = 0
cutd = 0
nwords = []
tmp = ""
for word in map(''.join, itertools.permutations(words, r=wn)):
    cw = cw+1
    if len(word) <= maxi:
        nwords.append(word)
        nwords = list(set(nwords))

    else:
                #Permutations, slicing of the big ones

        word = word[:maxi]

        tmp = clnw(words, word)

        wn = wn-1


        word = fixw(tmp,word,maxi)
        nwords.append(word)
        nwords = list(set(nwords))


print(sorted(nwords))

temp = "temp.txt"

f = open(temp, "w")

for w in nwords:
    w = w.strip()
    f.write(w + '\n')

f.close()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.