[Tutor] Looking for Words - Help

Dave Angel davea at davea.name
Fri Oct 11 12:23:00 CEST 2013


On 11/10/2013 01:18, Jackie Canales wrote:

> Need assistance with a questions in regards to python:
> 1. function occurs(name, word) which looks for a word in the file with name name.
> 2. for each occurrence of the word we want to display its context by showing the 5 words (or so) preceding and following the occurrence, e.g. '... a man to set the river on fire. He had ...' for the first occurrence of 'river' in 'innocents.txt'.
> 3. since the results may be long, we want to collect them all, and write them to a file whose name should be 'occurs'+name.
>
> Hint: at first ignore writing the results to a file. Simply collect all material in a string which you print to the screen. Then writing it to a file will be simple. To get both the word and its context you need an indexed loop through the words. Use the stripw() function we saw on individual words to make finding hits more accurate (e.g. the program found 'river.' above). Finally, the join() method will come in handy to reconstruct the context as a string.
>
>
> Link to final product: http://imgur.com/q1aAAhp
>
> For my program this is what i have so far, I am kinda lost at this point if you can please guide me to help resolve this program.
>
> def lines(name, word):
>     'print all lines of name in which word occurs'
>
>     infile = open(name, 'r')
>     lst = infile.readlines()
>     infile.close()
>
>     for i in range(len(lst)):
>         line = lst[i]
>         if wordin(word, line):
>             w = ('Word found in line {}:'.format(i))
>             #x = (lst[i+1])
>             y = lst[i]        
>              
>             print (''.join(y))
>
> <html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:12pt"><div>Need assistance with a questions in regards to python:</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>1.<span style="font-family: 'Times New Roman'; font-size: medium;"> </span><span style="font-family: 'Times New Roman'; font-size: 16px;">function occurs(name, word) which looks for a word in the file with name name.</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Times New Roman'; background-color: transparent; font-style: normal;"><span style="font-family: 'Times New Roman'; font-size: 16px;"><span class="Apple-tab-span" style="white-space:pre">	</span>2. </span>for each occurrence of the word we want to display its context by showing the 5 words (or so) preceding and following the occurrence, e.g. '... a man to set the river
>  on fire. He had ...' for the first occurrence of 'river' in 'innocents.txt'.</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Times New Roman'; background-color: transparent; font-style: normal;"><span class="Apple-tab-span" style="white-space:pre">	</span>3. since the results may be long, we want to collect them all, and write them to a file whose name should be 'occurs'+name.</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Times New Roman'; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Times New Roman'; background-color: transparent; font-style: normal;"><em style="font-size: medium;">Hint</em><span style="font-size: 16px;">: at first ignore writing the results to a file. Simply collect all material in a string which you print to the screen. Then writing it to a file will be simple. To get both the word and its context you need an
>  indexed loop through the words. Use the stripw() function we saw on individual words to make finding hits more accurate (e.g. the program found 'river.' above). Finally, the join() method will come in handy to reconstruct the context as a string.</span><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Times New Roman'; background-color: transparent; font-style: normal;"><span style="font-size: 16px;"><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Times New Roman'; background-color: transparent; font-style: normal;"><span style="font-size: 16px;">Link to final product: </span><a href="http://imgur.com/q1aAAhp" style="font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12pt;">http://imgur.com/q1aAAhp</a></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Times New Roman'; background-color: transparent; font-style:
>  normal;"><span style="font-size: 16px;"><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Times New Roman'; background-color: transparent; font-style: normal;"><span style="font-size: 16px;">For my program this is what i have so far, I am kinda lost at this point if you can please guide me to help resolve this program.</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'Times New Roman'; background-color: transparent; font-style: normal;"><span style="font-size: 16px;"><br></span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';">def lines(name, word):</span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';">    'print all lines of name in which word occurs'</span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';"><br></span></div><div
>  style="background-color: transparent;"><span style="font-family: 'Times New Roman';">    infile = open(name, 'r')</span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';">    lst = infile.readlines()</span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';">    infile.close()</span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';"><br></span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';">    for i in range(len(lst)):</span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';">        line = lst[i]</span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';">        if wordin(word, line):</span></div><div
>  style="background-color: transparent;"><span style="font-family: 'Times New Roman';">            w = ('Word found in line {}:'.format(i))</span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';">            #x = (lst[i+1])</span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';">            y = lst[i]</span><span style="font-family: 'Times New Roman'; background-color: transparent; font-size: 12pt;">        </span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';">             </span></div><div style="background-color: transparent;"><span style="font-family: 'Times New Roman';"></span></div><div style="background-color: transparent;"><span style="font-family: 'Times New
>  Roman';">            print (''.join(y))</span></div></div></body></html>
>

This is a text mailing list. Please post as text message, not html. 
(That's an option in your email program.) That's a lot of mess to wade
through. And in many email programs, the text portion of an html
message will not quite match the html portion, so we see something
somewhat different than what you typed.

Your phrase "kinda lost" doesn't narrow it down much.  When you run this
code, what happens?  By inspection, the first problem I see is that you
don't actually call the function.  You need a call to lines() at your
top-level, supplying some filename and word name.

Second problem I spot is the call to wordin(), which isn't included.

Third problem is that you're calling join() method and passing it a
string rather than a list.  That's legal, but it doesn't do what you
think.  And once you find a list to pass it, I think you'll find that
the "" string for its object wants to be " " instead.

Fourth is that the function isn't called occurs(), and doesn't do what
the assignment says.

Which part are you stumped at, or are you getting a syntax error or
something else that I'm not guessing.



-- 
DaveA




More information about the Tutor mailing list