0

I want to find out the frequency of a specific word from a text file. Suppose in my document i have a line "this is me is is " if i input 'is' the output should 3 if my input is 'me' output should 1. i am trying this code

    import re
    doc1 = re.findall(r'\w+', open('E:\doc1.txt').read().lower())
    words = raw_input("Input Number :: ")
    docmtfrequency1 =  words.count(words)

but it is not giving desired output

2
  • 1
    It should be doc1.count(words). Commented Oct 2, 2015 at 18:32
  • 1
    what is words.count(words) supposed to do? Commented Oct 2, 2015 at 18:36

1 Answer 1

2

collections.Counter() has this covered if I understand your problem. The example from the docs would seem to match your problem.

# Tally occurrences of words in a list
cnt = Counter()
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
    cnt[word] += 1
print cnt


# Find the ten most common words in Hamlet
import re
words = re.findall('\w+', open('hamlet.txt').read().lower())
Counter(words).most_common(10)

From the example above you should be able to do:

import re
import collections
words = re.findall('\w+', open('1976.03.txt').read().lower())
print collections.Counter(words)

naive approach to show one way.

wanted = "fish chips steak"
cnt = Counter()
words = re.findall('\w+', open('1976.03.txt').read().lower())
for word in words:
    if word in wanted:
        cnt[word] += 1
print cnt
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.