Connecting Tech Pros Worldwide Help | Site Map

a program for reading a file and correcting a word

Newbie
 
Join Date: Oct 2009
Posts: 3
#1: 3 Weeks Ago
I need to write a program that reads a text file, which contains words that are followed with a preposition. If the preposition is wrong for the word, the program is supposed to warn the reader and suggest the correct preposition that follows a specific word.

Ex:

>> The decrease *of* (in) diseases is a positive development...

the preposition of is not correct, in is.

I got all the prepositions in a seperate text file.

What I've done so far is reading in the whole text file as a string, and then using "split(' ')" to break the string into a list that you can look through word by word.

Appreciate if someone would take the time to help me with this!

Jessica
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#2: 3 Weeks Ago

re: a program for reading a file and correcting a word


Jessica,

It would be helpful if you would show us your code. What algorithm are you using to determine if the usage is proper? Do you have a dictionary of words mapped to the proper preposition?

This simplified example may give you some ideas:
Expand|Select|Wrap|Line Numbers
  1. s = "The decrease of diseases is a positive development."
  2.  
  3. # list of possible prepositions
  4. prepList = ["of", "in"]
  5.  
  6. # dictionary mapping words to proper preposition
  7. properDict = {'decrease': 'in'}
  8.  
  9. def improper_usage(word, prep):
  10.     # code to determine if usage is improper
  11.     # if usage is improper, return True
  12.     return True
  13.  
  14. sList = s.split()
  15. for i, word in enumerate(sList):
  16.     # if the word is a preposition
  17.     if word in prepList:
  18.         if improper_usage(sList[i-1], word):
  19.             # usage is improper, determine proper preposition
  20.             new_prep = properDict[sList[i-1]]
  21.             # get input from the user
  22.             option = raw_input("Replace %s with %s (Y/N)" % (word, new_prep))
  23.             if option.lower() == "y":
  24.                 sList[i] = new_prep
  25.  
  26. # join to words into a sentence
  27. print " ".join(sList)
Newbie
 
Join Date: Oct 2009
Posts: 3
#3: 3 Weeks Ago

re: a program for reading a file and correcting a word


Thank you for replying.

I was given a list of words with prepositions like this:

afraid of
aim at
apprehensive of
approve of
article on
available for
aware of
because of
beware of
call for
capable of
certain of
change in
characteristic of
comment on
conceive of
conscious of
consequence of
consist of
copy of
critical of
decline in
decrease in
demand for
dependent on
designed for
desire for
development in
development of
die of
difference in
discussion on
discussion about
disparity in
dispose of
drop in
empty of
end of
end to
equality in
estimate at
evidence for
exception to
exchange for
explanation for
fall in
free of
full of
hear of
hope for
improvement in
incapable of
increase of
increase by
increase in
increase with
independent of
indicative of
interest in
irrespective of
know of
knowledge of
learn of
limit to
look at
mean by
multiply by
necessity for
need for
need of
neglectful of
oblivious of
obstacle to
paper on
problem of
proud of
rate of
reason for
regardless of
requirement for
result of
sceptical of
short of
shortage of
similarity in
skill in
smell of
speak of
start to
sure of
surplus of
think of
threat to
time of
transcript of
typical of
useful for
useful to
variation in
version of
wish for
work of
work on
work with
worthy of

What I've done so far is reading the textfile which needs correction and saving it as a list with the split function. I don't really know what I should do with the words and prepositions, and how to determine if the usage is proper. I would be glad if you knew how to help me with this..
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#4: 3 Weeks Ago

re: a program for reading a file and correcting a word


This looks like homework. Did you understand the script I wrote in my previous reply? Your list of words/prepositions would make up the proper usage dictionary (propDict in my post). You would then need to create a list of all the possible prepositions to be in your "watch list" (prepList in my post). You don't really need function improper_usage(). Just test for membership of the word preceeding the preposition in propDict. My code gives you the solution. Now you need to show us some code of your own.

BV

Hint - You can make propDict like this:
Expand|Select|Wrap|Line Numbers
  1. s = '''afraid of
  2. aim at
  3. apprehensive of
  4. approve of
  5. article on'''
  6.  
  7. sList = [item.split() for item in s.split('\n')]
  8.  
  9. prepDict = dict(zip([item[0] for item in sList], [item[1] for item in sList]))
Newbie
 
Join Date: Oct 2009
Posts: 3
#5: 2 Weeks Ago

re: a program for reading a file and correcting a word


You're right, and it's killing me!

Alright, I'm gonna try and do this on my own and I will return when I've come a bit further. But just one question about how to create the propDict. I go all the words and prepositions in a file and I want to keep it that way. So i read the file like:

prepWords=open("prepositions.txt", "r")

How do I then create a dictionary from this with the words as keys and prepositions as values? Maybe you explained this to me but I didn't really get this.

Jessie
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#6: 2 Weeks Ago

re: a program for reading a file and correcting a word


Here's some psuedocode to create prepDict.

Open file and assign to fileObj
Initialize a dictionary
Iterate on the file object (for line in fileObj:)
....Strip and split line, assign to a variable name lineList
....Assign dd[key] = value where key = lineList[0] and value = lineList[1]
Close file
Reply


Similar Python bytes