-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimize.py
More file actions
43 lines (32 loc) · 1.12 KB
/
minimize.py
File metadata and controls
43 lines (32 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
MIN_FREQUENCY = 10
def load_model(input_path):
with open(input_path, "r", encoding="utf-8") as f:
return json.load(f)
def filter_model(model, min_freq):
return {
context: {
word: freq
for word, freq in predictions.items()
if freq >= min_freq
}
for context, predictions in model.items()
if any(freq >= min_freq for freq in predictions.values())
}
def save_model(output_path, model):
with open(output_path, "w", encoding="utf-8") as f:
json.dump(model, f, ensure_ascii=False, indent=2)
def main():
input_file = "./corpus/en_US.twitter.clean.trigrams.json"
output_file = "./corpus/en_US.clean.trigrams.min.json"
print("Loading model...")
model = load_model(input_file)
print("Filtering model...")
minimized_model = filter_model(model, MIN_FREQUENCY)
print("Saving minimized model...")
save_model(output_file, minimized_model)
print(f"Model reduced from {len(model)} to {len(minimized_model)} contexts.")
if __name__ == "__main__":
main()