forked from lxa2015/lxa-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngrams.py
214 lines (161 loc) · 7.52 KB
/
ngrams.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!usr/bin/env python3
from collections import Counter
import argparse
from pathlib import Path
from lxa5lib import (get_language_corpus_datafolder, stdout_list,
load_config_for_command_line_help, sorted_alphabetized,
changeFilenameSuffix, json_pdump)
#------------------------------------------------------------------------------#
#
# This program creates a trigram file, bigram file, and a word list.
#
# Input is a corpus text.
#
# John Goldsmith and Wang Xiuli 2012
# Jackson Lee 2014-
#
#------------------------------------------------------------------------------#
def makeArgParser(configfilename="config.json"):
language, \
corpus, \
datafolder, \
configtext = load_config_for_command_line_help(configfilename)
parser = argparse.ArgumentParser(
description="This program extracts ngrams from a corpus.\n\n{}"
.format(configtext),
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--config", help="configuration filename",
type=str, default=configfilename)
parser.add_argument("--language", help="Language name",
type=str, default=None)
parser.add_argument("--corpus", help="Corpus file to use",
type=str, default=None)
parser.add_argument("--datafolder", help="path of the data folder",
type=str, default=None)
parser.add_argument("--maxwordtokens", help="maximum number of word tokens;"
" if this is zero, then the program reads "
"all word tokens in the corpus",
type=int, default=0)
return parser
def main(language=None, corpus=None, datafolder=None, filename=None,
maxwordtokens=0):
print("\n*****************************************************\n"
"Running the ngrams.py program now...\n")
if filename:
infilename = Path(filename)
outfolder = Path(infilename.parent, "ngrams")
outfolderDx1 = Path(infilename.parent, "dx1")
corpus = infilename.name
else:
infilename = Path(datafolder, language, corpus)
outfolder = Path(datafolder, language, "ngrams")
outfolderDx1 = Path(datafolder, language, "dx1")
if not outfolder.exists():
outfolder.mkdir(parents=True)
if not outfolderDx1.exists():
outfolderDx1.mkdir(parents=True)
if maxwordtokens:
corpusName = Path(corpus).stem + "_{}-tokens".format(maxwordtokens)
else:
corpusName = Path(corpus).stem
outfilenameWords = Path(outfolder, corpusName + "_words.txt")
outfilenameBigrams = Path(outfolder, corpusName + "_bigrams.txt")
outfilenameTrigrams = Path(outfolder, corpusName + "_trigrams.txt")
outfilenameDx1 = Path(outfolderDx1, corpusName + ".dx1")
wordDict = Counter()
trigramDict = Counter()
bigramDict = Counter()
sep = "\t"
corpusCurrentSize = 0 # running word token count
print('Reading the corpus file now...')
with infilename.open() as f:
for line in f.readlines():
if not line:
continue
line = line.strip().casefold()
# TODO: modify/combine these with "scrubbing", cf. Alchemist and Lxa4
line = line.replace(".", " . ")
line = line.replace(",", " , ")
line = line.replace(";", " ; ")
line = line.replace("!", " ! ")
line = line.replace("?", " ? ")
line = line.replace(":", " : ")
line = line.replace(")", " ) ")
line = line.replace("(", " ( ")
words = line.split()
lenWords = len(words)
corpusCurrentSize += lenWords
for i in range(lenWords-2):
word1 = words[i]
word2 = words[i+1]
word3 = words[i+2]
wordDict[word3] += 1
if i == 0:
wordDict[word1] += 1
wordDict[word2] += 1
bigram = word1 + sep + word2
bigramDict[bigram] += 1
bigram = word2 + sep + word3
trigram = word1 + sep + word2 + sep + word3
trigramDict[trigram] += 1
bigramDict[bigram] += 1
if maxwordtokens and corpusCurrentSize > maxwordtokens:
break
print("\nCompleted counting words, bigrams, and trigrams.")
print("Token count: {}".format(corpusCurrentSize))
intro_string = "# data source: {}\n# token count: {}".format(str(infilename),
corpusCurrentSize)
# wordsSorted = sorted(wordDict.items(),
# key=lambda x: x[1], reverse=True)
wordsSorted = sorted_alphabetized(wordDict.items(),
key=lambda x: x[1], reverse=True)
bigramsSorted = sorted_alphabetized(bigramDict.items(),
key=lambda x: x[1], reverse=True)
trigramsSorted = sorted_alphabetized(trigramDict.items(),
key=lambda x: x[1], reverse=True)
# print txt outputs
with outfilenameWords.open('w') as f:
print(intro_string, file=f)
print("# type count: {}".format(len(wordsSorted)), file=f)
for (word, freq) in wordsSorted:
print(word + sep + str(freq), file=f)
with outfilenameBigrams.open('w') as f:
print(intro_string, file=f)
print("# type count: {}".format(len(bigramsSorted)), file=f)
for (bigram, freq) in bigramsSorted:
print(bigram + sep + str(freq), file=f)
with outfilenameTrigrams.open('w') as f:
print(intro_string, file=f)
print("# type count: {}".format(len(trigramsSorted)), file=f)
for (trigram, freq) in trigramsSorted:
print(trigram + sep + str(freq), file=f)
# print dx1 output
with outfilenameDx1.open('w') as f:
for (word, freq) in wordsSorted:
print(word, freq, ' '.join(word), file=f)
# print json outputs
with changeFilenameSuffix(outfilenameWords, ".json").open('w') as f:
json_pdump(dict(wordsSorted), f)
with changeFilenameSuffix(outfilenameBigrams, ".json").open('w') as f:
json_pdump(dict(bigramsSorted), f)
with changeFilenameSuffix(outfilenameTrigrams, ".json").open('w') as f:
json_pdump(dict(trigramsSorted), f)
print('wordlist, bigram and trigram files ready')
print('dx1 file ready')
stdout_list("Output files:", outfilenameWords,
outfilenameBigrams, outfilenameTrigrams, outfilenameDx1,
changeFilenameSuffix(outfilenameWords, ".json"),
changeFilenameSuffix(outfilenameBigrams, ".json"),
changeFilenameSuffix(outfilenameTrigrams, ".json"))
if __name__ == "__main__":
args = makeArgParser().parse_args()
maxwordtokens = args.maxwordtokens
description="You are running {}.\n".format(__file__) + \
"This program extracts word n-grams.\n" + \
"maxwordtokens = {} (zero means all word tokens)".format(maxwordtokens)
language, corpus, datafolder = get_language_corpus_datafolder(args.language,
args.corpus, args.datafolder, args.config,
description=description,
scriptname=__file__)
main(language=language, corpus=corpus, datafolder=datafolder,
maxwordtokens=maxwordtokens)