I'm currently trying to make a sort of "word mixer": for two given words and the desired length specified, the program should return the "mix" of the two words. However, it can be any sort of mix: it can be the first half of the first word combined with the second half of the second word, it can be a random mix, anything really.
Examples:
fish + cake, length 5: fiske
dog + cat, length 4: doga
late + cross, length 6: losste
I've written a very sloppy code (as seen below), and I'd appreciate some tips on what am I doing wrong (since my outputs aren't really good) and if there's anything that can be improved.
from random import randint
name1 = "domingues"
name2 = "signorelli"
names = [name1,name2]
# a list of the desired lengths
lengths = [5,6,7]
mixes = []
def sizes(size):
    if size == 5:
        letters1 = randint(2,3)
    else:
        letters1 = randint(2,size-2)
    letters2 = size-letters1
    return letters1, letters2
def mix(letters1, letters2):
    n = randint(0,1)
    if n == 1:
        a = 0
    else:
        a = 1
    n1 = names[n]
    n2 = names[a]
    result = n1[0:letters2]+n2[-letters1::]
    return result
file = open("results.txt","w+")
for leng in lengths:
    file.write("RESULTS WITH "+str(leng)+" LETTERS \n")
    file.write("\n")
    for i in range(10):
        let1, let2 = sizes(leng)
        result = mix(let1,let2)
        while result == name1 or result == name2:
            result = mix(let2)
        if result not in mixes:
            mixes.append(result)
    for m in mixes:
        if m not in file:
            file.write(m+" \n")
            file.write("\n")
file.close()
(Thanks for taking your time to help me btw, I appreciate it!)
In general, this is AI-related problem, because we are implicitly want to get readable mixed words. I just wrote simple (and dirty) code that tries to catch sequences of vowels and consonants from training data and builds mixed words according to catched rules.
import random
consonants_pat = 'BCDFGHJKLMNPQRSTVXZ'.lower()
vowels_pat = 'aeiouy'
train_data = '''
                This our sentence to be used as a training dataset
                It should be longer
                '''
def build_mixer(train_data, num=3, mixed_len=(2, 4)):
    def _get_random_pattern(td, wlen):
        td_splitted = td.lower().split()
        while True:
            w = random.choice(list(filter(lambda x: len(x)>=wlen, td_splitted)))
            for j in range(len(w)-wlen):
                yield tuple(map(lambda x: 0 if x in vowels_pat else 1,  w[j:j + wlen]))
    def _select_vowels(w):
        return 
    def _mixer(w1, w2, num=num, mixed_len=mixed_len):
        allowed_letters = w1.lower().strip() + w2.lower().strip()
        ind = 1
        for j in range(num):
            wlen = random.choice(range(*mixed_len))
            pattern = _get_random_pattern(train_data, wlen)
            _aux = allowed_letters
            word = ''
            try:
                for pat in pattern:
                    for k in pat:
                        if k == 0:
                            choiced = random.choice(list(filter(lambda x: x in vowels_pat,  _aux)))
                            word += choiced
                        else:
                            choiced = random.choice(list(filter(lambda x: x in consonants_pat,  _aux)))
                            word += choiced
                        l = list(_aux)
                        l.remove(choiced)
                        _aux = ''.join(l)
                    ind += 1
                    yield word
                    if ind>num:
                        raise StopIteration
            except IndexError:
                continue
    return _mixer
mixer = build_mixer(train_data, num=6, mixed_len=(3,6))
for mixed in mixer('this', 'horse'):
    print(mixed)
I got the following words:
het hetihs hetihssro sheo hsio tohir
I recommend taking a random slice of the word string and combining it with another random slice from the second word. Get the len(word) and take a slice of the word randomly using random.randrange(). 
import random
def word_mixer(word1, word2):
    slice1 = word1[:random.randrange(2, len(word1))]
    slice2 = word2[:random.randrange(2, len(word2))]
    return slice1 + slice2
mixed = word_mixer('weasel', 'snake')
print(mixed)
Output:
wesnak
weasesna
weassnak
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With