3

So I am writing a program in python 2.7 that loops through all of the words in the English language to see if the Morse code version of English word matches the unknown Morse phrase. The reason I can't just interpret it is because there are no spaces between letters. This is a snippet of the code:

def morse_solver(nol,morse,words): 
#nol is the number of letters to cut down search time,morse is the Morse phrase to decode, and words is a string (or can be a list) of all english words.
    lista=_index(words)
    #_index is a procedure that organizes the input in the following way:[nol,[]]
    selection=lista[nol-1][1]
    #selects the words with that nol to loop through
    for word in selection:
        if morse_encode(word)==morse:
            print morse+"="+word

So my Question is:
It's kinda hard to find a list of all the words in the English language and copy it over into a huge string. So is there a way or some Python module to access all the words in the English dictionary by only having to type a little bit?

If such a thing doesn't exist, how can I handle such a large string? Is there some place I can copy paste from (onto just one line)? Thanks in advance

Moshe Goldberg
  • 461
  • 2
  • 15
  • I would recommend naming `index()` something else, as you're masking the built-in function of the same name. – TigerhawkT3 Jul 10 '15 at 00:01
  • please dont make duplicate. no other open answers (or closed ones) had good or helpful answers – Moshe Goldberg Jul 10 '15 at 00:01
  • I tend to make many mistakes like that, as I am quite new to python. thanks – Moshe Goldberg Jul 10 '15 at 00:02
  • What platform are you on? If you're on a Unix-like, try opening `/usr/share/dict/words` in read mode. – Kevin Jul 10 '15 at 00:15
  • @Kevin I am using Windows 8.1 – Moshe Goldberg Jul 10 '15 at 00:58
  • 2
    Google yawl.txt or enable.txt. Both word files are easy to find on the web and can be loaded into a Python dictionary or set in just 2 or 3 lines of code. – John Coleman Jul 10 '15 at 02:18
  • @JohnColeman ok so I found the files, but how can I fit it onto just a few lines it has one word per line. – Moshe Goldberg Jul 10 '15 at 19:44
  • 2
    @JediPythonClone - I said that you can load it into a Python data structure in just a few lines of code. Something like `wfile = open("yawl.txt")` then `wordlist = wfile.read().split('\n')` then `wfile.close()` . The resulting list itself has 200000+ entries in it -- but it takes less than a second to load and is easy to use. If you prefer a set or a dictionary to a list, such transformations are each 1 line of code. – John Coleman Jul 10 '15 at 19:59
  • @JohnColeman oh thanks I got it.... That would work in Python 2.7 right? – Moshe Goldberg Jul 10 '15 at 20:23
  • 1
    @JediPythonClone It should be no problem in Python 2.7 -- though it's been years since I've used Python 2 and no I longer remember all of the little differences. – John Coleman Jul 10 '15 at 20:26
  • Why not just make an interpreter for Morse? – rassa45 Jul 10 '15 at 21:02
  • @ytpillai I am but how do – Moshe Goldberg Jul 10 '15 at 21:02
  • I do that if there are no breaks between letters – Moshe Goldberg Jul 10 '15 at 21:03
  • @JohnColeman I tried it with both yawl.text and enable1.text(which I downloaded it, but it gives me a traceback error that says – Moshe Goldberg Jul 10 '15 at 21:27
  • @JohnColeman it says Traceback (most recent call last): File "C:\Users\michael\Desktop\Python Projects\you.py", line 4, in wfile=open('yawl.txt') IOError: [Errno 2] No such file or directory: 'yawl.txt' – Moshe Goldberg Jul 10 '15 at 21:27
  • Well then -- pass it a file that does exist. Note that in Python string literals '\' needs to be escaped as '\\', though all recent versions of Windows accept '/' as a file-path delimiter, so using '/' instead of '\' is probably the easier fix. – John Coleman Jul 10 '15 at 21:31
  • @JohnColeman thanks it worked – Moshe Goldberg Jul 10 '15 at 21:48
  • @JediPythonClone Well, you could figure out how regular people interpret morse code words and come up with a dictionary for it. You'll have to start at the basic letter by letter, but can incorporate more complicated grammar as well. I don't know Morse Code, but if you learn how to use it in real life, you can just apply the same logic to Python – rassa45 Jul 11 '15 at 00:47

2 Answers2

1

There is a dictionary tool for python called enchant. Check out this thread.

How to check if a word is an English word with Python?

Community
  • 1
  • 1
ate50eggs
  • 444
  • 3
  • 14
1

What fun is Morse code if you can't hear it? Let me know if this doesn't work in Python 2.7:

from winsound import Beep
from time import sleep

dot = 150 # milliseconds
dash = 300 
freq = 2500 #Hertz
delay = 0.05 #delay between beeps in seconds

def transmit(morseCode):
    for key in morseCode:
        if key == '.':
            Beep(freq,dot)
        elif key == '-':
            Beep(freq,dash)
        else:
            pass #ignore (e.g. new lines)
        sleep(delay)

example = '----. ----.   -... --- - - .-.. .   --- ..-.   -... . . .-.'
#This last is the first line of
#99 Bottles of Beer in Morse Code
#from http://99-bottles-of-beer.net/language-morse-code-406.html

transmit(example)
John Coleman
  • 51,337
  • 7
  • 54
  • 119