-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNGrammer.rb
168 lines (142 loc) · 3.38 KB
/
NGrammer.rb
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
require 'csv'
require 'reflekt'
$ENV = {
:reflekt => {
:enabled => true
}
}
class NGrammer
prepend Reflekt
reflekt_skip :display
def initialize(language, wordlist, blocklist = nil, allowlist = nil, encoding = 'UTF-8')
# Set alphabet.
@alphabet = Alphabet.new(language)
# Get wordlists.
@wordlist = wordlist
@blocklist = []
if blocklist
@blocklist = get_blocklist(blocklist)
end
# Set encoding.
@encoding = encoding
# Setup ngrams.
@ngrams = {}
@percentages = {}
end
####
# Getters.
####
def get_ngrams
@ngrams
end
def get_alphabet
@alphabet
end
def get_percentages
@percentages
end
####
# Sort ngrams by count.
####
def sort
@ngrams = @ngrams.sort_by {|_key, value| value}.reverse.to_h
end
def sort_reverse
@ngrams = @ngrams.sort_by {|_key, value| value}.to_h
end
####
# Process words into ngrams.
#
# @param integer $ngram_length: Number of letters in the ngram.
# @param integer $start_position: Where in the word to detect the ngram?
####
def process(ngram_length, start_position = 0)
end_position = start_position + ngram_length - 1
# For each word.
File.foreach(@wordlist, encoding: @encoding) do |line|
line = line.downcase
letter = line[0]
# That's not shorter than asked.
if line.strip.length < ngram_length
next
end
# That has no bad characters.
if (line =~ /[[:alpha:]]/).nil?
next
end
if line.include? "\'"
next
end
# That fits in alphabet.
unless @alphabet.include? letter
next
end
# That's not in blocklist.
if @blocklist.include? line[start_position..end_position]
next
end
# Create ngram.
ngram = line[start_position..end_position]
# Add ngram to its key
if @ngrams.key? ngram
@ngrams[ngram] = @ngrams[ngram] + 1
# Create new ngram if it doesn't exist.
else
@ngrams[ngram] = 1
end
end
create_percentages()
end
####
# Percentage of each ngram in the total.
#
# @param hash $ngrams: The ngrams to find distribution of.
# @return hash @percentages:
####
def create_percentages()
# Get total amount of ngrams.
total_count = 0;
@ngrams.each do |ngram, count|
total_count = total_count + count
end
# Percentage of ngram in total.
@ngrams.each do |ngram, count|
fraction = count.to_f / total_count.to_f
@percentages[ngram] = fraction
end
# Return distribution
@percentages
end
private def get_blocklist(blocklist_path)
# Get blocked words.
blocklist = []
CSV.foreach(blocklist_path) do |row|
blocklist << row[0]
end
# Remove header from blocklist.
blocklist.shift
# Return blocklist.
blocklist
end
####
# Display ngrams via the commandline.
#
# @param amount - The amount of results to show. Leave empty for all results.
####
def display(amount = nil)
# Limit amount of results.
if (amount)
ngrams = @ngrams.first(amount)
else
ngrams = @ngrams
end
# Display ngrams.
ngrams.each do |ngram, count|
percentage = ''
unless @percentages[ngram].nil?
percentage = " (" + (@percentages[ngram] * 100).round(2).to_s + "%)"
end
puts ngram + " (" + count.to_s + ")" + percentage
end
end
end