ruby_fast_text 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c8a62041da945594d545e6d0f45a3349dd9d6dd1
4
+ data.tar.gz: d75c61b72631f67669d347a42705a18983c02567
5
+ SHA512:
6
+ metadata.gz: 83244f07a1cec29a4c03f796682c96bd6a7124d232b8ec35d735afe0fd6da66168596a0fdf4efbe1e91f0d6e870406d86cb5d7e23cfe35dd67831d1126c04211
7
+ data.tar.gz: 0aaab9dd2d5edb8bec947fa06c4eda27442ea5b3684cd52b83b86114d438fda97defd044ff831dcdb8adc2105e6ef7e93adf7a8206fd3e9e6b0496f8f2a748b4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.vscode
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,7 @@
1
+ build
2
+ clean
3
+ clobber
4
+ install
5
+ install:local
6
+ release[remote]
7
+ test
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ruby_fast_text.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ # RubyFastText
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ruby_fast_text`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ruby_fast_text'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ruby_fast_text
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_fast_text.
@@ -0,0 +1,17 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
11
+
12
+ namespace :install do
13
+ task :fast_text do
14
+ `git clone https://github.com/facebookresearch/fastText.git`
15
+ `cd fastText && make`
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruby_fast_text"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ module RubyFastText
2
+ # Your code goes here...
3
+ require "ruby_fast_text/version"
4
+ require "ruby_fast_text/commands"
5
+ require "ruby_fast_text/classifier"
6
+ require "ruby_fast_text/word_vectors"
7
+ require "ruby_fast_text/skipgram"
8
+ require "ruby_fast_text/cbow"
9
+ require "ruby_fast_text/nn"
10
+ end
@@ -0,0 +1,13 @@
1
+ module RubyFastText
2
+ class Cbow
3
+ extend RubyFastText::Commands
4
+
5
+ def self.train(params)
6
+ path = params[:path]
7
+
8
+ Dir.chdir(path) do
9
+ system("fasttext cbow #{params_to_s(params)}")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,55 @@
1
+ module RubyFastText
2
+ class Classifier
3
+ attr_accessor :classifier
4
+
5
+ attr_reader :probability
6
+ alias probability? probability
7
+
8
+ extend RubyFastText::Commands
9
+
10
+ def initialize(params)
11
+ path = params[:path]
12
+ model = params[:model]
13
+ labels_number = params.fetch(:labels_number, 1)
14
+ @probability = params[:probability]
15
+ probability_param = probability ? 'predict-prob' : 'predict'
16
+ #p File.expand_path File.dirname(__FILE__)
17
+ #p $0
18
+ @classifier = Dir.chdir(path) do
19
+ IO.popen("fasttext #{probability_param} #{model} - #{labels_number}", 'r+')
20
+ end
21
+ end
22
+
23
+ def predict(sentence)
24
+ classifier.puts(sentence)
25
+ format_result(classifier.gets)
26
+ end
27
+
28
+ def self.train(params)
29
+ path = params[:path]
30
+ Dir.chdir(path) do
31
+ system("fasttext supervised #{params_to_s(params)}")
32
+ end
33
+ end
34
+
35
+ def self.test(params)
36
+ path = params[:path]
37
+ model = params[:model]
38
+ test_data = params[:test_data]
39
+ k = params.fetch(:k, 1)
40
+ Dir.chdir(path) do
41
+ system("fasttext test #{model} #{test_data} #{k}")
42
+ end
43
+ end
44
+
45
+
46
+ private
47
+ def format_result(result)
48
+ if probability?
49
+ Hash[*result.strip.split.flatten]
50
+ else
51
+ result.strip.split
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,14 @@
1
+ module RubyFastText
2
+ module Commands
3
+ def params_to_s(params)
4
+ commands = %i[input output verbose minCount minCountLabel
5
+ wordNgrams bucket minn mann maxn t label lr lrUpdateRate
6
+ dim ws epoch neg loss thread pretrainedVectors saveOutput
7
+ cutoff retrain qnorm qout dsub]
8
+
9
+ params.select { |k, _v| commands.include? k}
10
+ .map { |k,v| "-#{k.to_s} #{v}"}
11
+ .join(" ")
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,45 @@
1
+ module RubyFastText
2
+ class NN
3
+ attr_accessor :predictor
4
+ attr_reader :k
5
+
6
+ def initialize(params)
7
+ path = params[:path]
8
+ model = params[:model]
9
+ @k = params.fetch(:k, 10)
10
+
11
+ @predictor = Dir.chdir(path) do
12
+ IO.popen("fasttext nn #{model} #{k}", 'r+')
13
+ end
14
+ end
15
+
16
+ # TODO: Fix, only getting one line out of the result
17
+ def neighbours(word)
18
+ predictor.puts(word)
19
+ format_result(read_lines)
20
+ end
21
+
22
+ private
23
+ def read_lines
24
+ # Should be done with predictor.readlines
25
+ # but process gets blocked...
26
+ lines = ''
27
+ k.times {
28
+ lines << predictor.gets
29
+ }
30
+ lines
31
+ end
32
+
33
+ def format_result(result)
34
+ # Deletes CLI comment on first line
35
+ # Separates lines into array
36
+ # Transforms each line into hash entry
37
+ result.sub!('Query word? ', '')
38
+ .split("\n")
39
+ .each_with_object({}) do |item, res|
40
+ key, value = item.split
41
+ res[key] = value
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module RubyFastText
2
+ class Skipgram
3
+ extend RubyFastText::Commands
4
+
5
+ def self.train(params)
6
+ path = params[:path]
7
+
8
+ Dir.chdir(path) do
9
+ system("fasttext skipgram #{params_to_s(params)}")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module RubyFastText
2
+ VERSION = "0.3.5"
3
+ end
@@ -0,0 +1,24 @@
1
+ module RubyFastText
2
+ class WordVectors
3
+ attr_accessor :vectors
4
+
5
+ def initialize(params)
6
+ path = params[:path]
7
+ model = params[:model]
8
+ @vectors = Dir.chdir(path) do
9
+ IO.popen("fasttext print-word-vectors #{model}", 'r+')
10
+ end
11
+ end
12
+
13
+ def get_vector(word)
14
+ vectors.puts(word)
15
+ format_result(vectors.gets)
16
+ end
17
+
18
+ private
19
+ def format_result(result)
20
+ result.split.drop(1)
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ruby_fast_text/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_fast_text"
8
+ spec.version = RubyFastText::VERSION
9
+ spec.authors = ["amval"]
10
+ spec.email = ["alberto.m.valdunciel@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby wrapper for FastText using the CLI.}
13
+ spec.description = %q{Ruby wrapper for FastText using the CLI.}
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_fast_text
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.5
5
+ platform: ruby
6
+ authors:
7
+ - amval
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Ruby wrapper for FastText using the CLI.
56
+ email:
57
+ - alberto.m.valdunciel@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rake_tasks~"
64
+ - ".travis.yml"
65
+ - ".vscode/tasks.json"
66
+ - Gemfile
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/ruby_fast_text.rb
72
+ - lib/ruby_fast_text/cbow.rb
73
+ - lib/ruby_fast_text/classifier.rb
74
+ - lib/ruby_fast_text/commands.rb
75
+ - lib/ruby_fast_text/nn.rb
76
+ - lib/ruby_fast_text/skipgram.rb
77
+ - lib/ruby_fast_text/version.rb
78
+ - lib/ruby_fast_text/word_vectors.rb
79
+ - ruby_fast_text.gemspec
80
+ homepage:
81
+ licenses: []
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.8
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Ruby wrapper for FastText using the CLI.
103
+ test_files: []