Class: SpaCyModel
- Inherits:
-
VectorModel
- Object
- VectorModel
- SpaCyModel
- Defined in:
- lib/rbbt/vector/model/spaCy.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Attributes inherited from VectorModel
#directory, #eval_model, #extract_features, #factor_levels, #features, #labels, #model_file, #names, #train_model
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(dir, config, categories = %w(positive negative),, lang = 'en_core_web_md') ⇒ SpaCyModel
constructor
A new instance of SpaCyModel.
Methods inherited from VectorModel
R_eval, R_run, R_train, #__load_method, #add, #add_list, #clear, #cross_validation, #eval, #eval_list, f1_metrics, #run, #save_models, #train
Constructor Details
#initialize(dir, config, categories = %w(positive negative),, lang = 'en_core_web_md') ⇒ SpaCyModel
Returns a new instance of SpaCyModel.
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 |
# File 'lib/rbbt/vector/model/spaCy.rb', line 13 def initialize(dir, config, categories = %w(positive negative), lang = 'en_core_web_md') @config = case when Path === config config.read when Misc.is_filename?(config) Open.read(config) when (Misc.is_filename?(config, false) && Rbbt.share.spaCy.cpu[config].exists?) Rbbt.share.spaCy.cpu[config].read when (Misc.is_filename?(config, false) && Rbbt.share.spaCy[config].exists?) Rbbt.share.spaCy[config].read else config end @lang = lang super(dir) @train_model = Proc.new do |file, features, labels| texts = features docs = [] tmpconfig = File.join(file, 'config') tmptrain = File.join(file, 'train.spacy') SpaCy.config(@config, tmpconfig) SpaCyModel.spacy do nlp = SpaCy.nlp(lang) docs = [] RbbtPython.iterate nlp.pipe(texts.zip(labels), as_tuples: true), :bar => "Training documents into spacy format" do |doc,label| doc.cats[label] = 1 #if %w(1 true pos).include?(label.to_s.downcase) # doc.cats["positive"] = 1 # doc.cats["negative"] = 0 #else # doc.cats["positive"] = 0 # doc.cats["negative"] = 1 #end docs << doc end doc_bin = spacy.tokens.DocBin.new(docs: docs) doc_bin.to_disk(tmptrain) end gpu = Rbbt::Config.get('gpu_id', :spacy, :spacy_train, :default => 0) CMD.cmd_log(:spacy, "train #{tmpconfig} --output #{file} --paths.train #{tmptrain} --paths.dev #{tmptrain}", "--gpu-id" => gpu) end @eval_model = Proc.new do |file, features| texts = features docs = [] SpaCyModel.spacy do nlp = spacy.load("#{file}/model-best") Log::ProgressBar. texts.length, :desc => "Evaluating documents" do || texts.collect do |text| cats = nlp.(text).cats .tick cats.sort_by{|l,v| v.to_f }.last.first #cats['positive'] > cats['negative'] ? 1 : 0 end end end end end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
5 6 7 |
# File 'lib/rbbt/vector/model/spaCy.rb', line 5 def config @config end |
Class Method Details
.spacy(&block) ⇒ Object
7 8 9 10 11 |
# File 'lib/rbbt/vector/model/spaCy.rb', line 7 def self.spacy(&block) RbbtPython.run "spacy" do RbbtPython.module_eval(&block) end end |