Reading from a file question

Reply

Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Solved Threads: 1
Rete Rete is offline Offline
Newbie Poster

Reading from a file question

 
0
  #1
Aug 31st, 2005
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:

  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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Reading from a file question

 
0
  #2
Aug 31st, 2005
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
  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...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Solved Threads: 1
Rete Rete is offline Offline
Newbie Poster

Re: Reading from a file question

 
0
  #3
Sep 1st, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: Reading from a file question

 
0
  #4
Sep 1st, 2005
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:
  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.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC