Python Forum
No output in 'english word generator'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No output in 'english word generator'
#1
Hi everyone

I'm quite new to python, only been doing it for a semester now, so I don't really now quite a lot of syntax, in fact, the syntax I use in the program of our interest today, may well be the only syntax I've fully mastered (that's why it's in the homework section). 'Fully' is a little ironic when posting on a forum ;), so let's get right into it. For a friend, who studies languages and likes to write poems, I tried to code a 'english word generator' that can 1. determine word length based on the normal distribution of the word lenghts in the English language or just use the word length that the user provided it with (this whole part works). And 2. Completely randomly build up a word from nothing (this part works too)(*). And 3. Checks if that word is a valid English word (I posses a digital list with all valid English words to check in), and if it is, print it to display. This last part is where the problems occur: the program runs perfectly natural, but instead of printing a word, it just doesn't print anything. In the variables list, I can see my word being a build-up of random characters, as long as the word length, but not nearly a valid word (it doesn't print, also). I understand that the chance of generating a valid English word this way(*) is pretty small, so I wondered if my computer might just stop trying it after a couple of hundred times. Is that a valid thought? Or should my computer keep running and generating? If it doesn't stop because of this, I don't see why it stops running at a certain point (it kind of 'gives up') because printing out a valid word is actually (at least that's what I think), the only way to stop for the program. I'll include the code in beneath.
Thanks in advance!

(*) I understand this way of 'building' a word might not be the most efficient way to do it (I could also just let the computer pick a random word from the list with valid words, even with the normal distribution (would be pretty much the same outcome and way less difficult for the computer)), but the idea that the computer 'composes' his own words just appeals very much to me! I can't stop thinking about the applications in artificial intelligence!

Extra information: I'm coding in Python 3 (IDE: Thonny) and my computer is a recent laptop with an i7 (7th gen).

Here's the failing code:

import random
alphabet=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
word=""
counter=0

def isValid(word):{
file = open("wordsEn.txt", "r")
if word in file.read():
return True
else:
return False
}
wordlength=input("How many characters do you want the word to have? ")
if wordlength=="choose":{
ql=random.randint(1,1000)
if ql==1:{
wordlength=2}
if ql in range(2,7):{
wordlength=3}
if ql in range(8,33):{
wordlength=4}
if ql in range(34,85):{
wordlength=5}
if ql in range(86,170):{
wordlength=6}
if ql in range(171,292):{
wordlength=7}
if ql in range(293,432):{
wordlength=8}
if ql in range(433,572):{
wordlength=9}
if ql in range(573,698):{
wordlength=10 }
if ql in range(699,799):{
wordlength=11}
if ql in range(800,874):{
wordlength=12}
if ql in range(875,926):{
wordlength=13}
if ql in range(927,958):{
wordlength=14}
if ql in range(959,978):{
wordlength=15}
if ql in range(979,988):{
wordlength=16}
if ql in range(989,994):{
wordlength=17}
if ql in range(995,997):{
wordlength=18}
if ql in range(998,999):{
wordlength=19}
if ql==1000:{
wordlength=20}
}
else:{
wordlength=int(wordlength)
}
while counter<wordlength:{
word=word+alphabet[random.randint(0,25)]

if len(word)==wordlength:{
if isValid(word)==True:{
print("The generated word is: "+word)}
else:{
word=""
counter=0}
}
counter=counter+1
}

I haven't put all those brackets {} in the real file but I saw my indents didn't make it through to the layout on the threadpage, so I've added those to clarify.

On request, I can post the file I'm using with all the English words, but that might take up some space here, and I'm absolutely sure the file is correct (my teacher provided me with it in an earlier assignment and it worked perfectly). For the layout though, here are the first 10 lines:

a
aah
aahed
aahing
aahs
aardvark
aardvarks
aardwolf
ab
abaci


(You guys understand, it's really ALL English words ;))
Reply
#2
Hello, please edit your post so that your code is contained within Python code tags. You can also put output in output tags for better readibility. You can find help here.
Reply
#3
This is not a valid Python code. In Python, a code block is not closed in curly brackets but indented: https://docs.python.org/3/reference/lexi...ndentation
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Yeah I know but as I said, my indents didn't come trough, I'm trying to edit it into Python tags for the post, but I can't find where to edit it anymore. Anyway, the problem has moved on: my python code is now completely valid, but it also allows itself to generate parts of the words in the file (like 'ngish' from 'extuingisher', you see?). Given the great lay-out of the file (every line contains a single word), I wondered if there was a way to make Python understand it should read line per line and compare every word on every line as a whole with its own produced word. So, "ngish" != "extuingisher", you see?

I'm sorry for the above post, the 'wordlenghth' in the first line of the while loop should be 'wordlength + 1'.

So the new problem (don't know if I should start a new post) lies now in the isValid() function:
def isValid(word):
    file = open("wordsEn.txt", "r")
    if word in file.read():
        return True
    else:
        return False

I guess I just need to know a way to go through a text file line per line and check for each line if the line completely matches the generated word, instead of checking if the generated word is in the file (in other words: if the file contains the generated word (e.g. 'extinguisher' contains 'nguish'))
Reply
#5
A lot of what you are trying to do is already included in the NLTK (Natural Language Tool Kit).
You may want to examine that and see if it might be a good fit for your application.
See: http://www.nltk.org/
This is a very comprehensive package and will put all kind of tools at your fingertips.
Reply
#6
Wow I didn't know that was all done already. Great! I'm trying to get better though. At this instance, after some research, I've managed to change my isValid() function to:
def isValid(word):
    file = open("wordsEn.txt", "r")
    for element in file.readlines():
        if element==word:
            return True
        else:
            return False
Though, if I test it with, for example, "extinguisher", it results me 'False'. This is strange, as the word 'extinguisher' is certainly in the list (I've looked it up). I don't really see what I'm doing wrong here :/
Reply
#7
Change the if statement:
if element.strip() == word:
Print 'element' to see what you get from readlines method.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
This seems to work too, is it correct?
def isValid(word):
    file = open("AllEnglishWords.txt", "r")
    if word in file.read():
        return True
    else:
        return False

print(isValid("\n"+word+"\n"))
I adjusted the file with two 'enters' at the beginning and the end of the file.
Reply
#9
You can also write
def isValid(word):
    with open('AllEnglishWords.txt', 'r') as file:
        return any(element == word for element in file)
However, if you need to call isValid() several times, it would be a better idea to open the file only once, that is to say, out of the function.
Reply
#10
I fixed a lot of bugs now, but I'm stuck again with something I don't understand. Here's the failing code:

import random
alphabet=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
word=""
counter=0
wordtype=""
chance=0
timeonbatteryseconds=0
timeonACseconds=0

language=input("What language do you wish to proceed in? (dutch/english) ")
if language=="english" or language=="English" or language=="Eng" or language=="eng":
    wordtype=input("What words do you want to generate? (common/all) ")
    wordlength=input("How many characters do you want the word to have? ")
if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
    wordlength=input("Hoelang moet het woord zijn? ")

def isValid(word):
    if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
        file = open("DutchWords.txt", "r")
        if word in file.read():
            return True
        else:
            return False
    if wordtype=="common":
        file = open("CommonEnglishWords.txt", "r")
        if word in file.read():
            return True
        else:
            return False
    if wordtype=="all":
        file = open("AllEnglishWords.txt", "r")
        if word in file.read():
            return True
        else:
            return False
    if wordtype=="choose":
        decider=random.randint(0,1)
        if decider==0:
            wordtype="common"
        if decider==1:
            wordtype="all"
            

if wordlength=="choose" or wordlength=="kies" or wordlength=="kies maar" or wordlength=="kies zelf" or wordlength=="goh kies maar zelf" or wordlength=="goh kies maar" or wordlength=="aan u de keuze":
    ql=random.randint(1,1000)
    if ql==1:
        wordlength=2
    if ql in range(2,7):
        wordlength=3
    if ql in range(8,33):
        wordlength=4
    if ql in range(34,85):
        wordlength=5
    if ql in range(86,170):
        wordlength=6
    if ql in range(171,292):
        wordlength=7
    if ql in range(293,432):
        wordlength=8
    if ql in range(433,572):
        wordlength=9
    if ql in range(573,698):
        wordlength=10            
    if ql in range(699,799):
        wordlength=11
    if ql in range(800,874):
        wordlength=12
    if ql in range(875,926):
        wordlength=13
    if ql in range(927,958):
        wordlength=14
    if ql in range(959,978):
        wordlength=15
    if ql in range(979,988):
        wordlength=16
    if ql in range(989,994):
        wordlength=17
    if ql in range(995,997):
        wordlength=18
    if ql in range(998,999):
        wordlength=19
    if ql==1000:
        wordlength=20
        
    chance=float((1/(26**wordlength))*100)
    timeonbatteryseconds=float(((26**int(wordlength))*1.5)*(10**(-9)))
    timeonACseconds=float(((26**int(wordlength))*3.6)*(10**(-9)))
        
    if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
        print("Ik ga voor "+str(wordlength)+" letters.")
        print("Aan netstroom zou dit ongeveer "+str(timeonACseconds)+" seconden duren.")
        print("Op batterijstroom duurt dit waarschijnlijk echter "+str(timeonbatteryseconds)+" seconden. Maar ondertussen al weer minder natuurlijk!")
        print("Aan het generen...")
    if language=="english" or language=="English" or language=="Eng" or language=="eng":
        print("I'm going for "+str(wordlength)+" characters.")
        print("On AC power, this should take around "+str(timeonACseconds)+" seconds.")
        print("On battery power, this'll probably take "+str(timeonbatteryseconds)+" seconds. But now already a lot less of course!")
        print("Generating...")
else:
    wordlength=int(wordlength)
    
    chance=float((1/(26**wordlength))*100)
    timeonbatteryseconds=float(((26**int(wordlength))*1.5)*(10**(-9)))
    timeonACseconds=float(((26**int(wordlength))*3.6)*(10**(-9)))
    
    if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
        print("Aan netstroom zou dit ongeveer "+str(timeonACseconds)+" seconden duren.")
        print("Op batterijstroom duurt dit waarschijnlijk echter "+str(timeonbatteryseconds)+" seconden. Maar ondertussen al weer minder natuurlijk!")
        print("Aan het generen...")
    if language=="english" or language=="English" or language=="Eng" or language=="eng":
        print("On AC power, this should take around "+str(timeonACseconds)+" seconds.")
        print("On battery power, this'll probably take "+str(timeonbatteryseconds)+" seconds. But now already a lot less of course!")
        print("Generating...")
while counter<wordlength+1:
    word=word+alphabet[random.randint(0,25)]
    
    if len(word)==wordlength:
        if isValid("\n"+word+"\n")==True:
            if language=="english" or language=="English" or language=="Eng" or language=="eng":
                print("The generated word is: "+word)
            if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
                print("Het gecreƫerde woord is: "+word)
        else:
            word=""
            counter=0
    counter=counter+1
When I enter "english" as language, "common" as wordtype and "3" as wordlength, I get the following error:

Error:
Traceback (most recent call last): File "D:\Documents\1 ADRIAAN\Professioneel\Onderwijs\KU Leuven\Computers en programmeren\Python Programs\EnglishWordGenerator.py", line 118, in <module> if isValid("\n"+word+"\n")==True: File "D:\Documents\1 ADRIAAN\Professioneel\Onderwijs\KU Leuven\Computers en programmeren\Python Programs\EnglishWordGenerator.py", line 24, in isValid if wordtype=="common": UnboundLocalError: local variable 'wordtype' referenced before assignment
I don't understand the error: I gave the wordtype as input?
Don't mind the dutch language in between; I'm trying to make the program elegible for dutch application too.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,407 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Word generator jack1234 10 147,169 Jul-03-2017, 09:30 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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