-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dickens example, bump version number to 0.1
- Loading branch information
Showing
6 changed files
with
57 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env python | ||
|
||
# Find terms that distinguish various novels by Charles Dickens. | ||
# Note: if the w parameter is set wisely, no stop list is needed. | ||
|
||
from weighwords import ParsimoniousLM | ||
import gzip | ||
import logging | ||
import numpy as np | ||
import re | ||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
top_k = 20 # How many terms per book to retrieve | ||
|
||
books = [ | ||
('Oliver Twist', '730'), | ||
('David Copperfield', '766'), | ||
('Great Expectations', '1400'), | ||
] | ||
|
||
startbook = """*** START OF THIS PROJECT GUTENBERG EBOOK """ | ||
|
||
|
||
def read_book(title, num): | ||
"""Returns generator over words in book num""" | ||
|
||
logger.info("Fetching terms from %s" % title) | ||
path = "%s.txt.utf8.gz" % num | ||
in_book = False | ||
for ln in gzip.open(path): | ||
if in_book: | ||
for w in re.sub(r"[.,:;!?\"']", " ", ln).lower().split(): | ||
yield w | ||
elif ln.startswith(startbook): | ||
in_book = True | ||
|
||
|
||
book_contents = [(title, list(read_book(title, num))) for title, num in books] | ||
|
||
model = ParsimoniousLM([terms for title, terms in book_contents], w=.01) | ||
|
||
for title, terms in book_contents: | ||
print("Top %d words in %s:" % (top_k, title)) | ||
for term, p in model.top(top_k, terms): | ||
print(" %s %.4f" % (term, np.exp(p))) | ||
print("") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters