Class: NekonekoGen::TextClassifierGenerator
- Inherits:
-
Object
- Object
- NekonekoGen::TextClassifierGenerator
- Defined in:
- lib/nekoneko_gen/text_classifier_generator.rb
Instance Attribute Summary collapse
-
#quiet ⇒ Object
Returns the value of attribute quiet.
Instance Method Summary collapse
- #generate(lang = :ruby) ⇒ Object
- #generate_ruby_code ⇒ Object
-
#initialize(filename, files, options = {}) ⇒ TextClassifierGenerator
constructor
A new instance of TextClassifierGenerator.
- #train(iteration = nil) ⇒ Object
Constructor Details
#initialize(filename, files, options = {}) ⇒ TextClassifierGenerator
Returns a new instance of TextClassifierGenerator.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/nekoneko_gen/text_classifier_generator.rb', line 10 def initialize(filename, files, = {}) @quiet = false @options = @filename = filename @files = files @word2id = {} @id2word = {} @classifier = ClassifierFactory.create(files.size, ) @name = safe_name(@filename).split("_").map(&:capitalize).join @labels = files.map {|file| "#{safe_name(file).upcase}"} end |
Instance Attribute Details
#quiet ⇒ Object
Returns the value of attribute quiet.
9 10 11 |
# File 'lib/nekoneko_gen/text_classifier_generator.rb', line 9 def quiet @quiet end |
Instance Method Details
#generate(lang = :ruby) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/nekoneko_gen/text_classifier_generator.rb', line 67 def generate(lang = :ruby) lang ||= :ruby case lang when :ruby generate_ruby_code else raise NotImplementedError end @name end |
#generate_ruby_code ⇒ Object
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 |
# File 'lib/nekoneko_gen/text_classifier_generator.rb', line 77 def generate_ruby_code labels = @labels.each_with_index.map{|v, i| " #{v} = #{i}"}.join("\n") File.open(@filename, "w") do |f| f.write <<MODEL # -*- coding: utf-8 -*- require 'rubygems' require 'json' require 'bimyou_segmenter' class #{@name} def self.k K end def self.predict(text) classify(fv(text)) end #{labels} LABELS = #{@labels.inspect} K = #{@classifier.k} private def self.fv(text) prev = nil BimyouSegmenter.segment(text).map do |word| if (prev) if (NGRAM_TARGET =~ word) nword = [prev + word, word] prev = word nword else prev = nil word end else if (NGRAM_TARGET =~ word) prev = word end word end end.flatten(1) end #{@classifier.classify_method_code(:ruby)} NGRAM_TARGET = Regexp.new('(^[ァ-ヾ]+$)|(^[a-zA-Z\\-_a-zA-Z‐_0-90-9]+$)|' + '(^[々〇ヵヶ' + [0x3400].pack('U') + '-' + [0x9FFF].pack('U') + [0xF900].pack('U') + '-' + [0xFAFF].pack('U') + [0x20000].pack('U') + '-' + [0x2FFFF].pack('U') + ']+$)') #{@classifier.parameter_code(:ruby, lambda{|id| id2word(id) })} end MODEL end end |
#train(iteration = nil) ⇒ Object
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 |
# File 'lib/nekoneko_gen/text_classifier_generator.rb', line 22 def train(iteration = nil) iteration ||= @classifier.default_iteration data = [] @classifier.k.times do |i| t = Time.now data[i] = [] print "loading #{@files[i]}... " content = nil File.open(@files[i]) do |f| content = f.read end content = NKF.nkf('-w', content) content.lines do |line| vec = fv(line.chomp) if (vec.size > 0) data[i] << normalize(vec) end end puts sprintf("%.4fs", Time.now - t) end samples = data.map{|v| v.size}.min iteration.times do |step| loss = 0.0 c = 0 t = Time.now print sprintf("step %3d...", step) @classifier.k.times.map do |i| sampling(data[i], samples).map {|vec| [vec, i] } end.flatten(1).shuffle!.each do |v| loss += @classifier.update(v[0], v[1]) c += 1 end print sprintf(" %.6f, %.4fs\n", 1.0 - loss / c.to_f, Time.now - t) end if (@classifier.k > 2) @classifier.k.times do |i| puts "#{@labels[i]} : #{@classifier.features(i)} features" end else puts "#{@labels[0]}, #{@labels[1]} : #{@classifier.features(0)} features" end puts "done nyan! " end |