A program reads a file and corrects a word

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CuteBeginner
    New Member
    • Oct 2007
    • 1

    A program reads a file and corrects a word

    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 =)??
  • KaezarRex
    New Member
    • Sep 2007
    • 52

    #2
    Originally posted by CuteBeginner
    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.

    Comment

    • KaezarRex
      New Member
      • Sep 2007
      • 52

      #3
      Maybe something like this:

      [CODE=python]prepositions = [] #put all your prepositions in here
      words = {} #put {word : preposition} pairs in here
      file = open("file.txt" , "r")
      text = file.read()
      text = text.split(' ')
      for i in range(len(text) ):
      if text[i] in prepositions and i > 0:
      #make sure that text[i - 1] is the right word
      #then suggest the right preposition if needed[/CODE]
      Remember that your also going to have to ignore punctuation some how.

      Comment

      Working...