-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_wordlist.py
75 lines (58 loc) · 1.98 KB
/
make_wordlist.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python3
""" Given a json file with transcript information this tools can perform
manipulations including generating word lists.
Optionally provide the output json file name with -j
Usage: python filter_text.py sample.json wordlist.txt
"""
import argparse
import json
import string
import os
import re
import sys
def save_wordlist(wordlist):
""" Given a list of strings write to file
"""
try:
for word in wordlist: sys.stdout.write(word + '\n')
except:
print("Could not write out to file " + filename)
exit()
def extract_wordlist(data):
""" Given the data object produce a list of strings of single words
Returned list is of unique words and sorted
"""
result = []
for utt in data:
words = utt.get('transcript').split()
result.extend(words)
result = list(set(result))
result.sort()
return result
def load_file(filename=""):
""" Given a filename load and return the object
"""
try:
f = sys.stdin
if filename:
f = open(filename, "r", encoding = "utf-8")
data = json.load(f)
except Exception as e:
print("Could not read file " + filename)
exit()
return data
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--infile", type=str,
help="The input file to clean.")
args = parser.parse_args()
if args.infile: data = load_file(args.infile)
else: data = load_file()
print("Wordlist...", end='', flush=True,file=sys.stderr)
wordlist = extract_wordlist(data)
print("Done.",file=sys.stderr)
print("Write out wordlist...", end='', flush=True,file=sys.stderr)
save_wordlist(wordlist)
print("Done.",file=sys.stderr)
if __name__ == '__main__':
main()