By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

How to read the text from file and check for repeated words

P: n/a
Hi guys,

I've got a text file and im trying to read the text from that file and then check every word with 40 words around that word, to make sure the word in question has not been repeated more than once.

In other words, I want to first split the text into words, put them in a list and then check [0] against [1] all the way to [39]. Then I want to check [1] against [40], then check [2] against [41] etc.

Splitting the words is not that hard I think, I just need to split at every space and every dot. What I am not sure how to do is check the words against the other words in the text..
Any ideas guys on how that could be done? =)
Nov 1 '10 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The count function should work. If count > 1 then the word is repeated.

import string
string.count("word you want to check", start, end)

I believe you can use a loop and string indexing to define the start and end.

(But all of this could be utter nonsense, I'm pretty new to python. Sorry if I've led you astray....)
Nov 4 '10 #2

Expert 100+
P: 624
in other words, I want to first split the text into words, put them in a list and then check [0] against [1] all the way to [39]. Then I want to check [1] against [40], then check [2] against [41] etc.
You want to split the text into a list of words (all lower case), sort the list of words, and compare this_word with next_word. An easier way is to convert to a set, as a set does not allow duplicate keys, and then check the length of the set compared to the length of the original list. You can also use a dictionary, with the word as key, pointing to an integer that is used to count how many times the word appears.
Nov 5 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.