Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX] Fix failing specs on item repeats
Fix #7 * Use a C or Java extension * Kill a lot of yucky, poorly performing and error prone code
- Loading branch information
1 parent
d3bf708
commit 1630fce
Showing
16 changed files
with
152 additions
and
683 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.DS_Store | ||
*.gem | ||
.bundle | ||
*.bundle | ||
*.jar | ||
Gemfile.lock | ||
pkg/* | ||
spec/reports | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.benlangfeld.ruby_speech; | ||
|
||
import org.jruby.Ruby; | ||
import org.jruby.RubyClass; | ||
import org.jruby.RubyModule; | ||
import org.jruby.RubyObject; | ||
import org.jruby.anno.JRubyClass; | ||
import org.jruby.anno.JRubyMethod; | ||
import org.jruby.runtime.ObjectAllocator; | ||
import org.jruby.runtime.ThreadContext; | ||
import org.jruby.runtime.Visibility; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
import org.jruby.javasupport.util.RuntimeHelpers; | ||
|
||
import java.util.regex.*; | ||
|
||
@JRubyClass(name="RubySpeech::GRXML::Matcher") | ||
public class RubySpeechGRXMLMatcher extends RubyObject { | ||
|
||
public RubySpeechGRXMLMatcher(final Ruby runtime, RubyClass rubyClass) { | ||
super(runtime, rubyClass); | ||
} | ||
|
||
@JRubyMethod(visibility=Visibility.PRIVATE) | ||
public IRubyObject check_potential_match(ThreadContext context, IRubyObject buffer) | ||
{ | ||
Ruby runtime = context.getRuntime(); | ||
|
||
IRubyObject regex = getInstanceVariable("@regex"); | ||
|
||
Pattern p = Pattern.compile(regex.toString()); | ||
Matcher m = p.matcher(buffer.toString()); | ||
|
||
if (m.matches()) { | ||
} else if (m.hitEnd()) { | ||
RubyModule potential_match = runtime.getClassFromPath("RubySpeech::GRXML::PotentialMatch"); | ||
return RuntimeHelpers.invoke(context, potential_match, "new"); | ||
} | ||
return runtime.getNil(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.benlangfeld.ruby_speech; | ||
|
||
import org.jruby.Ruby; | ||
import org.jruby.RubyClass; | ||
import org.jruby.RubyModule; | ||
import org.jruby.RubyObject; | ||
import org.jruby.runtime.ObjectAllocator; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
import org.jruby.runtime.load.BasicLibraryService; | ||
|
||
public class RubySpeechService implements BasicLibraryService { | ||
public boolean basicLoad(Ruby ruby) { | ||
RubyModule ruby_speech = ruby.defineModule("RubySpeech"); | ||
RubyModule grxml = ruby_speech.defineModuleUnder("GRXML"); | ||
RubyClass matcher = grxml.defineClassUnder("Matcher", ruby.getObject(), new ObjectAllocator() { | ||
public IRubyObject allocate(Ruby runtime, RubyClass rubyClass) { | ||
return new RubySpeechGRXMLMatcher(runtime, rubyClass); | ||
} | ||
}); | ||
matcher.defineAnnotatedMethods(RubySpeechGRXMLMatcher.class); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'mkmf' | ||
|
||
$LIBS << " -lpcre" | ||
|
||
abort "-----\n#{lib} is missing.\n-----" unless find_header('pcre.h') | ||
|
||
create_makefile 'ruby_speech' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "ruby.h" | ||
#include "pcre.h" | ||
#include <stdio.h> | ||
|
||
static VALUE method_check_potential_match(VALUE self, VALUE buffer) | ||
{ | ||
int erroffset = 0; | ||
const char *errptr = ""; | ||
int options = 0; | ||
VALUE regex_string = rb_funcall(rb_iv_get(self, "@regex"), rb_intern("to_s"), 0); | ||
const char *regex = StringValueCStr(regex_string); | ||
|
||
pcre *compiled_regex = pcre_compile(regex, options, &errptr, &erroffset, NULL); | ||
|
||
int result = 0; | ||
int ovector[30]; | ||
int workspace[1024]; | ||
char *input = StringValueCStr(buffer); | ||
result = pcre_dfa_exec(compiled_regex, NULL, input, strlen(input), 0, PCRE_PARTIAL, | ||
ovector, sizeof(ovector) / sizeof(ovector[0]), | ||
workspace, sizeof(workspace) / sizeof(workspace[0])); | ||
pcre_free(compiled_regex); | ||
|
||
if (result == PCRE_ERROR_PARTIAL) { | ||
VALUE RubySpeech = rb_const_get(rb_cObject, rb_intern("RubySpeech")); | ||
VALUE GRXML = rb_const_get(RubySpeech, rb_intern("GRXML")); | ||
VALUE PotentialMatch = rb_const_get(GRXML, rb_intern("PotentialMatch")); | ||
|
||
return rb_class_new_instance(0, NULL, PotentialMatch); | ||
} | ||
return Qnil; | ||
} | ||
|
||
void Init_ruby_speech() | ||
{ | ||
VALUE RubySpeech = rb_define_module("RubySpeech"); | ||
VALUE GRXML = rb_define_module_under(RubySpeech, "GRXML"); | ||
VALUE Matcher = rb_define_class_under(GRXML, "Matcher", rb_cObject); | ||
|
||
rb_define_method(Matcher, "check_potential_match", method_check_potential_match, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.