-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ethan Fast
authored and
Ethan Fast
committed
Apr 16, 2016
0 parents
commit cf046b7
Showing
11 changed files
with
248 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .core import Empath |
Binary file not shown.
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,40 @@ | ||
import os | ||
import sys | ||
from collections import defaultdict | ||
from . import helpers as util | ||
|
||
class Empath: | ||
def __init__(self, backend_url="http://localhost:8000"): | ||
self.cats = defaultdict(list) | ||
self.invcats = defaultdict(list) | ||
self.base_dir = os.path.dirname(util.__file__) | ||
self.load(self.base_dir+"/data/categories.tsv") | ||
|
||
def load(self,file): | ||
with open(file,"r") as f: | ||
for line in f: | ||
cols = line.strip().split("\t") | ||
name = cols[0] | ||
terms = cols[1:] | ||
for t in terms: | ||
self.cats[name].append(t) | ||
self.invcats[t].append(name) | ||
|
||
def analyze(self,doc,tokenizer="default",normalize=True): | ||
if tokenizer == "default": | ||
tokenizer = util.default_tokenizer | ||
elif tokenizer == "bigrams": | ||
tokenizer = util.bigram_tokenizer | ||
if not hasattr(tokenizer,"__call__"): | ||
raise Exception("invalid tokenizer") | ||
count = {} | ||
tokens = 0.0 | ||
for cat in self.cats.keys(): count[cat] = 0.0 | ||
for tk in tokenizer(doc): | ||
tokens += 1.0 | ||
for cat in self.invcats[tk]: | ||
count[cat]+=1.0 | ||
if normalize: | ||
for cat in count.keys(): | ||
count[cat] = count[cat] / tokens | ||
return count |
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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,9 @@ | ||
def default_tokenizer(doc): | ||
return doc.split() | ||
|
||
def bigram_tokenizer(doc): | ||
tokens = doc.split() | ||
for t1,t2 in zip(tokens,tokens[1:]): | ||
yield t1 | ||
yield t1+"_"+t2 | ||
yield tokens[-1] |
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,4 @@ | ||
from empath import Empath | ||
empath = Empath() | ||
|
||
|