foma 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ v0.1.0. initial release
data/README.rdoc ADDED
@@ -0,0 +1,33 @@
1
+ = foma
2
+
3
+ A wrapper for the FOMA finite state library.
4
+
5
+ The wrapper is very rudimentary (or an early version, if you like). Only
6
+ loading of an FSM and the operations 'apply up' and 'apply down' are
7
+ supported.
8
+
9
+ == Installation
10
+
11
+ gem install foma
12
+
13
+ == Usage
14
+
15
+ fsm = FOMA::FSM.new("fsm.bin")
16
+
17
+ fsm.apply_up("foo")
18
+ # => true
19
+
20
+ fsm.apply_down("bar")
21
+ # => true
22
+
23
+ fsm.apply_up("foo") do |d|
24
+ puts d
25
+ end
26
+
27
+ == Development
28
+
29
+ The project is hosted on github on http://github.com/mlj/ruby-foma.
30
+
31
+ == License
32
+
33
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ begin
3
+ require 'jeweler'
4
+
5
+ Jeweler::Tasks.new do |p|
6
+ p.name = "foma"
7
+ p.summary = "FOMA finite state library interface"
8
+ p.description = "A wrapper for the FOMA finite state library"
9
+ p.authors = ['Marius L. Jøhndal']
10
+ p.email = "mariuslj (at) ifi [dot] uio (dot) no"
11
+ p.homepage = "http://github.com/mlj/ruby-foma"
12
+ p.rubyforge_project = "foma"
13
+ end
14
+ rescue LoadError
15
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
16
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/ext/extconf.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+ $LDFLAGS = '-lfoma'
3
+ create_makefile "foma"
data/ext/foma.c ADDED
@@ -0,0 +1,110 @@
1
+ #include "ruby.h"
2
+ #include <fomalib.h>
3
+
4
+ /*:enddoc:*/
5
+
6
+ VALUE mFOMA = Qnil;
7
+ VALUE mFSM = Qnil;
8
+
9
+ struct foma_fsm {
10
+ struct fsm *net;
11
+ struct apply_handle *ah;
12
+ };
13
+
14
+ static void foma_fsm_mark(struct foma_fsm *t)
15
+ {
16
+ }
17
+
18
+ static void foma_fsm_free(struct foma_fsm *t)
19
+ {
20
+ if (t) {
21
+ fsm_destroy(t->net);
22
+ apply_clear(t->ah);
23
+ free(t);
24
+ }
25
+ }
26
+
27
+ static VALUE foma_fsm_alloc(VALUE klass)
28
+ {
29
+ struct foma_fsm *t = NULL;
30
+
31
+ return Data_Wrap_Struct(klass, foma_fsm_mark, foma_fsm_free, t);
32
+ }
33
+
34
+ static VALUE foma_fsm_init(VALUE obj, VALUE filename)
35
+ {
36
+ FILE *file;
37
+ struct foma_fsm *t;
38
+
39
+ t = malloc(sizeof(struct foma_fsm));
40
+
41
+ if (!t) {
42
+ rb_raise(rb_eRuntimeError, "Error allocating memory");
43
+ }
44
+
45
+ file = fopen(RSTRING(filename)->ptr, "rb");
46
+
47
+ if (!file) {
48
+ free(t);
49
+ rb_raise(rb_eRuntimeError, "Unable to open file %s", RSTRING(filename)->ptr);
50
+ }
51
+
52
+ fclose(file);
53
+
54
+ t->net = fsm_read_binary_file(RSTRING(filename)->ptr);
55
+
56
+ if (!t->net) {
57
+ free(t);
58
+ rb_raise(rb_eRuntimeError, "Unable to read file %s", RSTRING(filename)->ptr);
59
+ }
60
+
61
+ t->ah = apply_init(t->net);
62
+
63
+ DATA_PTR(obj) = t;
64
+ return Qnil;
65
+ }
66
+
67
+ static VALUE foma_fsm_apply(VALUE self, VALUE string, char *(*applyer)())
68
+ {
69
+ struct foma_fsm *t;
70
+ char *result;
71
+
72
+ Check_Type(string, T_STRING);
73
+
74
+ Data_Get_Struct(self, struct foma_fsm, t);
75
+
76
+ result = applyer(t->ah, RSTRING(string)->ptr);
77
+
78
+ if (result) {
79
+ if (rb_block_given_p()) {
80
+ rb_yield(rb_str_new2(result));
81
+
82
+ while (result = applyer(t->ah, NULL))
83
+ rb_yield(rb_str_new2(result));
84
+ }
85
+
86
+ return Qtrue;
87
+ } else {
88
+ return Qfalse;
89
+ }
90
+ }
91
+
92
+ static VALUE foma_fsm_apply_down(VALUE self, VALUE string)
93
+ {
94
+ return foma_fsm_apply(self, string, &apply_down);
95
+ }
96
+
97
+ static VALUE foma_fsm_apply_up(VALUE self, VALUE string)
98
+ {
99
+ return foma_fsm_apply(self, string, &apply_up);
100
+ }
101
+
102
+ void Init_foma(void)
103
+ {
104
+ mFOMA = rb_define_module("FOMA");
105
+ mFSM = rb_define_class_under(mFOMA, "FSM", rb_cObject);
106
+ rb_define_alloc_func(mFSM, foma_fsm_alloc);
107
+ rb_define_method(mFSM, "initialize", foma_fsm_init, 1);
108
+ rb_define_method(mFSM, "apply_down", foma_fsm_apply_down, 1);
109
+ rb_define_method(mFSM, "apply_up", foma_fsm_apply_up, 1);
110
+ }
data/foma.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{foma}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Marius L. J\303\270hndal"]
12
+ s.date = %q{2011-10-05}
13
+ s.description = %q{A wrapper for the FOMA finite state library}
14
+ s.email = %q{mariuslj (at) ifi [dot] uio (dot) no}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ "CHANGELOG",
20
+ "ext/foma.c",
21
+ "foma.gemspec",
22
+ "test/test_foma.rb"
23
+ ]
24
+ s.homepage = %q{http://github.com/mlj/ruby-foma}
25
+ s.require_paths = ["lib"]
26
+ s.rubyforge_project = %q{foma}
27
+ s.rubygems_version = %q{1.3.7}
28
+ s.summary = %q{FOMA finite state library interface}
29
+
30
+ if s.respond_to? :specification_version then
31
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
32
+ s.specification_version = 3
33
+
34
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
35
+ else
36
+ end
37
+ else
38
+ end
39
+ end
40
+
data/test/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.a
Binary file
data/test/test_foma.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'foma'
2
+ require 'test/unit'
3
+
4
+ TEST_DIRECTORY = File.expand_path(File.dirname(__FILE__))
5
+ TEST_FSM = File.join(TEST_DIRECTORY, 'test_foma.bin')
6
+
7
+ class InitTestCase < Test::Unit::TestCase
8
+ def test_foma_init
9
+ fsm = FOMA::FSM.new(TEST_FSM)
10
+ end
11
+
12
+ def test_foma_acceptance
13
+ fsm = FOMA::FSM.new(TEST_FSM)
14
+
15
+ assert fsm.apply_up("amandum")
16
+ assert !fsm.apply_up("amanderxgtyvnuij")
17
+ assert fsm.apply_down("amō+VERB+ger+acc")
18
+ assert !fsm.apply_up("amō+VERB+ger+foobar")
19
+ end
20
+
21
+ def test_foma_analyses
22
+ fsm = FOMA::FSM.new(TEST_FSM)
23
+
24
+ analyses = []
25
+
26
+ fsm.apply_up("amandum") do |analysis|
27
+ analyses << analysis
28
+ end
29
+
30
+ assert_equal 3, analyses.count
31
+ assert_equal "amō+VERB+gdv+masc+sg+acc", analyses.sort.first
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foma
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - "Marius L. J\xC3\xB8hndal"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-05 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A wrapper for the FOMA finite state library
23
+ email: mariuslj (at) ifi [dot] uio (dot) no
24
+ executables: []
25
+
26
+ extensions:
27
+ - ext/extconf.rb
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - CHANGELOG
32
+ - Rakefile
33
+ - VERSION
34
+ - ext/extconf.rb
35
+ - ext/foma.c
36
+ - foma.gemspec
37
+ - test/.gitignore
38
+ - test/test_foma.bin
39
+ - test/test_foma.rb
40
+ - README.rdoc
41
+ has_rdoc: true
42
+ homepage: http://github.com/mlj/ruby-foma
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: foma
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: FOMA finite state library interface
75
+ test_files: []
76
+