Module: Polaris::Nlp
- Defined in:
- lib/polaris/nlp.rb,
lib/polaris/nlp/calc.rb,
lib/polaris/nlp/init.rb,
lib/polaris/nlp/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
-
.calc_polarity(sentence) ⇒ Object
init method is called in first setup.
-
.init ⇒ Object
init method is called in first setup.
- .insert_value(db, word, value) ⇒ Object
Class Method Details
.calc_polarity(sentence) ⇒ Object
init method is called in first setup.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/polaris/nlp/calc.rb', line 14 def calc_polarity(sentence) if File.exists?(@path_to_db) db = SQLite3::Database.new("#{@home}/.polaris/features.sqlite3") nm = Natto::MeCab.new sum = 0 nm.parse(sentence).split(/\n/).each do |line| elem = line.split(/\t/) break if elem[0] == 'EOS' word = (elem[1].split(/\,/)[6] == '*') ? elem[0] : elem[1].split(/\,/)[6] value = db.execute("SELECT value FROM models WHERE word = ? LIMIT 1",word)[0] sum += value[0] if value end e = Math.exp(1) return (e ** sum - e** (-1 * sum)) / (e ** sum + e** (-1 * sum)) else puts ("features.sqlite3 not found.\nplease run Polaris::Nlp.init") exit end end |
.init ⇒ Object
init method is called in first setup.
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 |
# File 'lib/polaris/nlp/init.rb', line 14 def init unless Dir.exists?("#{@home}/.polaris") Dir.mkdir("#{@home}/.polaris") end puts "Download data..." ja_text = open('http://www.lr.pi.titech.ac.jp/%7Etakamura/pubs/pn_ja.dic').read.encode('utf-8','sjis') en_text = open('http://www.lr.pi.titech.ac.jp/%7Etakamura/pubs/pn_en.dic').read.encode('utf-8','sjis') puts "Done" if File.exists?("#{@home}/.polaris/features.sqlite3") FileUtils.rm("#{@home}/.polaris/features.sqlite3") end db = SQLite3::Database.new("#{@home}/.polaris/features.sqlite3") db.execute("CREATE TABLE models (word TEXT ,value REAL);") puts "Seed Japanese data..." ja_text.split("\n").each do |line| item = line.split(':') word = item[0] value = item[3] insert_value(db,word,value) end puts "Done" puts "Seed English data..." en_text.split("\n").each do |line| item = line.split(':') word = item[0] value = item[2] insert_value(db,word,value) end puts "Done" end |
.insert_value(db, word, value) ⇒ Object
52 53 54 |
# File 'lib/polaris/nlp/init.rb', line 52 def insert_value(db,word,value) return db.execute("INSERT INTO models (word, value) VALUES(?, ?);", word, value) end |