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

Adding another possibility to provide the num_words in order to compute this dynamically #545

Merged
Merged
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions lm/vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class VocabularyFromTextJob(Job):
Extract vocabulary from given text files based on frequency.
"""

def __init__(self, file_paths: List[tk.Path], num_words: int = 1_000_000):
def __init__(self, file_paths: List[tk.Path], num_words: Union[int, tk.Variable] = 1_000_000):
"""
:param file_paths: paths to the text files
:param num_words: expected size of the vocabulary
Expand All @@ -185,12 +185,14 @@ def run(self):
words = line.strip().split()
counter.update(words)

cutoff = min(self.num_words, len(counter))
num_words = self.num_words if isinstance(self.num_words, int) else self.num_words.get()
JackTemaki marked this conversation as resolved.
Show resolved Hide resolved

cutoff = min(num_words, len(counter))

with open(self.out_vocabulary, "w") as vocabulary, open(
self.out_vocabulary_with_counts, "w"
) as vocabulary_with_counts:
for (word, count) in counter.most_common(cutoff):
for word, count in counter.most_common(cutoff):
vocabulary.write(f"{word}\n")
vocabulary_with_counts.write(f"{word} {count}\n")

Expand Down
Loading