943,962 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 5691
  • Python RSS
Aug 31st, 2005
0

Reading from a file question

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  1. cart - art car tar
  2. bagel - age bag gel
  3. 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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Rete is offline Offline
23 posts
since Jun 2005
Aug 31st, 2005
0

Re: Reading from a file question

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
Python Syntax (Toggle Plain Text)
  1. ...
  2. aaardkrv aardvark
  3. ...
  4. dgino doing dingo
  5. ...

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...
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Sep 1st, 2005
0

Re: Reading from a file question

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)

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?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Rete is offline Offline
23 posts
since Jun 2005
Sep 1st, 2005
0

Re: Reading from a file question

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:
Python Syntax (Toggle Plain Text)
  1. import sys, re
  2. # Open the file with read-access
  3. f = open("doc/dict.txt", "r")
  4. # Read the file text as one big block
  5. text = f.read()
  6. # Compile a regex pattern to catch words and definitions
  7. p = re.compile("(([\w]+)[^\\n]+)\\n")
  8. # Find all matches, store in l
  9. l = re.findall(p, text)
  10. # Close the file
  11. f.close()
  12. # The list 'l' is a list of tuples, such that for each
  13. # tuple, the first coordinate is the definition, and
  14. # the second coordinate is the corresponding word
  15. # Break the list down into two separate lists
  16. words = []; definitions = []
  17. for x in l: words.append(x[1]); definitions.append(x[0])
  18. # At this point, the word in words[i] corresponds to the
  19. # definition in definitions[i]
  20. # Print them
  21. print words
  22. print definitions
Read the regex part of the Python tutorial to figure out what (([\w]+)[^\\n]+)\\n means.
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Modification of Vigenere en/deciphering algorithm
Next Thread in Python Forum Timeline: Plotting math functions (Python)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC