Blame view

bin/tokenizer.rb 15.5 KB
362b552ee   Rouvier Mickael   upload system
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
  #--
  # DO NOT MODIFY!!!!
  # This file is automatically generated by rex 1.0.5
  # from lexical definition file "bin/tokenizer.rex".
  #++
  
  require 'racc/parser'
  
  class TokenizerTwitter < Racc::Parser
    require 'strscan'
  
    class ScanError < StandardError ; end
  
    attr_reader   :lineno
    attr_reader   :filename
    attr_accessor :state
  
    def scan_setup(str)
      @ss = StringScanner.new(str)
      @lineno =  1
      @state  = nil
    end
  
    def action
      yield
    end
  
    def scan_str(str)
      scan_setup(str)
      do_parse
    end
    alias :scan :scan_str
  
    def load_file( filename )
      @filename = filename
      open(filename, "r") do |f|
        scan_setup(f.read)
      end
    end
  
    def scan_file( filename )
      load_file(filename)
      do_parse
    end
  
  
    def next_token
      return if @ss.eos?
      
      # skips empty actions
      until token = _next_token or @ss.eos?; end
      token
    end
  
    def _next_token
      text = @ss.peek(1)
      @lineno  +=  1  if text == "
  "
      token = case @state
      when nil
        case
        when (text = @ss.scan(/&amp;/))
           action { ["wtoken", "&"] }
  
        when (text = @ss.scan(/((isn|aren|wasn|weren|haven|hasn|hadn|won|wouldn|don|doesn|didn|can|couldn|shouldn|mightn|mustn)'t)/))
           action { ["wtoken_negation", text] }
  
        when (text = @ss.scan(/((([a-zA-Z][a-zA-Z+\-.]*)?:?([\/][\/])?)?((([a-zA-Z0-9]([:\/?\#\[\]@!%\-$&'()*+,;=_~.a-zA-Z0-9])*[a-zA-Z0-9])[.]([a-zA-Z0-9]([:\/?\#\[\]@!%\-$&'()*+,;=_~.a-zA-Z0-9])*[a-zA-Z0-9])))|(([a-zA-Z0-9]([:\/?\#\[\]@!%\-$&'()*+,;=_~.a-zA-Z0-9])*[a-zA-Z0-9])[.]([a-zA-Z0-9]([:\/?\#\[\]@!%\-$&'()*+,;=_~.a-zA-Z0-9])*[a-zA-Z0-9])))/))
           action {["url", "#URL"] }
  
        when (text = @ss.scan(/(<(3)+)/))
           action { ["heart", text] }
  
        when (text = @ss.scan(/(([<>]?[:;=8][\-o\*\']?[\)\]\(\[dDpP\/\:\}\{@\|\\]{1,3})|([\)\]\(\[dDpP\/\:\}\{@\|\\][\-o\*\']?[:;=8][<>]?))/))
           action { ["smiley", text] }
  
        when (text = @ss.scan(/((°|[\^\+\.\-\;\*=])([_~\-\-\^\.]){0,3}(°|[\^\+\.\-\;\*=]))|([\^=]?[\^\(=]((°|[\^\+\.\-\;\*=])([_~\-\-\^\.]){0,3}(°|[\^\+\.\-\;\*=]))[=\)\^][=\^]?)/))
           action { ["smiley", text] }
  
        when (text = @ss.scan(/(\@([a-zâàãäáæéèêëîïíìòœôöóûùüúçñÿA-ZÂÀÃÄÁÆÉÈÊËÎÏÍÌÒŒÔÖÓÛÙÜÚÇÑŸ0-9]|_)+)/))
           action { ["twitter_username", "#TWITTERUSERNAME"] }
  
        when (text = @ss.scan(/(\#([a-zâàãäáæéèêëîïíìòœôöóûùüúçñÿA-ZÂÀÃÄÁÆÉÈÊËÎÏÍÌÒŒÔÖÓÛÙÜÚÇÑŸ0-9]|'|_|-)+)/))
           action { ["twitter_hashtag", text] }
  
        when (text = @ss.scan(/['’´]/))
           action { ["apos", text] }
  
        when (text = @ss.scan(/[Nn]( )?(º|°)/))
           action { ["wtoken", text] }
  
        when (text = @ss.scan(/([0-9]+|(I|V|X|C|L|M|D)+)(st|nd|rd|th)/))
           action { ["ordinal", text] }
  
        when (text = @ss.scan(/[012]?[0-9][hH][012345][0-9]/))
           action { ["timex-time", text] }
  
        when (text = @ss.scan(/([0-9]+([Hh]|[Mm](i?n)?|[Ss](ec)?))+/))
           action { ["timex-chrono", text] }
  
        when (text = @ss.scan(/((I|V|X|C|L|M|D)(I|V|X|C|L|M|D)+)/))
           action { ["romain", text] }
  
        when (text = @ss.scan(/[0-9]+([',.\/ ][0-9]+)*/))
           action { ["cardinal", "#CARDINAL"] }
  
        when (text = @ss.scan(/([a-zâàãäáæéèêëîïíìòœôöóûùüúçñÿA-ZÂÀÃÄÁÆÉÈÊËÎÏÍÌÒŒÔÖÓÛÙÜÚÇÑŸ]([.][a-zâàãäáæéèêëîïíìòœôöóûùüúçñÿA-ZÂÀÃÄÁÆÉÈÊËÎÏÍÌÒŒÔÖÓÛÙÜÚÇÑŸ])+([.])?)/))
           action {["acronym", text] }
  
        when (text = @ss.scan(/[a-zâàãäáæéèêëîïíìòœôöóûùüúçñÿA-ZÂÀÃÄÁÆÉÈÊËÎÏÍÌÒŒÔÖÓÛÙÜÚÇÑŸ0-9][a-zâàãäáæéèêëîïíìòœôöóûùüúçñÿA-ZÂÀÃÄÁÆÉÈÊËÎÏÍÌÒŒÔÖÓÛÙÜÚÇÑŸ0-9]*/))
           action { ["wtoken", text] }
  
        when (text = @ss.scan(/(<[\/]?[^>]+>)/))
           action {["sgmltag", text] }
  
        when (text = @ss.scan(/([,;:"'”“(){}&]|«|»|’|—|--|…|‘)/))
           action {["ponctw", text] }
  
        when (text = @ss.scan(/[A-ZÂÀÃÄÁÆÉÈÊËÎÏÍÌÒŒÔÖÓÛÙÜÚÇÑŸ][h]?[.]/))
           action {["initial", text] }
  
        when (text = @ss.scan(/(­|-)/))
           action {["hyphen", text] }
  
        when (text = @ss.scan(/([!?]+)|([.!?¿¡…])/))
           action {["poncts", text] }
  
        when (text = @ss.scan(/([\#$%&*+\/<=>@\[\\\]^_`|~]|–|¶|£|¥|₤|€|§|²|³|º|°|µ)/))
           action {["symbol", text] }
  
        when (text = @ss.scan(/(([A-Za-z0-9\-\._])+[@]([A-Za-z0-9\-\._])+([\.][A-Za-z0-9]+))/))
           action {["email", text] }
  
        when (text = @ss.scan(/[\t
  \v\f\r ]+/))
           action {["space", text] }
  
        when (text = @ss.scan(/./))
           action { ["no_match", text] }
  
        else
          text = @ss.string[@ss.pos .. -1]
          raise  ScanError, "can not match: '" + text + "'"
        end  # if
  
      else
        raise  ScanError, "undefined state: '" + state.to_s + "'"
      end  # case state
      token
    end  # def _next_token
  
  def tokenize(code)
      scan_setup(code)
      tokens = []
      while token = next_token
          tokens.push( token )
      end
      tokens
  end # class
  
  end
  
  
  def _accent(str)
  
      temp_word = str
  
      temp_word = temp_word.gsub("&Agrave;", "à")
      temp_word = temp_word.gsub("&agrave;", "à")
      temp_word = temp_word.gsub("&Acirc;", "â")
      temp_word = temp_word.gsub("&acirc;", "â")
      temp_word = temp_word.gsub("&Ccedil;", "ç")
      temp_word = temp_word.gsub("&ccedil;", "ç")
      temp_word = temp_word.gsub("&Egrave;", "è")
      temp_word = temp_word.gsub("&egrave;", "è")
      temp_word = temp_word.gsub("&Eacute;", "é")
      temp_word = temp_word.gsub("&eacute;", "é")
      temp_word = temp_word.gsub("&Ecirc;", "ê")
      temp_word = temp_word.gsub("&ecirc;", "ê")
      temp_word = temp_word.gsub("&Euml;", "ë")
      temp_word = temp_word.gsub("&euml;", "ë")
      temp_word = temp_word.gsub("&Icirc;", "î")
      temp_word = temp_word.gsub("&icirc;", "î")
      temp_word = temp_word.gsub("&Iuml;", "ï")
      temp_word = temp_word.gsub("&iuml;", "ï")
      temp_word = temp_word.gsub("&Ocirc;", "ô")
      temp_word = temp_word.gsub("&ocirc;", "ô")
      temp_word = temp_word.gsub("&OElig;", "œ")
      temp_word = temp_word.gsub("&oelig;", "œ")
      temp_word = temp_word.gsub("&Ugrave;", "ù")
      temp_word = temp_word.gsub("&ugrave;", "ù")
      temp_word = temp_word.gsub("&Ucirc;", "û")
      temp_word = temp_word.gsub("&ucirc;", "û")
      temp_word = temp_word.gsub("&Uuml;", "ü")
      temp_word = temp_word.gsub("&uuml;", "ü")
      temp_word = temp_word.gsub("&#376;", "ÿ")
      temp_word = temp_word.gsub("&yuml;", "ÿ")
  
      temp_word = temp_word.gsub("À", "à")
      temp_word = temp_word.gsub("Â", "â")
      temp_word = temp_word.gsub("Ç", "ç")
      temp_word = temp_word.gsub("È", "è")
      temp_word = temp_word.gsub("É", "é")
      temp_word = temp_word.gsub("Ê", "ê")
      temp_word = temp_word.gsub("Ë", "ë")
      temp_word = temp_word.gsub("Î", "î")
      temp_word = temp_word.gsub("Ï", "ï")
      temp_word = temp_word.gsub("Ô", "î")
      temp_word = temp_word.gsub("Œ", "œ")
      temp_word = temp_word.gsub("Ù", "ù")
      temp_word = temp_word.gsub("Û", "û")
      temp_word = temp_word.gsub("Ü", "ü")
      temp_word = temp_word.gsub("Ü", "ü")
  
      temp_word = temp_word.gsub("Ÿ", "ÿ")
      temp_word = temp_word.gsub("Ÿ", "ÿ")
  
      #Spanish
      temp_word = temp_word.gsub("Ñ", "ñ")
      temp_word = temp_word.gsub("Á", "á")
      temp_word = temp_word.gsub("Ó", "ó")
      temp_word = temp_word.gsub("Ú", "ú")
      temp_word = temp_word.gsub("Í", "í")
  
      return temp_word
  
  end
  
  
  def _html(str)
      temp_word = str
  
      temp_word = temp_word.gsub("&gt;", "<")
      temp_word = temp_word.gsub("&lt;", ">")
  
      return temp_word
  end
  
  def squeezeWhitespace(str)
      return str.gsub(/[\s\u0020\u00a0\u1680\u180e\u202f\u205f\u3000\u2000-\u200a]+/, " ")
  end
  
  
  def _downcase(str)
      return str.downcase
  end
  
  def _cleanword(string)
      return string.scan(/((.)\2*)/).map(&:first).map { |x| x[0..2] }.join("")
  end
  
  def _convert(string)
      temp_word = string
  
      temp_word = temp_word.gsub("€", "€")
      temp_word = temp_word.gsub("â", "â")
      temp_word = temp_word.gsub("é", "é")
      temp_word = temp_word.gsub("è", "è")
      temp_word = temp_word.gsub("ê", "ê")
      temp_word = temp_word.gsub("ë", "ë")
      temp_word = temp_word.gsub("î", "î")
      temp_word = temp_word.gsub("ï", "ï")
      temp_word = temp_word.gsub("ö", "ö")
      temp_word = temp_word.gsub("ù", "ù")
      temp_word = temp_word.gsub("û", "û")
      temp_word = temp_word.gsub("ü", "ü")
      temp_word = temp_word.gsub("ç", "ç")
      temp_word = temp_word.gsub("Å", "œ")
      temp_word = temp_word.gsub("°", "°")
      temp_word = temp_word.gsub("Ã ", "à ")
      temp_word = temp_word.gsub("Ã", "ô")
  
      return temp_word
  end
  
  def all_caps(str)
      evaluator = TokenizerTwitter.new
      ar = evaluator.tokenize( _html( _accent( squeezeWhitespace( _cleanword( _convert( str ) ) )) ) )
  
      counter = 0
  
      ar.each do |x|
          if x[0] == "wtoken"
              if x[1].match(/^([A-Z]+)$/) 
                  if x[1].upcase == x[1]
                      counter += 1
                  end
              end
          end
      end
  
      return counter
  end
  
  def elongated_words(str)
      evaluator = TokenizerTwitter.new
  
      ar = evaluator.tokenize( _html( _accent( squeezeWhitespace( _convert( str ) ) ) )  )
  
      counter = 0
  
      ar.each do |x|
          if x[1] != _cleanword(x[1])
              counter += 1
          end
      end
  
      return counter
  end
  
  def last_tokens_emoticons?(str)
      evaluator = TokenizerTwitter.new
      ar = evaluator.tokenize( _html( _accent( squeezeWhitespace(_downcase(  _cleanword( _convert( str ) ) )) ) ) )
      return +1 if ar.last[0] == "smiley"
      return -1
  end
  
  def emoticons(str)
      evaluator = TokenizerTwitter.new
      ar = evaluator.tokenize( _html( _accent( squeezeWhitespace(_downcase(  _cleanword( _convert( str ) ) )) ) ) )
      counter = 0
      ar.each do |x|
          if x[0] == "smiley"
              counter += 1
          end
      end
      return counter
  end
  
  def last_tokens_url?(str)
      evaluator = TokenizerTwitter.new
      ar = evaluator.tokenize( _html( _accent( squeezeWhitespace(_downcase(  _cleanword( _convert( str ) ) )) ) ) )
      return +1 if ar.last[0] == "url"
      return -1
  end
  
  def ponctuation(str)
      ponct = Array.new(3, 0)
      evaluator = TokenizerTwitter.new
      ar = evaluator.tokenize( _html( _accent( squeezeWhitespace(_downcase(  _cleanword( _convert( str ) ) )) ) ) )
      ar.each do |x|
          if x[1].match(/^(\?\?+)$/)
              ponct[0] += 1
          end
  
          if x[1].match(/^(!!+)$/)
              ponct[1] += 1
          end
  
          if x[1].include?("?!") == true
              ponct[2] += 1
          elsif x[1].include?("!?") == true
              ponct[2] += 1
          else
              #nothing
          end
  
      end
  
      return ponct
  end
  
  
  def semi_tokenize(str)
      y = Array.new
      evaluator = TokenizerTwitter.new
      ar = evaluator.tokenize( _html( _accent( squeezeWhitespace(_downcase(  _cleanword( _convert( str ) ) )) ) ) )
  
      ar.each do |x|
          y.push(x[1])
      end
  
      return y.join(" ")
  end
  
  def tokenize_charliehebdo(str)
      hash_tags = Array.new
      y = Array.new
      evaluator = TokenizerTwitter.new
      ar = evaluator.tokenize( _html( _accent( squeezeWhitespace( _downcase(  _cleanword( _convert( str ) ) ) ) ) ) )
  
      ar.each do |x|
          if x[0] != "ponctw"
              if x[0] != "space"
                  if x[0] != "sgmltag"
                      if x[0] != "hyphen"
                          if x[0] != "poncts"
                              if x[0] != "apos"
                                  if x[0] != "symbol"
                                      #x[1] = "pour" if x[1] == "pr"
                                      #x[1] = "putain" if x[1] == "ptn"
                                      #x[1] = "maintenant" if x[1] == "mnt"
                                      #x[1] = "beaucoup" if x[1] == "bcp"
  
  
                                  x[1] = "isn't" if x[1] == "isnt"
                                  x[1] = "aren't" if x[1] == "arent"
                                  x[1] = "wasn't" if x[1] == "wasnt"
                                  x[1] = "weren't" if x[1] == "werent"
                                  x[1] = "haven't" if x[1] == "havent"
                                  x[1] = "hasn't" if x[1] == "hasnt"
                                  x[1] = "hadn't" if x[1] == "hadnt"
                                  x[1] = "won't" if x[1] == "wont"
                                  x[1] = "wouldn't" if x[1] == "wouldnt"
                                  x[1] = "don't" if x[1] == "dont"
                                  x[1] = "doesn't" if x[1] == "doesnt"
                                  x[1] = "didn't" if x[1] == "didnt"
                                  x[1] = "can't" if x[1] == "cant"
                                  x[1] = "couldn't" if x[1] == "couldnt"
                                  x[1] = "shouldn't" if x[1] == "shouldnt"
                                  x[1] = "mightn't" if x[1] == "mightnt"
                                  x[1] = "mustn't" if x[1] == "mustnt"
                                      
  
                                      if x[0] == "twitter_hashtag"
                                          hash_tags.push( x[1] )
                                      end
  
                                      y.push(x[1])
                                  end
                              end
                          end
                      end
                  end
              end
          end
      end
  
      return y.join(" "), hash_tags
  end
  
  
  
  def tokenize(str)
      y = Array.new
      evaluator = TokenizerTwitter.new
      ar = evaluator.tokenize( _html( _accent( squeezeWhitespace( _downcase(  _cleanword( _convert( str ) ) ) ) ) ) )
  
      ar.each do |x|
          if x[0] != "ponctw"
              if x[0] != "space"
                  if x[0] != "sgmltag"
                      if x[0] != "hyphen"
                          if x[0] != "poncts"
                              if x[0] != "apos"
                                  if x[0] != "symbol"
  
                                  x[1] = "isn't" if x[1] == "isnt"
                                  x[1] = "aren't" if x[1] == "arent"
                                  x[1] = "wasn't" if x[1] == "wasnt"
                                  x[1] = "weren't" if x[1] == "werent"
                                  x[1] = "haven't" if x[1] == "havent"
                                  x[1] = "hasn't" if x[1] == "hasnt"
                                  x[1] = "hadn't" if x[1] == "hadnt"
                                  x[1] = "won't" if x[1] == "wont"
                                  x[1] = "wouldn't" if x[1] == "wouldnt"
                                  x[1] = "don't" if x[1] == "dont"
                                  x[1] = "doesn't" if x[1] == "doesnt"
                                  x[1] = "didn't" if x[1] == "didnt"
                                  x[1] = "can't" if x[1] == "cant"
                                  x[1] = "couldn't" if x[1] == "couldnt"
                                  x[1] = "shouldn't" if x[1] == "shouldnt"
                                  x[1] = "mightn't" if x[1] == "mightnt"
                                  x[1] = "mustn't" if x[1] == "mustnt"
                                      
  
                                      if x[1] != ".."
                                          if x[1] != "..."
                                              y.push(x[1])
                                          end
                                      end
                                  end
                              end
                          end
                      end
                  end
              end
          end
      end
  
      return y.join(" ")
  end