[Tutor] Looking for Words - Help

Alan Gauld alan.gauld at btinternet.com
Fri Oct 11 15:23:11 CEST 2013


On 11/10/13 06: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.

That means you need to define a function called
occurs() not lines().

> 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'.

OK, so far

> 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.

That's a good hint so lets just do that.
In fact writing to a file in the occurs function is probably
a bad idea anyway! But that's another debate altogether.

> to a file will be simple. To get both the word and its context you need
> an indexed loop through the words.

That's only partly true. although you need both index and word you don;t 
need to loop on the index. You can use the enumerate() function to get 
both in a single hit.


> Use the stripw() function we saw on individual words to make
 > finding hits more accurate

No idea what that means but since the assignment suggests
it we should assume its correct.

> 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.

You have several bad habits in here...

> def lines(name, word):
>      'print all lines of name in which word occurs'
>
>      infile = open(name, 'r')
>      lst = infile.readlines()
>      infile.close()

You could do that in one line:

        lst = open(name).readlines()

Although if you want context to stretch over line breaks
you would be better using read() to get the whole file
as a single string.

>      for i in range(len(lst)):
>          line = lst[i]

This is where enumerate gives you a better solution:

        for index, line in enumerate(lst):

>          if wordin(word, line):

I assume round about now is where you were supposed
to use the stripw() function mentioned in the hint?

You haven't got the location of the word to work out the
context. So really you need to split the line into words
and then see if your word is in there. You can then get
the location either by using enumerate() and a loop or
using the index() list operation.

>              w = ('Word found in line {}:'.format(i))

You don't need the parens here and I don't think the colon
on the end makes much sense either

>              y = lst[i]
>              print (''.join(y))

y is just the original input line so is not needed
you can just passs lst[i]) to join()..
But joining it with an empty string doesn't do much.

 >>> ''.join('abcde')
'abcde'

In summary I'd go for reading the whole file as a string then use your 
splitw() function to extract the words (assuming that's what it does?)
Then use enumerate() to loop over the list of words to find the words 
and its index.

Then use list slicing to get the 5 words before and after the found 
word. Finally join them together to form a string and append that string 
to a result list that you can return.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list