pihsi 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a6adab4ac73877425b169cfba97535972b4b7cd
4
- data.tar.gz: a4c59e6f444ab4847488184e916525a9f44833e2
3
+ metadata.gz: 59186bbdfc0d6a8209230283e39111f2dea70f67
4
+ data.tar.gz: 798220a80b22f1a512aa9685db6897f6145719b2
5
5
  SHA512:
6
- metadata.gz: a48cfd2701d562e9cacf3b8234a7476210a112f48eb50ce7f78d5d55809b51bd2ae1b46bba2f2e06e5b22fdd1d20daa1490eb7477ad35ca89b84a31799f3de13
7
- data.tar.gz: f12d78ca7636e6fd6f431e246bc5c22c5147040c05bd34817734765ef17e3f65e47b463669baacdfb2365dbc8d0708e91aadb46250606705b1a02a8c91b9aed6
6
+ metadata.gz: 94ffd1f8da9d88e6d230ec824e2c0e945061d4bbca532960806c6f8913e992ed37c40df0e26adcc82422b25455bc20c13312b558cd11d418efa8b441412a8a2b
7
+ data.tar.gz: 834d2901bdf0addffde57f43757197b19ab1f327ee315b76bc9d814407536b3d17f85fe03535b27543b87772f7d300f65018c1023755093d3dab0dc938c74c62
data/.travis.yml CHANGED
@@ -5,8 +5,6 @@ rvm:
5
5
  - 1.9.3
6
6
  - rbx-2.2.7
7
7
  before_install:
8
- - sudo apt-get update
9
- - sudo apt-get install build-essential bison
10
8
  - wget http://downloads.sourceforge.net/project/cmusphinx/sphinxbase/0.8/sphinxbase-0.8.tar.gz
11
9
  - tar -zxf sphinxbase-0.8.tar.gz
12
10
  - cd sphinxbase-0.8
@@ -21,3 +19,4 @@ before_install:
21
19
  - make
22
20
  - sudo make install
23
21
  - cd ..
22
+ - sudo ldconfig
@@ -1,33 +1,29 @@
1
1
  #include <ruby.h>
2
2
  #include <pocketsphinx.h>
3
3
 
