-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_histogram_of_sentence_length.py
54 lines (43 loc) · 1.56 KB
/
plot_histogram_of_sentence_length.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
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import pyprind
import utils
from main import load_corpus
def count_sentence_length(corpus, count):
for s in pyprind.prog_bar(corpus):
length = len(s)
if length >= len(count):
continue
count[length] += 1
return count
def plot_histogram(count):
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
keys = np.arange(len(count))
ax.hist(keys, weights=count, bins=len(count))
ax.set_title("Sentence Length Distribution")
ax.set_xlabel("Length")
ax.set_ylabel("Frequency")
fig.show()
def main():
config = utils.Config()
path_corpus_train = config.getpath("prep_corpus") + ".train"
path_corpus_val = config.getpath("prep_corpus") + ".val"
corpus_train = load_corpus(
path_corpus_train,
vocab=path_corpus_train + ".vocab",
max_length=1000000000)
corpus_val = load_corpus(path_corpus_val,
vocab=corpus_train.vocab,
max_length=1000000000)
count = np.zeros((101,))
count = count_sentence_length(corpus_train, count=count)
count = count_sentence_length(corpus_val, count=count)
diff = len(corpus_train) + len(corpus_val) - count.sum()
utils.logger.debug("[info] Excluded %d sentences of length longer than %d" % (diff, len(count)-1))
path_out = config.getpath("prep_corpus") + ".histogram.npy"
np.save(path_out, count)
plot_histogram(count)
if __name__ == "__main__":
main()