[Tutor] thesaurus

Pete Froslie froslie at gmail.com
Sat Jul 11 20:15:28 CEST 2009


So, I basically have this working so that it pulls a word from a 'txt' file;
searches for the synonym in the thesaurus; returns the result... but then I
am having trouble with probably the most simple part: I cannot seem to go
back into the 'txt' file and replace the word I just searched with the new
word! I tried a few methods. One with re.sub, which I can't seem to get to
work and one adapting Rich's suggestion in this thread. Rich's suggestion
works to a degree-- it replaces all of the words in the 'txt' with the new
word. I'm probably missing something obvious, so thought I'd ask before I
get too far down the confused road. here is the code I'm tinkering with now
(know its a bit messy and will clean it up for a repost  when I'm done):

from __future__ import with_statement
> import re
> from urllib2 import urlopen
>
> word_number = 2 # when incremented-- steps through each word of the text
> file.
> API_URL = '
> http://words.bighugelabs.com/api/2/e413f24701aa30b8d441ca43a64317be/'
> #thesaurus = API_URL + response[word_number]
>
> fin = open("journey_test.txt", "r")
> #really need to make sure I split at all of the delimiters at this point
> response = re.split(r"[/|/,\n, , ,:\"\"\.?,)(\-\<>\[\]\r]", fin.read())
>
> thesaurus = API_URL + response[word_number] + '/'
> print thesaurus # just to test the thesaurus is working
> response2 = urlopen(thesaurus) #set the API to search for the current word
> response3 = re.split(r"[/|/,\n]", response2.read()) # strip the result
> downd to the needed word
>
> print response3[2] # shows the result of the stripping down to the needed
> word
> fin.close()
>
>
> #*****HAVING TROUBLE HERE**********************
> #
> new_word = response3[2]
> old_word = response[word_number]
>
>
> buff = []
> with open("journey_test.txt", "r") as fin:
>   for line in fin:
>       buff.append(" ".join(new_word for old_word in line.split()))
>
> with open("journey_test.txt", "w") as fout:
>   fout.write("\n".join(buff))
> #
> #*****HAVING TROUBLE HERE**********************
>
> """
> #****Different Attempt:  I stopped on*******************
> combine = response3[2]
> combine2 = response[word_number]
>
> fout = open("journey_test.txt", "r/w")
> #data = open("journey_test.txt").read()
> #exchange = re.compile('MAKES')
> fout.write( re.sub(combine, combine2) )  #trying to replace
> fout.close()
> #****THIS IS THE AREA OF DIFFICULTY*******************
> """
>

On Fri, Jul 10, 2009 at 7:26 PM, ALAN GAULD <alan.gauld at btinternet.com>wrote:

>
>
>
>
> > right now, but Chinese apparently has no distinct verb forms for past,
> present,
> > future.  So they rely on other words to indicate which they might mean.
>
>
> Chinese also has the problem of relying on intonation to distinguish
> between
> identically spelled words. We have the same in English - bow(on stage) v
> bow
> (as in arrows), but there are lots of these things in Chinese making
> translation
> of written language almost wholly context dependant, and therefore very
> difficult for a machine.
>
> Alan G
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090711/e59112c2/attachment-0001.htm>


More information about the Tutor mailing list