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

Frequencies tk #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 9 additions & 3 deletions followthemoney_compare/lib/word_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def preprocess_text(text):
class WordFrequency:
def __init__(self, confidence, error_rate, bin_dtype="uint32"):
self.n_items = 0
self.n_documents = 0
self.confidence = confidence
self.error_rate = error_rate

Expand All @@ -46,6 +47,9 @@ def iter_idxs(self, key):
idx = (k1 + i * k2) % self.width
yield (idx % self.width) + (i * self.width)

def add_doc(self):
self.n_documents += 1

def add(self, key):
idxs = list(self.iter_idxs(key))
return self.add_idxs(idxs)
Expand Down Expand Up @@ -131,6 +135,7 @@ def from_proxies(
)
for i, proxy in enumerate(proxies):
collection = proxy.context.get("collection")
tf.add_doc()
for name in proxy.names:
for token in preprocess_text(name):
idxs = list(tf.iter_idxs(token))
Expand All @@ -139,12 +144,12 @@ def from_proxies(
sf[proxy.schema.name].add_idxs(idxs)
if checkpoint_freq and (i + 1) % checkpoint_freq == 0:
idf_merge = WordFrequency.merge(*idf.values(), binarize=True)
freq = cls(tf, idf_merge, sf)
freq = cls(tf, sf, idf_merge)
freq.summarize()
if checkpoint_dir:
freq.save_dir(checkpoint_dir)
idf_merge = WordFrequency.merge(*idf.values(), binarize=True)
freq = cls(tf, idf_merge, sf)
freq = cls(tf, sf, idf_merge)
if checkpoint_dir:
freq.save_dir(checkpoint_dir)
return freq
Expand Down Expand Up @@ -175,7 +180,8 @@ def tfidf(self, token, schema=None):
return self.token_frequency(token, schema) / self.document_frequency(token)

def inv_tfidf(self, token, schema=None):
return self.document_frequency(token) / self.token_frequency(token, schema)
return self.token.n_documents / self.document_frequency(token)
# return self.document_frequency(token) / self.token_frequency(token, schema)

@classmethod
def load_dir(cls, directory):
Expand Down
2 changes: 1 addition & 1 deletion followthemoney_compare/metrics/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def _compare_names_fuzzy_wf(t1, s1, t2, s2, frequencies):
for left, schema, right in ((tp1, s1, tp2), (tp2, s2, tp1)):
right_str = " ".join(right)
for token_left in left:
n = math.log1p(frequencies.inv_tfidf(token_left, schema=schema))
n = math.log(frequencies.inv_tfidf(token_left, schema=schema))
s = partial_token_set_ratio(
token_left, right_str, force_ascii=False, full_process=False
)
Expand Down