Sentence Processing

Thread Solved

Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Sentence Processing

 
0
  #1
Nov 5th, 2006
I have a file of sentences similar to this:
  1.  
  2. Tom ate his Apple.
  3. Frank shoveled Snow.
  4. Henry drove a Truck.
I would like to prosess each sentence so it only retains the capital letter in the first word, every other word in the senetnce should be lower case. The result would look like this:
  1.  
  2. Tom ate his apple.
  3. Frank shoveled snow.
  4. Henry drove a truck.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Sentence Processing

 
1
  #2
Nov 5th, 2006
so pull in the lines:

[php]
f = open("sentences.txt","r")
lines = f.readlines()
f.close()
[/php]
Then sort through each line and uppercase each first letter and lowercase the rest; stick those new sentences into a new list:

[php]
lines_new = []

for line in lines:
lines_new.append(line[0].upper() + line[1:].lower())
[/php]
Last edited by LaMouche; Nov 5th, 2006 at 6:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Sentence Processing

 
0
  #3
Nov 6th, 2006
Wow LaMouche, this is sweet Python code. I learned a lot about file reading/writing too. Here is what I have, showing it with my test file:
  1. """
  2. file sentences.txt looks like that:
  3. Tom ate his Apple.
  4. Frank shoveled Snow.
  5. Henry drove a Truck.
  6. """
  7. f_in = open("sentences.txt","r")
  8. lines = f_in.readlines()
  9. f_in.close()
  10. lines_new = []
  11. for line in lines:
  12. lines_new.append(line[0].upper() + line[1:].lower())
  13. f_out = open("sentences_fixed.txt", "w")
  14. for line in lines_new:
  15. f_out.write(line)
  16. f_out.close()
  17. """
  18. file sentences_fixed.txt looks like that:
  19. Tom ate his apple.
  20. Frank shoveled snow.
  21. Henry drove a truck.
  22. """
My actual sentence file works fine too. I think I understand your code. Each line is a sentence and line[0].upper() simply makes certain that the first letter in the sentence is capitalized. To this you concatenate with the + the remainder of the sentence converted to lower case. Nice example of string slicing!

Thanks!
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Sentence Processing

 
0
  #4
Nov 6th, 2006
You got it. I'm glad it works for you. I always like to be a good help to people despite my lack of experience.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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