Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hocr-wordfreq: word frequency counter #93

Merged
merged 1 commit into from
Jan 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions hocr-wordfreq
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/python

import sys
import re
import argparse
from lxml import html

parser = argparse.ArgumentParser()
parser.add_argument('-i', '--case-insensitive', action='store_false',
default=True, help="Ignore case")
parser.add_argument('-n', '--max', type=int, default=10, help="Number of hits")
parser.add_argument('hocr_in', help="HOCR file to count frequency for")
args = parser.parse_args()

doc = html.parse(args.hocr_in)
text = doc.find('//body').text_content().strip()
if args.case_insensitive:
text = text.lower()
wc = {}
for word in re.split('\W+', text):
if word == '': continue
wc[word] = wc[word]+1 if word in wc else 1

for idx, word in enumerate(sorted(wc, reverse=True, key=wc.get)):
if idx > args.max: break
print("%-5d\t%s"%(wc[word], word))