polaris-nlp 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +29 -5
- data/lib/polaris/nlp.rb +1 -0
- data/lib/polaris/nlp/calc.rb +38 -0
- data/lib/polaris/nlp/init.rb +45 -0
- data/lib/polaris/nlp/version.rb +1 -1
- data/polaris-nlp.gemspec +3 -0
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b143d4b16dc4270634b483eb8aed1cff3b934b8
|
4
|
+
data.tar.gz: 81169c951c23132e021c769ff9154f67fab484e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c33d6455655cd8ad0c6cc37d51919cd1e528064cfb162c8eea30f3f600e1479557078cd9ce3c34a8e67828b2ac12093fd25c5a98dd752babd89ed351db6b637a
|
7
|
+
data.tar.gz: 1951a8d2a7d6920a03faa054fc58b0182a59cf687105b2c390573a0247238172430e2ddadbe513e3bdf4018ccb641dda162b727fc3298c702f7f02f4569dab69
|
data/README.md
CHANGED
@@ -1,15 +1,25 @@
|
|
1
|
-
# Polaris
|
1
|
+
# Polaris
|
2
2
|
|
3
|
-
|
3
|
+
natural language processing / polarity analysis--
|
4
4
|
|
5
|
-
|
5
|
+
Polaris provides methods for polarity analysis (semantic orientations).
|
6
|
+
|
7
|
+
I use a dataset offered by the Tokyo Institute of Technology.
|
8
|
+
|
9
|
+
[Semantic Orientations of Words](http://www.lr.pi.titech.ac.jp/~takamura/pndic_en.html)
|
10
|
+
|
11
|
+
Gem Interface
|
12
|
+
|
13
|
+
## Requirements
|
14
|
+
|
15
|
+
* mecab
|
6
16
|
|
7
17
|
## Installation
|
8
18
|
|
9
19
|
Add this line to your application's Gemfile:
|
10
20
|
|
11
21
|
```ruby
|
12
|
-
gem 'polaris
|
22
|
+
gem 'polaris/nlp'
|
13
23
|
```
|
14
24
|
|
15
25
|
And then execute:
|
@@ -22,7 +32,21 @@ Or install it yourself as:
|
|
22
32
|
|
23
33
|
## Usage
|
24
34
|
|
25
|
-
|
35
|
+
### Init
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
|
39
|
+
require 'polaris/nlp'
|
40
|
+
Polaris::Nlp.init
|
41
|
+
|
42
|
+
```
|
43
|
+
|
44
|
+
### Calculate polarity
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
require 'polaris/nlp'
|
48
|
+
puts Polaris::Nlp.calc_polarity("I'm happy !")
|
49
|
+
```
|
26
50
|
|
27
51
|
## Development
|
28
52
|
|
data/lib/polaris/nlp.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'sqlite3'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'natto'
|
6
|
+
|
7
|
+
module Polaris
|
8
|
+
|
9
|
+
module Nlp
|
10
|
+
module_function
|
11
|
+
@path_to_db = "#{Dir.home}/.polaris/features.sqlite3"
|
12
|
+
|
13
|
+
# init method is called in first setup.
|
14
|
+
def calc_polarity(sentence)
|
15
|
+
|
16
|
+
if File.exists?(@path_to_db)
|
17
|
+
db = SQLite3::Database.new("#{@home}/.polaris/features.sqlite3")
|
18
|
+
nm = Natto::MeCab.new
|
19
|
+
|
20
|
+
sum = 0
|
21
|
+
nm.parse(sentence).split(/\n/).each do |line|
|
22
|
+
elem = line.split(/\t/)
|
23
|
+
break if elem[0] == 'EOS'
|
24
|
+
word = (elem[1].split(/\,/)[6] == '*') ? elem[0] : elem[1].split(/\,/)[6]
|
25
|
+
value = db.execute("SELECT value FROM models WHERE word = ? LIMIT 1",word)[0]
|
26
|
+
sum += value[0] if value
|
27
|
+
end
|
28
|
+
e = Math.exp(1)
|
29
|
+
return (e ** sum - e** (-1 * sum)) / (e ** sum + e** (-1 * sum))
|
30
|
+
else
|
31
|
+
puts ("features.sqlite3 not found.\nplease run Polaris::Nlp.init")
|
32
|
+
exit
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
data/lib/polaris/nlp/init.rb
CHANGED
@@ -1,11 +1,56 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
|
+
require 'sqlite3'
|
4
|
+
require 'open-uri'
|
5
|
+
|
3
6
|
module Polaris
|
4
7
|
|
5
8
|
module Nlp
|
9
|
+
module_function
|
10
|
+
|
11
|
+
@home = Dir.home
|
6
12
|
|
7
13
|
# init method is called in first setup.
|
8
14
|
def init
|
15
|
+
|
16
|
+
unless Dir.exists?("#{@home}/.polaris")
|
17
|
+
Dir.mkdir("#{@home}/.polaris")
|
18
|
+
end
|
19
|
+
|
20
|
+
puts "Download data..."
|
21
|
+
ja_text = open('http://www.lr.pi.titech.ac.jp/%7Etakamura/pubs/pn_ja.dic').read.encode('utf-8','sjis')
|
22
|
+
en_text = open('http://www.lr.pi.titech.ac.jp/%7Etakamura/pubs/pn_en.dic').read.encode('utf-8','sjis')
|
23
|
+
puts "Done"
|
24
|
+
|
25
|
+
if File.exists?("#{@home}/.polaris/features.sqlite3")
|
26
|
+
FileUtils.rm("#{@home}/.polaris/features.sqlite3")
|
27
|
+
end
|
28
|
+
|
29
|
+
db = SQLite3::Database.new("#{@home}/.polaris/features.sqlite3")
|
30
|
+
db.execute("CREATE TABLE models (word TEXT ,value REAL);")
|
31
|
+
|
32
|
+
puts "Seed Japanese data..."
|
33
|
+
ja_text.split("\n").each do |line|
|
34
|
+
item = line.split(':')
|
35
|
+
word = item[0]
|
36
|
+
value = item[3]
|
37
|
+
insert_value(db,word,value)
|
38
|
+
end
|
39
|
+
puts "Done"
|
40
|
+
|
41
|
+
puts "Seed English data..."
|
42
|
+
en_text.split("\n").each do |line|
|
43
|
+
item = line.split(':')
|
44
|
+
word = item[0]
|
45
|
+
value = item[2]
|
46
|
+
insert_value(db,word,value)
|
47
|
+
end
|
48
|
+
puts "Done"
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
def insert_value(db,word,value)
|
53
|
+
return db.execute("INSERT INTO models (word, value) VALUES(?, ?);", word, value)
|
9
54
|
end
|
10
55
|
|
11
56
|
end
|
data/lib/polaris/nlp/version.rb
CHANGED
data/polaris-nlp.gemspec
CHANGED
@@ -20,6 +20,9 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
21
|
spec.require_paths = ["lib"]
|
22
22
|
|
23
|
+
spec.add_dependency "sqlite3"
|
24
|
+
spec.add_dependency "natto"
|
25
|
+
|
23
26
|
spec.add_development_dependency "bundler", "~> 1.10"
|
24
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
25
28
|
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polaris-nlp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- himkt
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sqlite3
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: natto
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
13
41
|
- !ruby/object:Gem::Dependency
|
14
42
|
name: bundler
|
15
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -54,6 +82,7 @@ files:
|
|
54
82
|
- bin/console
|
55
83
|
- bin/setup
|
56
84
|
- lib/polaris/nlp.rb
|
85
|
+
- lib/polaris/nlp/calc.rb
|
57
86
|
- lib/polaris/nlp/init.rb
|
58
87
|
- lib/polaris/nlp/version.rb
|
59
88
|
- polaris-nlp.gemspec
|