forked from github-linguist/linguist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.rb
412 lines (368 loc) · 11.5 KB
/
classifier.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
require 'linguist/tokenizer'
require 'set'
module Linguist
# Language content classifier.
class Classifier
# Maximum number of bytes to consider for classification.
# This is only used at evaluation time. During training, full content of
# samples is used.
CLASSIFIER_CONSIDER_BYTES = 50 * 1024
# Public: Use the classifier to detect language of the blob.
#
# blob - An object that quacks like a blob.
# possible_languages - Array of Language objects
#
# Examples
#
# Classifier.call(FileBlob.new("path/to/file"), [
# Language["Ruby"], Language["Python"]
# ])
#
# Returns an Array of Language objects, most probable first.
def self.call(blob, possible_languages)
language_names = possible_languages.map(&:name)
classify(Samples.cache, blob.data[0...CLASSIFIER_CONSIDER_BYTES], language_names).map do |name, _|
Language[name] # Return the actual Language objects
end
end
# Public: Train classifier that data is a certain language.
#
# db - Hash classifier database object
# language - String language of data
# data - String contents of file or array of tokens.
#
# Examples
#
# Classifier.train!(db, 'Ruby', "def hello; end")
#
# Returns nil.
#
# Set LINGUIST_DEBUG=1, =2 or =3 to print internal statistics.
def self.train!(db, language, data)
tokens = data
tokens = Tokenizer.tokenize(tokens) if tokens.is_a?(String)
db['vocabulary'] ||= {}
# Set hash to autoincremented index value
if db['vocabulary'].default_proc.nil?
db['vocabulary'].default_proc = proc do |hash, key|
hash[key] = hash.length
end
end
db['samples'] ||= {}
db['samples'][language] ||= []
termfreq = to_vocabulary_index_termfreq(db['vocabulary'], tokens)
db['samples'][language] << termfreq
nil
end
# Public: Finalize training.
#
# db - Hash classifier database object
#
# Examples:
# Classifier.finalize_train!(db)
#
# Returns nil.
#
# This method must be called after the last #train! call.
def self.finalize_train!(db)
db['vocabulary'] ||= {}
# Unset hash autoincrement
db['vocabulary'].default_proc = nil
db['samples'] ||= []
filter_vocab_by_freq! db, MIN_DOCUMENT_FREQUENCY
sort_vocab! db
db['icf'] = inverse_class_freqs db
normalize_samples! db
db['centroids'] = get_centroids db
db.delete 'samples'
nil
end
# Public: Guess language of data.
#
# db - Hash of classifier tokens database.
# data - Array of tokens or String data to analyze.
# languages - Array of language name Strings to restrict to.
#
# Examples
#
# Classifier.classify(db, "def hello; end")
# # => [ 'Ruby', 0.90], ['Python', 0.2], ... ]
#
# Returns sorted Array of result pairs. Each pair contains the
# String language name and a Float score between 0.0 and 1.0.
def self.classify(db, tokens, languages = nil)
languages ||= db['centroids'].keys
new(db).classify(tokens, languages)
end
# Internal: Initialize a Classifier.
def initialize(db = {})
@vocabulary = db['vocabulary']
@centroids = db['centroids']
@icf = db['icf']
end
# Internal: Guess language of data
#
# data - Array of tokens or String data to analyze.
# languages - Array of language name Strings to restrict to.
#
# Returns sorted Array of result pairs. Each pair contains the
# String language name and a Float score between 0.0 and 1.0.
def classify(tokens, languages)
return [] if tokens.nil? || languages.empty?
tokens = Tokenizer.tokenize(tokens) if tokens.is_a?(String)
debug_dump_tokens(tokens) if verbosity >= 3
vec = Classifier.to_vocabulary_index_termfreq_gaps(@vocabulary, tokens)
vec.each do |idx, freq|
tf = 1.0 + Math.log(freq)
vec[idx] = tf * @icf[idx]
end
return [] if vec.empty?
Classifier.l2_normalize!(vec)
scores = {}
languages.each do |language|
centroid = @centroids[language]
score = Classifier.similarity(vec, centroid)
if score > 0.0
scores[language] = score
end
end
scores = scores.sort_by { |x| -x[1] }
debug_dump_all_tokens(tokens, scores) if verbosity >= 2
debug_dump_scores(scores) if verbosity >= 1
scores
end
private
MIN_DOCUMENT_FREQUENCY = 2
def verbosity
@verbosity ||= (ENV['LINGUIST_DEBUG'] || 0).to_i
end
def debug_dump_scores(scores)
headers = ["Language", "Score"]
rows = scores.map { |l, s| [l, "%.3f" % s] }
dump_table(headers, rows)
end
def debug_dump_tokens(tokens)
counts = Hash.new(0)
tokens.each do |tok|
idx = @vocabulary[tok]
if not idx.nil?
counts[tok] += 1
end
end
norm = Classifier.l2_norm(counts)
rows = counts.map do |tok, tf|
idx = @vocabulary[tok]
log_tf = 1.0 + Math.log(tf)
with_icf = log_tf * @icf[idx]
normalized = with_icf / norm
row = [tok, tf, "%.3f" % log_tf, "%.3f" % with_icf, "%.3f" % normalized]
[normalized, row]
end
headers = ["Token", "TF", "log", "*ICF", "L2"]
rows = rows.sort_by { |x| -x[0] }.map { |_, row| row }
dump_table(headers, rows)
end
# Internal: show a table of probabilities for each <token,language> pair.
#
# The number in each table entry is the number of "points" that each
# token contributes toward the belief that the file under test is a
# particular language. Points are additive.
def debug_dump_all_tokens(tokens, scores)
languages = scores.map { |l, _| l }
counts = Hash.new(0)
tokens.each do |tok|
idx = @vocabulary[tok]
if not idx.nil?
counts[tok] += 1
end
end
data = {}
norm = Classifier.l2_norm(counts)
languages.each do |language|
data[language] = {}
counts.each do |tok, tf|
idx = @vocabulary[tok]
log_tf = 1.0 + Math.log(tf)
with_icf = log_tf * @icf[idx]
normalized = with_icf / norm
data[language][tok] = normalized * @centroids[language][idx].to_f
end
end
norm = Classifier.l2_norm(counts)
rows = counts.map do |tok, tf|
idx = @vocabulary[tok]
log_tf = 1.0 + Math.log(tf)
with_icf = log_tf * @icf[idx]
normalized = with_icf / norm
scores = languages.map do |l, _|
[l, data[l][tok].to_f]
end
max_score = scores.to_h.values.max
row = [tok] + scores.map do |l, s|
if s == max_score
"%.4f*" % s
elsif s > 0.0
"%.4f" % s
else
"-"
end
end
[normalized, row]
end
headers = ["Token"] + (0..languages.length-1).map { |lidx| "[#{lidx}]" }
rows = rows.sort_by { |x| -x[0] }.map { |_, row| row }
legend = languages.each_with_index.map { |l, lidx| "[#{lidx}] = #{l}" }
dump_table(headers, rows, legend)
end
def dump_table(header, rows, legend = nil)
n_cols = header.length
rows = rows.map { |r| r.map { |c| c.to_s } }
col_widths = (0..n_cols - 1).map do |j|
([header[j].length] + rows.map { |row| row[j].length }).max
end
sep_line = "| #{(0..n_cols-1).map { |j| "-" * col_widths[j] }.join(" | ")} |"
content_width = sep_line.length - 4
top_line = "| #{"-" * content_width} |"
format_row = Proc.new do |row|
cells = row.zip(col_widths).map do |cell, width|
"%-#{width}s" % cell
end
"| %s |" % cells.join(" | ")
end
puts top_line
puts format_row.call(header)
puts sep_line
rows.each do |row|
puts format_row.call(row)
end
puts top_line
if legend
legend.each do |line|
puts "| %-#{content_width}s |" % line
end
puts top_line
end
end
def self.to_vocabulary_index_termfreq(vocab, tokens)
counts = Hash.new(0)
tokens.each do |key|
idx = vocab[key]
counts[idx] += 1
end
counts
end
def self.to_vocabulary_index_termfreq_gaps(vocab, tokens)
counts = Hash.new(0)
tokens.each do |key|
if vocab.key? key
idx = vocab[key]
counts[idx] += 1
end
end
counts
end
def self.l2_norm(vec)
norm = vec.values.inject(0.0) { |sum, x| sum + x**2 }
Math.sqrt(norm)
end
def self.l2_normalize!(vec)
norm = l2_norm(vec)
vec.transform_values! do |value|
value.to_f / norm
end
nil
end
def self.similarity(a, b)
sum = 0.0
a.each_key do |idx|
if b.key? idx
sum += a[idx] * b[idx]
end
end
sum
end
# Filter vocabulary by minimum document frequency.
def self.filter_vocab_by_freq!(db, min_freq)
vocabulary = db['vocabulary']
# Get document frequencies
docfreq = Array.new(vocabulary.size, 0)
db['samples'].each_value do |samples|
samples.each do |sample|
sample.each_key do |idx|
docfreq[idx] += 1
end
end
end
vocabulary.select! do |_, idx|
docfreq[idx] >= min_freq
end
nil
end
# Sort vocabulary lexicographically.
def self.sort_vocab!(db)
new_indices = Hash.new { |h,k| h[k] = h.length }
db['vocabulary'].sort_by { |x| x[0] }.each do |term, idx|
db['vocabulary'][term] = new_indices[idx]
end
new_indices.default_proc = nil
db['samples'].transform_values! do |samples|
samples.map do |sample|
new_sample = {}
sample.each do |idx, freq|
new_idx = new_indices[idx]
if not new_idx.nil?
new_sample[new_idx] = freq
end
end
new_sample
end
end
end
# Compute inverse class frequency (ICF) for every term.
def self.inverse_class_freqs(db)
icf = Array.new(db['vocabulary'].size, 0)
db['samples'].each_value do |samples|
terms = Set.new
samples.each do |sample|
terms |= sample.keys
end
terms.each do |idx|
icf[idx] += 1
end
end
icf.map! do |val|
Math.log(db['samples'].size.to_f / val.to_f) + 1
end
icf
end
def self.normalize_samples!(db)
icf = db['icf']
db['samples'].each_value do |samples|
samples.each do |sample|
sample.each do |idx, freq|
tf = 1.0 + Math.log(freq)
sample[idx] = tf * icf[idx]
end
l2_normalize! sample
end
end
end
def self.get_centroids(db)
centroids = {}
db['samples'].each do |language, samples|
centroid = Hash.new(0.0)
samples.each do |sample|
sample.each do |idx, val|
centroid[idx] += val
end
end
centroid.each_key do |idx|
centroid[idx] = centroid[idx] / samples.length
end
l2_normalize! centroid
centroids[language] = centroid
end
centroids
end
end
end