By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,158 Members | 917 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,158 IT Pros & Developers. It's quick & easy.

A program reads a file and corrects a word

P: 1
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 am having a hard time starting, I don't know how the whole structure should look? maybe a dicitionary with the words (as keys) and prepositon. {"decrease":"of"} ?

Could anybody help =)??
Oct 15 '07 #1
Share this Question
Share on Google+
2 Replies


KaezarRex
P: 52
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 am having a hard time starting, I don't know how the whole structure should look? maybe a dicitionary with the words (as keys) and prepositon. {"decrease":"of"} ?

Could anybody help =)??
A dictionary sounds like a good idea to me. You'll also probably want a list of all the prepositions you might encounter so your program knows where to check for the proper prepositions. Id suggest 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. That way when you come across a preposition, you can back up one index and check the word in front of it.
Oct 15 '07 #2

KaezarRex
P: 52
Maybe something like this:

Expand|Select|Wrap|Line Numbers
  1. prepositions = [] #put all your prepositions in here
  2. words = {} #put {word : preposition} pairs in here
  3. file = open("file.txt", "r")
  4. text = file.read()
  5. text = text.split(' ')
  6. for i in range(len(text)):
  7.     if text[i] in prepositions and i > 0:
  8.         #make sure that text[i - 1] is the right word
  9.         #then suggest the right preposition if needed
Remember that your also going to have to ignore punctuation some how.
Oct 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.