7 Replies - 1924 Views - Last Post: 07 June 2012 - 06:22 PM Rate Topic: -----

#1 bogahoga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-December 11

How do I display a sentence from a text file with given word.

Posted 07 June 2012 - 12:30 PM

I am trying to create a program that will print the sentence in a text with the given word, but am not sure where to go from here.
Here's the code so far:
#print sentences etc with word by looking for both punctuation
def stuff():
    word = raw_input("Put in a word!")
    f = open("Catcher.txt")
    m = f.read()
    while(len(m) > 0):
        index = m.find(word)
        for x in range(index, len(m)):
            if x is '.':
                i = m.index(x)               
        m = m[x + 1: len(m)]
 


and heres some of the text file, so that you get an idea:

HE CATCHER IN THE RYE by J.D. Salinger
TO
MY MOTHER
1
If you really want to hear about it, the first thing you'll probably want to know is where I was born, an what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don't feel like going into it, if you want to know the truth. In the first place, that stuff bores me, and in the second place, my parents would have about two hemorrhages apiece if I told anything pretty personal about them. They're quite touchy about anything like that, especially my father. They're nice and all--I'm not saying that--but they're also touchy as hell. Besides, I'm not going to tell you my whole goddam autobiography or anything. I'll just tell you about this madman stuff that happened to me around last Christmas just before I got pretty run-down and had to come out here and take it easy. I mean that's all I told D.B. about, and he's my brother and all. He's in Hollywood. That isn't too far from this crumby place, and he comes over and visits me practically every week end.



Is This A Good Question/Topic? 0
  • +

Replies To: How do I display a sentence from a text file with given word.

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: How do I display a sentence from a text file with given word.

Posted 07 June 2012 - 12:43 PM

There are more than 500,000 users on the DIC. Can you imagine if they all titled threads as "Problem" ? Please use meaningful titles so the people with the most knowledge in your area are drawn to your question. This also helps people later using search engines to find they help they need.

So you're saying that if the key word is "first" you want to display the sentence or sentences that contain that word? Is that right?

To start with stop and think about the logic.
The word won't be the first word of the sentence, so you have to conduct TWO loops:
You have to look backwards for a period
You have to look forward for a period.
Then take the text in the middle.

Though personally, I think I would break down the process a bit more.
Split the big text into an array of sentences at the periods.
Then loop through each sentence (element of the sentence array)
If it has the key word, then you already have the sentence so show it.
Was This Post Helpful? 2
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: How do I display a sentence from a text file with given word.

Posted 07 June 2012 - 04:36 PM

View PosttlhIn`toq, on 07 June 2012 - 03:43 PM, said:

There are more than 500,000 users on the DIC. Can you imagine if they all titled threads as "Problem" ? Please use meaningful titles so the people with the most knowledge in your area are drawn to your question. This also helps people later using search engines to find they help they need.


tlhIn`toq, I don't always love your responses, but this should be one of the canned responses that we can automatically post after any bad topic. Thank you sir, I couldn't have said it better myself.

This post has been edited by atraub: 07 June 2012 - 04:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: How do I display a sentence from a text file with given word.

Posted 07 June 2012 - 04:40 PM

LOL - Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 859
  • View blog
  • Posts: 2,308
  • Joined: 28-March 11

Re: How do I display a sentence from a text file with given word.

Posted 07 June 2012 - 04:56 PM

Changed title from "Problem with program" to current.
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: How do I display a sentence from a text file with given word.

Posted 07 June 2012 - 06:02 PM

Alright, back on topic (topic... bahahaha!). I'm not sure if I fully understand your question. Are you simply saying, "Given a word, find the sentence containing that word"? That's actually very easy to do in python. If you use str.split(char), you can aplit the file into a list of strings, if you use the right characters, you can make it a list of sentences. Then, all you'd have to do is find which sentences in that list contains the word. Simple, right? However, I do fear I may have misunderstood something.

This post has been edited by atraub: 07 June 2012 - 06:16 PM

Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: How do I display a sentence from a text file with given word.

Posted 07 June 2012 - 06:17 PM

atraub I don't think you misunderstand at all. And if you do, then we are both in the wrong because it means we both read it wrong in the same way.

View PosttlhIn`toq, on 07 June 2012 - 01:43 PM, said:

Split the big text into an array of sentences at the periods.
Then loop through each sentence (element of the sentence array)
If it has the key word, then you already have the sentence so show it.


At this point there is nothing left to do but wait for the OP to respond with questions or updated code.
Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: How do I display a sentence from a text file with given word.

Posted 07 June 2012 - 06:22 PM

Whoops, I didn't read the very bottom, my bad!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1