4
- VALUE recognize(VALUE self, VALUE data) {
4
+ typedef struct ps {
5
+ ps_decoder_t *decoder;
6
+ } PocketSphinx;
7
+
8
+ VALUE decode(VALUE self, VALUE data) {
5
9
  char const *hyp, *uttid;
6
10
  int rv;
7
11
  int32 score;
8
12
  ps_decoder_t *ps;
9
- cmd_ln_t *config;
13
+ PocketSphinx *pocketSphinx;
14
+ VALUE result = rb_hash_new();
15
+
16
+ Data_Get_Struct(self, PocketSphinx, pocketSphinx);
17
+ ps = pocketSphinx -> decoder;
10
18
 
11
19
  int data_length = RARRAY_LEN(data);
12
20
  int16 *c_data = malloc(sizeof(int16) * data_length);
13
21
 
14
- char *hmm = RSTRING_PTR(rb_funcall(self, rb_intern("hmm"), 0));
15
- char *lm = RSTRING_PTR(rb_funcall(self, rb_intern("lm"), 0));
16
- char *dict = RSTRING_PTR(rb_funcall(self, rb_intern("dict"), 0));
17
-
18
22
  int i;
19
23
  for(i = 0; i < data_length; i++) {
20
- c_data[i] = NUM2SHORT(rb_ary_entry(data, i));
21
- }
22
-
23
- config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", hmm, "-lm", lm, "-dict", dict, "-logfn", "/dev/null", NULL);
24
-
25
- if (config == NULL) {
26
- free(c_data);
27
- rb_raise(rb_eStandardError, "configuration might be wrong");
24
+ c_data[i] = NUM2INT(rb_ary_entry(data, i)); // Rubinius does not support NUM2SHORT();
28
25
  }
29
26
 
30
- ps = ps_init(config);
31
27
  rv = ps_start_utt(ps, "goforward");
32
28
 
33
29
  if (rv < 0) {
@@ -50,12 +46,59 @@ VALUE recognize(VALUE self, VALUE data) {
50
46
  if (hyp == NULL) {
51
47
  return Qnil;
52
48
  } else {
53
- return rb_str_new2(hyp);
49
+ rb_hash_aset(result, rb_str_new2("score"), INT2NUM(hyp));
50
+ rb_hash_aset(result, rb_str_new2("hypothesis"), rb_str_new2(hyp));
51
+ return result;
54
52
  }
55
53
  }
56
54
 
55
+ static void deallocate(void *ps) {
56
+ PocketSphinx *pocketSphinx = ps;
57
+ ps_free(pocketSphinx -> decoder);
58
+ free(pocketSphinx);
59
+ }
60
+
61
+ static VALUE allocate(VALUE self) {
62
+ PocketSphinx *ps;
63
+ return Data_Make_Struct(self, PocketSphinx, 0, deallocate, ps);
64
+ }
65
+
66
+ VALUE initialize(VALUE self, VALUE options) {
67
+ int i;
68
+ PocketSphinx *ps;
69
+ VALUE option, key, value, klass;
70
+ char *c_key;
71
+ cmd_ln_t *config = cmd_ln_init(NULL, ps_args(), TRUE, "-logfn", "/dev/null", NULL);
72
+
73
+ for (i = 0; i < RARRAY_LEN(options); i++) {
74
+ option = rb_ary_entry(options, i);
75
+ key = rb_ary_entry(option, 0);
76
+ c_key = RSTRING_PTR(key);
77
+ value = rb_ary_entry(option, 1);
78
+ klass = rb_funcall(value, rb_intern("class"), 0);
79
+
80
+ if (klass == rb_cString) {
81
+ cmd_ln_set_str_r(config, c_key, RSTRING_PTR(value));
82
+ } else if (klass == rb_cFixnum) {
83
+ cmd_ln_set_int_r(config, c_key, NUM2LONG(value));
84
+ } else if (klass == rb_cFloat) {
85
+ cmd_ln_set_float_r(config, c_key, NUM2DBL(value));
86
+ }
87
+ }
88
+
89
+ if (config == NULL)
90
+ rb_raise(rb_eStandardError, "bad configuration");
91
+
92
+ Data_Get_Struct(self, PocketSphinx, ps);
93
+ ps -> decoder = ps_init(config);
94
+ return self;
95
+ }
96
+
57
97
  void Init_pocket_sphinx() {
58
98
  VALUE rb_mPihsi = rb_define_module("Pihsi");
59
99
  VALUE rb_mPocketSphinx = rb_define_module_under(rb_mPihsi, "PocketSphinx");
60
- rb_define_method(rb_mPocketSphinx, "recognize", recognize, 1);
100
+ VALUE rb_cDecoder = rb_define_class_under(rb_mPocketSphinx, "Decoder", rb_cObject);
101
+ rb_define_alloc_func(rb_cDecoder, allocate);
102
+ rb_define_method(rb_cDecoder, "initialize", initialize, 1);
103
+ rb_define_method(rb_cDecoder, "decode", decode, 1);
61
104
  }
@@ -2,15 +2,20 @@ require "pocket_sphinx/pocket_sphinx"
2
2
 
3
3
  module Pihsi
4
4
  class SpeechRecognizer
5
- include PocketSphinx
6
- attr_reader :hmm, :lm, :dict
5
+ attr_reader :decoder
7
6
 
8
- def initialize(hmm, lm, dict)
9
- @hmm, @lm, @dict = hmm, lm, dict
7
+ def initialize(options = {})
8
+ _options = options.inject({}) do |result, (key, value)|
9
+ result["-#{key}"] = value unless value.nil?
10
+ result
11
+ end.to_a
12
+ @decoder = PocketSphinx::Decoder.new(_options)
10
13
  end
11
14
 
12
15
  def recognize(data)
13
- super data.unpack('s*')
16
+ if result = decoder.decode(data.unpack('s*'))
17
+ result["hypothesis"]
18
+ end
14
19
  end
15
20
  end
16
21
  end
data/lib/pihsi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pihsi
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -3,7 +3,10 @@ FactoryGirl.define do
3
3
  hmm 'spec/fixtures/model/hmm/en_US/hub4wsj_sc_8k'
4
4
  lm 'spec/fixtures/model/lm/en/turtle.DMP'
5
5
  dict 'spec/fixtures/model/lm/en/turtle.dic'
6
+ pip 1.0
7
+ samprate 16000.0
8
+ maxhmmpf -1
6
9
 
7
- initialize_with { new(hmm, lm, dict) }
10
+ initialize_with { new(attributes) }
8
11
  end
9
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pihsi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zhi-Qiang Lei
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-01 00:00:00.000000000 Z
11
+ date: 2014-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler