-
Notifications
You must be signed in to change notification settings - Fork 0
Add BoW and DTM creation, Keep Document IDs #4
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6517351
chore: remove unnecessary files
PaulKalho 1832c0a
chore: add files to gitignore
PaulKalho 2dc515c
fix: use absolute path for file downloads
PaulKalho 5a5ff9c
chore: update cbc to match definition
PaulKalho 1da836c
chore: update cbc yaml
PaulKalho 1ab7001
feat: keep doc_ids
PaulKalho 4d4b758
chore: remove minio from tests
PaulKalho 5ae0627
chore: use list native
PaulKalho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,8 @@ | ||
| from typing import List | ||
| from dataclasses import dataclass | ||
|
|
||
|
|
||
| @dataclass | ||
| class PreprocessedDocument: | ||
| doc_id: str | ||
| tokens: List[str] |
This file contains hidden or 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,103 @@ | ||
| from typing import List | ||
| import numpy as np | ||
| from collections import Counter | ||
|
|
||
| from algorithms.models import PreprocessedDocument | ||
|
|
||
|
|
||
| class NLPVectorizer: | ||
| def __init__(self, preprocessed_output: List[PreprocessedDocument]): | ||
| self.documents = preprocessed_output | ||
| self.doc_ids = [doc.doc_id for doc in preprocessed_output] | ||
|
|
||
| # Frequencies | ||
| self.token_frequency = Counter() | ||
| self.token_document_frequency = Counter() | ||
| self.ngram_frequency = Counter() | ||
| self.ngram_document_frequency = Counter() | ||
|
|
||
| # bow + dtm | ||
| self.bag_of_words = [] | ||
| self.vocab = {} | ||
| self.reverse_vocab = [] | ||
| self.dtm = None | ||
|
|
||
| def analyze_frequencies(self): | ||
| for doc in self.documents: | ||
| tokens = [t for t in doc.tokens if " " not in t] | ||
| ngrams = [t for t in doc.tokens if " " in t] | ||
|
|
||
| # token frequencies | ||
| self.token_frequency.update(tokens) | ||
| self.token_document_frequency.update(set(tokens)) | ||
|
|
||
| # ngram frequencies | ||
| self.ngram_frequency.update(ngrams) | ||
| self.ngram_document_frequency.update(set(ngrams)) | ||
|
|
||
| def build_bow(self): | ||
| bow = [] | ||
|
|
||
| for doc in self.documents: | ||
| entries = [] | ||
| unique = set() | ||
|
|
||
| for term in doc.tokens: | ||
| if term in unique: | ||
| continue | ||
| unique.add(term) | ||
|
|
||
| is_ngram = " " in term | ||
|
|
||
| entry = { | ||
| "term": term, | ||
| "type": "ngram" if is_ngram else "word", | ||
| "span": len(term.split(" ")), | ||
| "freq": ( | ||
| self.ngram_frequency[term] | ||
| if is_ngram | ||
| else self.token_frequency[term] | ||
| ), | ||
| "docs": ( | ||
| self.ngram_document_frequency[term] | ||
| if is_ngram | ||
| else self.token_document_frequency[term] | ||
| ), | ||
| "filters": [] | ||
| } | ||
|
|
||
| entries.append(entry) | ||
|
|
||
| bow.append(entries) | ||
|
|
||
| self.bag_of_words = bow | ||
| return bow | ||
|
|
||
| def build_vocabulary(self): | ||
| all_terms = set() | ||
|
|
||
| for doc in self.documents: | ||
| for term in doc.tokens: | ||
| all_terms.add(term) | ||
|
|
||
| sorted_terms = sorted(all_terms) | ||
| self.vocab = {term: i for i, term in enumerate(sorted_terms)} | ||
| self.reverse_vocab = sorted_terms | ||
|
|
||
| return self.vocab | ||
|
|
||
| def build_dtm(self): | ||
| if not self.vocab: | ||
| self.build_vocabulary() | ||
|
|
||
| num_docs = len(self.documents) | ||
| num_terms = len(self.vocab) | ||
|
|
||
| dtm = np.zeros((num_docs, num_terms), dtype=int) | ||
|
|
||
| for i, doc in enumerate(self.documents): | ||
| for term in doc.tokens: | ||
| dtm[i, self.vocab[term]] += 1 | ||
|
|
||
| self.dtm = dtm | ||
| return dtm |
This file contains hidden or 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.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.