class Linguist::Tokenizer
Generic programming language tokenizer.
Tokens are designed for use in the language bayes classifier. It strips any data strings or comments and preserves significant language symbols.
Public Class Methods
tokenize(data)
click to toggle source
Public: Extract tokens from data
data - String to tokenize
Returns Array of token Strings.
# File lib/linguist/tokenizer.rb, line 16 def self.tokenize(data) new.extract_tokens(data) end
Public Instance Methods
extract_tokens(p1)
click to toggle source
static VALUE rb_tokenizer_extract_tokens(VALUE self, VALUE rb_data) { YY_BUFFER_STATE buf; yyscan_t scanner; struct tokenizer_extra extra; VALUE ary, s; long len; int r; Check_Type(rb_data, T_STRING); len = RSTRING_LEN(rb_data); if (len > 100000) len = 100000; linguist_yylex_init_extra(&extra, &scanner); buf = linguist_yy_scan_bytes(RSTRING_PTR(rb_data), (int) len, scanner); ary = rb_ary_new(); do { extra.type = NO_ACTION; extra.token = NULL; r = linguist_yylex(scanner); switch (extra.type) { case NO_ACTION: break; case REGULAR_TOKEN: rb_ary_push(ary, rb_str_new2(extra.token)); free(extra.token); break; case SHEBANG_TOKEN: s = rb_str_new2("SHEBANG#!"); rb_str_cat2(s, extra.token); rb_ary_push(ary, s); free(extra.token); break; case SGML_TOKEN: s = rb_str_new2(extra.token); rb_str_cat2(s, ">"); rb_ary_push(ary, s); free(extra.token); break; } } while (r); linguist_yy_delete_buffer(buf, scanner); linguist_yylex_destroy(scanner); return ary; }