| | |
Reading from a file question
![]() |
•
•
Join Date: Jun 2005
Posts: 23
Reputation:
Solved Threads: 1
Hi, I've been trying to make a really simple word unscrambler, that takes a scrambled word, and then compares it to a txt file filled with words. The problem I'm getting, is that I'm trying to only read the first word of each line, and if it finds the word, then it prints out the entire line.
So, my txt file kinda looks like this:
If the user inputs "ikeb", then the program would only look at "cart", "bagel", and then "bike" before printing out "bike - ". Can anybody help me?
So, my txt file kinda looks like this:
Python Syntax (Toggle Plain Text)
cart - art car tar bagel - age bag gel bike -
If the user inputs "ikeb", then the program would only look at "cart", "bagel", and then "bike" before printing out "bike - ". Can anybody help me?
Suppose you have the scrambled string, "urcoppnei".
You want to be able to quickly go from scrambled string to unscrambled. How?
Sort the letters of the string in increasing order. That gives us "ceinoppru".
Now take every word in the dictionary, and sort their letters in increasing order. Put this in a hash, where the sorted form is the key and a list of words is the value. (Use a list, not a single word, because anagrams have the same sorted form. For example, the key "dgino" would have as its value a list containing both "doing" and "dingo".)
Once you do that, look up "ceinoppru" in the hash. You'll probably find that the key "ceinoppru" maps to a list containing one element, "porcupine".
Of course, it would be better to have your dictionary file in the format
It wouldn't hurt to sort it by the letter-sorted key.
So, you would unscramble by sorting the letters of the word in ascending order, by looking for this result in the text file (or hash, or map, or whatever).
I don't really understand your file format -- it looks like it's listing words with subwords. Why?
And here's a second edit. What you are asking and your sample input and output seem to be on completely different topics! You leave me baffled...
You want to be able to quickly go from scrambled string to unscrambled. How?
Sort the letters of the string in increasing order. That gives us "ceinoppru".
Now take every word in the dictionary, and sort their letters in increasing order. Put this in a hash, where the sorted form is the key and a list of words is the value. (Use a list, not a single word, because anagrams have the same sorted form. For example, the key "dgino" would have as its value a list containing both "doing" and "dingo".)
Once you do that, look up "ceinoppru" in the hash. You'll probably find that the key "ceinoppru" maps to a list containing one element, "porcupine".
Of course, it would be better to have your dictionary file in the format
Python Syntax (Toggle Plain Text)
... aaardkrv aardvark ... dgino doing dingo ...
It wouldn't hurt to sort it by the letter-sorted key.
So, you would unscramble by sorting the letters of the word in ascending order, by looking for this result in the text file (or hash, or map, or whatever).
I don't really understand your file format -- it looks like it's listing words with subwords. Why?
And here's a second edit. What you are asking and your sample input and output seem to be on completely different topics! You leave me baffled...
•
•
Join Date: Jun 2005
Posts: 23
Reputation:
Solved Threads: 1
Sorry, I stated my problem poorly. I can search for the words fine. (I don't sort them into least to greatest, I just check the first character, and then see if it's in the word I'm checking, and if it is, I delete both. Then I go onto the next character, and repeat. If all the characters are deleted, then it's a matching word.)
That's probably not a very good way of doing it, but I was just wondering if there was anyway of reading only the first word of each line in a file. so if I had a .txt that had the word, followed by it's definition (see fig 1)
the only thing the program would check, would be the first words on each line (fig 2)
and then if it found the word, i'd print out both it's definition, as well as the word. So if the scrambled word was erebed (beered) the program would actually print out
beered - A drunk person
Sorry for the bad explanation earlier, I used a pretty bad example. But, yeah, that's the problem I'm kinda facing, and I was wondering if anyone could help me with that?
That's probably not a very good way of doing it, but I was just wondering if there was anyway of reading only the first word of each line in a file. so if I had a .txt that had the word, followed by it's definition (see fig 1)
Fig 1. This is just a text document, not the output bared - A naked object beered - A drunk person bargain - to barter
the only thing the program would check, would be the first words on each line (fig 2)
Fig 2. computer looks at only the first words of each line bared beered bargain
and then if it found the word, i'd print out both it's definition, as well as the word. So if the scrambled word was erebed (beered) the program would actually print out
beered - A drunk person
Sorry for the bad explanation earlier, I used a pretty bad example. But, yeah, that's the problem I'm kinda facing, and I was wondering if anyone could help me with that?
So all you need is a list where each element is the first word on each line in the text file? This is a fairly simple regular expression problem:
Read the regex part of the Python tutorial to figure out what (([\w]+)[^\\n]+)\\n means.
Python Syntax (Toggle Plain Text)
import sys, re # Open the file with read-access f = open("doc/dict.txt", "r") # Read the file text as one big block text = f.read() # Compile a regex pattern to catch words and definitions p = re.compile("(([\w]+)[^\\n]+)\\n") # Find all matches, store in l l = re.findall(p, text) # Close the file f.close() # The list 'l' is a list of tuples, such that for each # tuple, the first coordinate is the definition, and # the second coordinate is the corresponding word # Break the list down into two separate lists words = []; definitions = [] for x in l: words.append(x[1]); definitions.append(x[0]) # At this point, the word in words[i] corresponds to the # definition in definitions[i] # Print them print words print definitions
Vi veri veniversum vivus vici
![]() |
Similar Threads
- RE: reading .wav file and putting ito inot the array (C)
- Reading .dat file (Visual Basic 4 / 5 / 6)
- Reading an input file as a class memeber function (C++)
- Hosts file question about Gorilla's explanation (Viruses, Spyware and other Nasties)
- reading txt file into array (C++)
- Not reading in entire file. (C++)
- problem reading text file to struct (C++)
- reading and printing a file to screen in C (C)
Other Threads in the Python Forum
- Previous Thread: K-means clustering
- Next Thread: Plotting math functions (Python)
| Thread Tools | Search this Thread |
abrupt accessdenied ansi anti apache application approximation argv array assignment backend beginner binary bluetooth builtin calculator change character converter countpasswordentry curved customdialog dan08 dictionary edit exe file float format function gnu heads homework ideas inches input java keyboard lapse leftmouse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook pointer prime programming py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session software sprite sqlite statictext statistics string strings syntax terminal text thread threading time tlapse tuple twoup ubuntu unicode unit urllib urllib2 variable wordgame write wxpython xlib






