Python : word frequency in a file -
i've created simple word count program in python reads text file, counts word frequency , writes result file. problem if want search "windows" , text file contain word "xwindows" count also.
import sys import glob import errno files = glob.glob('w.asm') the_count =['windows'] name in files: open(name) f: print "occurences in file -- %s " % name contents = f.read() print contents number in the_count: print "windows occured-", contents.count(number)
w.asm file contains
windows iwindows qwindows hwindows kwindows windows windows windowsh wwindows windows iwindows qwindows hwindows kwindows
output
occurences in file -- w.asm windows iwindows qwindows hwindows kwindows windows windows windowsh wwindows windows iwindows qwindows hwindows kwindows windows occured- 14
so actual output want 4 because windows occurred 4 times code giving 14....
so please help
14 correct, since windowsh
, such contain substring winows
. simple fix first split file words, call count()
:
for name in files: open(name) f: print "occurences in file -- %s " % name contents = f.read().split() # <--- split print contents number in the_count: print "windows occured-", contents.count(number)
Comments
Post a Comment