treat 1.2.0 → 2.0.0rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +2 -2
- data/README.md +12 -21
- data/lib/treat/autoload.rb +44 -0
- data/lib/treat/config/config.rb +38 -0
- data/lib/treat/config/configurable.rb +51 -0
- data/lib/treat/config/data/config.rb +50 -0
- data/lib/treat/config/data/core.rb +52 -0
- data/lib/treat/config/data/databases.rb +10 -0
- data/lib/treat/config/data/entities.rb +15 -0
- data/lib/treat/config/data/languages/agnostic.rb +31 -0
- data/lib/treat/config/{languages → data/languages}/arabic.rb +0 -0
- data/lib/treat/config/{languages → data/languages}/chinese.rb +0 -0
- data/lib/treat/config/{languages → data/languages}/dutch.rb +1 -1
- data/lib/treat/config/data/languages/english.rb +95 -0
- data/lib/treat/config/data/languages/french.rb +148 -0
- data/lib/treat/config/data/languages/german.rb +135 -0
- data/lib/treat/config/{languages → data/languages}/greek.rb +1 -1
- data/lib/treat/config/data/languages/italian.rb +162 -0
- data/lib/treat/config/data/languages/polish.rb +11 -0
- data/lib/treat/config/{languages → data/languages}/portuguese.rb +1 -1
- data/lib/treat/config/{languages → data/languages}/russian.rb +1 -1
- data/lib/treat/config/data/languages/spanish.rb +291 -0
- data/lib/treat/config/data/languages/swedish.rb +289 -0
- data/lib/treat/config/data/libraries.rb +12 -0
- data/lib/treat/config/data/linguistics.rb +44 -0
- data/lib/treat/config/data/tags.rb +328 -0
- data/lib/treat/config/{workers → data/workers}/extractors.rb +2 -10
- data/lib/treat/config/{workers → data/workers}/formatters.rb +0 -0
- data/lib/treat/config/{workers → data/workers}/inflectors.rb +0 -0
- data/lib/treat/config/{workers → data/workers}/learners.rb +0 -0
- data/lib/treat/config/{workers → data/workers}/lexicalizers.rb +4 -3
- data/lib/treat/config/{workers → data/workers}/processors.rb +3 -3
- data/lib/treat/config/{workers → data/workers}/retrievers.rb +0 -0
- data/lib/treat/config/importable.rb +31 -0
- data/lib/treat/config/paths.rb +23 -0
- data/lib/treat/config/tags.rb +37 -0
- data/lib/treat/core/dsl.rb +55 -0
- data/lib/treat/{installer.rb → core/installer.rb} +10 -12
- data/lib/treat/core/server.rb +40 -0
- data/lib/treat/entities/entities.rb +101 -0
- data/lib/treat/entities/{abilities/doable.rb → entity/applicable.rb} +5 -3
- data/lib/treat/entities/{abilities → entity}/buildable.rb +118 -63
- data/lib/treat/entities/{abilities → entity}/checkable.rb +2 -2
- data/lib/treat/entities/{abilities → entity}/comparable.rb +6 -6
- data/lib/treat/entities/{abilities → entity}/countable.rb +2 -1
- data/lib/treat/entities/entity/debuggable.rb +86 -0
- data/lib/treat/entities/{abilities → entity}/delegatable.rb +16 -26
- data/lib/treat/entities/{abilities → entity}/exportable.rb +2 -2
- data/lib/treat/entities/{abilities → entity}/iterable.rb +4 -16
- data/lib/treat/entities/{abilities → entity}/magical.rb +22 -17
- data/lib/treat/entities/entity/registrable.rb +36 -0
- data/lib/treat/entities/{abilities → entity}/stringable.rb +18 -15
- data/lib/treat/entities/entity.rb +86 -77
- data/lib/treat/exception.rb +3 -0
- data/lib/treat/helpers/hash.rb +29 -0
- data/lib/treat/helpers/help.rb +35 -0
- data/lib/treat/helpers/object.rb +55 -0
- data/lib/treat/helpers/string.rb +124 -0
- data/lib/treat/{core → learning}/data_set.rb +11 -11
- data/lib/treat/{core → learning}/export.rb +3 -3
- data/lib/treat/{core → learning}/problem.rb +26 -16
- data/lib/treat/{core → learning}/question.rb +5 -9
- data/lib/treat/loaders/linguistics.rb +8 -9
- data/lib/treat/loaders/stanford.rb +5 -11
- data/lib/treat/modules.rb +33 -0
- data/lib/treat/proxies/array.rb +27 -0
- data/lib/treat/proxies/language.rb +47 -0
- data/lib/treat/proxies/number.rb +18 -0
- data/lib/treat/proxies/proxy.rb +25 -0
- data/lib/treat/proxies/string.rb +18 -0
- data/lib/treat/version.rb +10 -1
- data/lib/treat/{workers.rb → workers/categorizable.rb} +18 -19
- data/lib/treat/workers/extractors/keywords/tf_idf.rb +11 -11
- data/lib/treat/workers/extractors/language/what_language.rb +8 -6
- data/lib/treat/workers/extractors/name_tag/stanford.rb +10 -4
- data/lib/treat/workers/extractors/similarity/levenshtein.rb +36 -0
- data/lib/treat/workers/extractors/similarity/tf_idf.rb +27 -0
- data/lib/treat/workers/extractors/tf_idf/native.rb +4 -4
- data/lib/treat/workers/extractors/time/chronic.rb +2 -4
- data/lib/treat/workers/extractors/time/nickel.rb +19 -20
- data/lib/treat/workers/extractors/time/ruby.rb +2 -1
- data/lib/treat/workers/extractors/topic_words/lda.rb +12 -12
- data/lib/treat/workers/extractors/topics/reuters.rb +9 -13
- data/lib/treat/workers/formatters/readers/autoselect.rb +1 -1
- data/lib/treat/workers/formatters/readers/image.rb +19 -9
- data/lib/treat/workers/formatters/readers/odt.rb +2 -1
- data/lib/treat/workers/formatters/readers/pdf.rb +20 -3
- data/lib/treat/workers/formatters/readers/xml.rb +0 -1
- data/lib/treat/workers/formatters/serializers/mongo.rb +10 -20
- data/lib/treat/workers/formatters/serializers/xml.rb +17 -26
- data/lib/treat/workers/formatters/serializers/yaml.rb +5 -4
- data/lib/treat/workers/formatters/unserializers/mongo.rb +4 -4
- data/lib/treat/workers/formatters/unserializers/xml.rb +3 -4
- data/lib/treat/workers/formatters/unserializers/yaml.rb +3 -4
- data/lib/treat/workers/formatters/visualizers/dot.rb +1 -0
- data/lib/treat/workers/formatters/visualizers/standoff.rb +2 -3
- data/lib/treat/workers/formatters/visualizers/tree.rb +2 -3
- data/lib/treat/workers/{group.rb → groupable.rb} +9 -9
- data/lib/treat/workers/inflectors/cardinalizers/linguistics.rb +1 -3
- data/lib/treat/workers/inflectors/conjugators/linguistics.rb +5 -7
- data/lib/treat/workers/inflectors/declensors/english.rb +13 -20
- data/lib/treat/workers/inflectors/declensors/linguistics.rb +29 -28
- data/lib/treat/workers/inflectors/ordinalizers/linguistics.rb +0 -2
- data/lib/treat/workers/inflectors/stemmers/porter.rb +8 -10
- data/lib/treat/workers/inflectors/stemmers/porter_c.rb +7 -7
- data/lib/treat/workers/inflectors/stemmers/uea.rb +3 -8
- data/lib/treat/workers/learners/classifiers/id3.rb +17 -14
- data/lib/treat/workers/learners/classifiers/linear.rb +15 -27
- data/lib/treat/workers/learners/classifiers/mlp.rb +32 -19
- data/lib/treat/workers/learners/classifiers/svm.rb +28 -21
- data/lib/treat/workers/lexicalizers/categorizers/from_tag.rb +19 -3
- data/lib/treat/workers/lexicalizers/sensers/wordnet.rb +15 -7
- data/lib/treat/workers/lexicalizers/taggers/brill/patch.rb +4 -1
- data/lib/treat/workers/lexicalizers/taggers/brill.rb +8 -19
- data/lib/treat/workers/lexicalizers/taggers/lingua.rb +4 -15
- data/lib/treat/workers/lexicalizers/taggers/stanford.rb +22 -13
- data/lib/treat/workers/processors/chunkers/autoselect.rb +2 -3
- data/lib/treat/workers/processors/chunkers/html.rb +1 -6
- data/lib/treat/workers/processors/parsers/enju.rb +2 -4
- data/lib/treat/workers/processors/parsers/stanford.rb +13 -7
- data/lib/treat/workers/processors/segmenters/punkt.rb +25 -11
- data/lib/treat/workers/processors/segmenters/scalpel.rb +20 -0
- data/lib/treat/workers/processors/segmenters/srx.rb +42 -0
- data/lib/treat/workers/processors/segmenters/stanford.rb +5 -5
- data/lib/treat/workers/processors/segmenters/tactful.rb +21 -11
- data/lib/treat/workers/processors/tokenizers/ptb.rb +40 -30
- data/lib/treat/workers/processors/tokenizers/punkt.rb +14 -19
- data/lib/treat/workers/processors/tokenizers/stanford.rb +38 -22
- data/lib/treat/workers/retrievers/indexers/ferret.rb +6 -3
- data/lib/treat/workers/retrievers/searchers/ferret.rb +2 -2
- data/lib/treat/workers/workers.rb +6 -0
- data/lib/treat.rb +18 -32
- data/models/MANIFEST +1 -0
- data/spec/core/data_set.rb +174 -0
- data/spec/core/export.rb +52 -0
- data/spec/core/problem.rb +144 -0
- data/spec/core/question.rb +52 -0
- data/spec/{collection.rb → entities/collection.rb} +20 -35
- data/spec/{document.rb → entities/document.rb} +3 -54
- data/spec/{entity.rb → entities/entity.rb} +10 -9
- data/spec/entities/phrase.rb +33 -0
- data/spec/{token.rb → entities/token.rb} +0 -57
- data/spec/entities/word.rb +3 -0
- data/spec/{zone.rb → entities/zone.rb} +0 -26
- data/spec/helper.rb +116 -32
- data/spec/sandbox.rb +258 -25
- data/spec/treat.rb +26 -34
- data/spec/workers/agnostic.rb +137 -0
- data/spec/workers/english.rb +194 -0
- data/spec/workers/examples/english/economist/hungarys_troubles.txt +46 -0
- data/spec/workers/examples/english/economist/saving_the_euro.odt +0 -0
- data/spec/{samples → workers/examples/english}/mathematicians/archimedes.abw +0 -0
- data/spec/{samples → workers/examples/english}/mathematicians/euler.html +0 -0
- data/spec/{samples → workers/examples/english}/mathematicians/gauss.pdf +0 -0
- data/spec/{samples → workers/examples/english}/mathematicians/leibniz.txt +0 -0
- data/spec/{samples → workers/examples/english}/mathematicians/newton.doc +0 -0
- data/spec/workers/examples/english/phrase.xml +5 -0
- data/spec/workers/examples/english/test.txt +1 -0
- data/spec/workers/language.rb +280 -0
- data/spec/workers.rb +28 -0
- metadata +122 -105
- data/lib/treat/config/core/acronyms.rb +0 -5
- data/lib/treat/config/core/encodings.rb +0 -8
- data/lib/treat/config/core/entities.rb +0 -2
- data/lib/treat/config/core/language.rb +0 -3
- data/lib/treat/config/core/paths.rb +0 -8
- data/lib/treat/config/core/syntax.rb +0 -1
- data/lib/treat/config/core/verbosity.rb +0 -1
- data/lib/treat/config/databases/default.rb +0 -1
- data/lib/treat/config/databases/mongo.rb +0 -1
- data/lib/treat/config/languages/agnostic.rb +0 -34
- data/lib/treat/config/languages/english.rb +0 -60
- data/lib/treat/config/languages/french.rb +0 -18
- data/lib/treat/config/languages/german.rb +0 -18
- data/lib/treat/config/languages/italian.rb +0 -12
- data/lib/treat/config/languages/polish.rb +0 -12
- data/lib/treat/config/languages/spanish.rb +0 -12
- data/lib/treat/config/languages/swedish.rb +0 -12
- data/lib/treat/config/libraries/punkt.rb +0 -1
- data/lib/treat/config/libraries/reuters.rb +0 -1
- data/lib/treat/config/libraries/stanford.rb +0 -1
- data/lib/treat/config/linguistics/categories.rb +0 -4
- data/lib/treat/config/linguistics/punctuation.rb +0 -33
- data/lib/treat/config/tags/aligned.rb +0 -221
- data/lib/treat/config/tags/enju.rb +0 -71
- data/lib/treat/config/tags/paris7.rb +0 -17
- data/lib/treat/config/tags/ptb.rb +0 -15
- data/lib/treat/config/workers/list.rb +0 -1
- data/lib/treat/config.rb +0 -135
- data/lib/treat/core.rb +0 -5
- data/lib/treat/entities/abilities/copyable.rb +0 -47
- data/lib/treat/entities/abilities/debuggable.rb +0 -83
- data/lib/treat/entities/abilities/registrable.rb +0 -46
- data/lib/treat/entities/collection.rb +0 -40
- data/lib/treat/entities/document.rb +0 -10
- data/lib/treat/entities/group.rb +0 -18
- data/lib/treat/entities/section.rb +0 -13
- data/lib/treat/entities/token.rb +0 -47
- data/lib/treat/entities/zone.rb +0 -12
- data/lib/treat/entities.rb +0 -6
- data/lib/treat/helpers/didyoumean.rb +0 -57
- data/lib/treat/helpers/escaping.rb +0 -15
- data/lib/treat/helpers/formatting.rb +0 -41
- data/lib/treat/helpers/objtohash.rb +0 -8
- data/lib/treat/helpers/platform.rb +0 -15
- data/lib/treat/helpers/reflection.rb +0 -17
- data/lib/treat/helpers/temporary.rb +0 -27
- data/lib/treat/helpers/verbosity.rb +0 -19
- data/lib/treat/helpers.rb +0 -5
- data/lib/treat/loaders.rb +0 -10
- data/lib/treat/proxies.rb +0 -106
- data/lib/treat/workers/formatters/unserializers/autoselect.rb +0 -17
- data/lib/treat/workers/inflectors/declensors/active_support.rb +0 -31
- data/lib/treat/workers/processors/tokenizers/tactful.rb +0 -68
- data/spec/core.rb +0 -441
- data/spec/phrase.rb +0 -112
- data/spec/word.rb +0 -111
data/spec/core.rb
DELETED
@@ -1,441 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
describe Treat::Core::Question do
|
4
|
-
|
5
|
-
describe "#initialize" do
|
6
|
-
context "when supplied with acceptable parameters" do
|
7
|
-
it "should give access to the parameters" do
|
8
|
-
question = Treat::Core::Question.new(
|
9
|
-
:is_keyword, :word, :continuous, 0, [0, 1])
|
10
|
-
question.name.should eql :is_keyword
|
11
|
-
question.target.should eql :word
|
12
|
-
question.type.should eql :continuous
|
13
|
-
question.default.should eql 0
|
14
|
-
question.labels.should eql [0, 1]
|
15
|
-
end
|
16
|
-
end
|
17
|
-
context "when supplied with wrong parameters" do
|
18
|
-
it "should raise an exception" do
|
19
|
-
# Name should be a symbol
|
20
|
-
expect { Treat::Core::Question.new(
|
21
|
-
nil, :sentence) }.to raise_error
|
22
|
-
# Target should be an actual entity type
|
23
|
-
expect { Treat::Core::Question.new(
|
24
|
-
:name, :foo) }.to raise_error
|
25
|
-
# Distribution type should be continuous or discrete
|
26
|
-
expect { Treat::Core::Question.new(
|
27
|
-
:name, :sentence, :nonsense) }.to raise_error
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "#==(question)" do
|
33
|
-
context "when supplied with an equal question" do
|
34
|
-
it "should return true" do
|
35
|
-
Treat::Core::Question.new(
|
36
|
-
:is_keyword, :word).
|
37
|
-
should == Treat::Core::Question.new(
|
38
|
-
:is_keyword, :word)
|
39
|
-
Treat::Core::Question.new(
|
40
|
-
:is_keyword, :word, :continuous).
|
41
|
-
should == Treat::Core::Question.new(
|
42
|
-
:is_keyword, :word, :continuous)
|
43
|
-
Treat::Core::Question.new(
|
44
|
-
:is_keyword, :word, :continuous, [0, 1]).
|
45
|
-
should == Treat::Core::Question.new(
|
46
|
-
:is_keyword, :word, :continuous, [0, 1])
|
47
|
-
end
|
48
|
-
end
|
49
|
-
context "when supplied with a different question" do
|
50
|
-
it "should return false" do
|
51
|
-
Treat::Core::Question.new(
|
52
|
-
:is_keyword, :word).
|
53
|
-
should_not == Treat::Core::Question.new(
|
54
|
-
:is_keyword, :sentence)
|
55
|
-
Treat::Core::Question.new(
|
56
|
-
:is_keyword, :word, :continuous).
|
57
|
-
should_not == Treat::Core::Question.new(
|
58
|
-
:is_keyword, :word, :discrete)
|
59
|
-
Treat::Core::Question.new(
|
60
|
-
:is_keyword, :word, :continuous, [0, 1]).
|
61
|
-
should_not == Treat::Core::Question.new(
|
62
|
-
:is_keyword, :word, :continuous, [1, 0])
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
describe Treat::Core::Export do
|
70
|
-
|
71
|
-
describe "#initialize" do
|
72
|
-
context "when supplied with acceptable parameters" do
|
73
|
-
it "should give access to the parameters" do
|
74
|
-
export = Treat::Core::Export.new(:name, 0, "->(e) { e }")
|
75
|
-
export.name.should eql :name
|
76
|
-
export.default.should eql 0
|
77
|
-
export.proc_string.should eql "->(e) { e }"
|
78
|
-
export.proc.should be_instance_of Proc
|
79
|
-
export.proc.call('x').should eql 'x'
|
80
|
-
end
|
81
|
-
end
|
82
|
-
context "when supplied with wrong parameters" do
|
83
|
-
it "should raise an exception" do
|
84
|
-
# First argument should be a symbol representing the name of the export.
|
85
|
-
expect { Treat::Core::Export.new(nil) }.to raise_error
|
86
|
-
# Third argument, if supplied, should be a string that
|
87
|
-
# evaluates to a proc (NOT a proc/lambda).
|
88
|
-
expect { Treat::Core::Export.new(:name, 0, lambda { x } ) }.to raise_error
|
89
|
-
# Third argument should be proper ruby syntax.
|
90
|
-
expect { Treat::Core::Export.new(:name, 0, "->(e) { ") }.to raise_error
|
91
|
-
# Third argument should evaluate to a proc.
|
92
|
-
expect { Treat::Core::Export.new(:name, 0, "2") }.to raise_error
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
describe "#==(question)" do
|
98
|
-
context "when supplied with an equal question" do
|
99
|
-
it "should return true" do
|
100
|
-
Treat::Core::Export.new(:name).
|
101
|
-
should == Treat::Core::Export.new(:name)
|
102
|
-
Treat::Core::Export.new(:name, 0).
|
103
|
-
should == Treat::Core::Export.new(:name, 0)
|
104
|
-
Treat::Core::Export.new(:name, 0, "->(e) { }").
|
105
|
-
should == Treat::Core::Export.new(:name, 0, "->(e) { }")
|
106
|
-
end
|
107
|
-
end
|
108
|
-
context "when supplied with a different question" do
|
109
|
-
it "should return false" do
|
110
|
-
Treat::Core::Export.new(:name).
|
111
|
-
should_not == Treat::Core::Export.new(:name2)
|
112
|
-
Treat::Core::Export.new(:name, 0).
|
113
|
-
should_not == Treat::Core::Export.new(:name, 1)
|
114
|
-
Treat::Core::Export.new(:name, 0, "->(e) { }").
|
115
|
-
should_not == Treat::Core::Export.new(:name, 0, "->(e) { x }")
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
end
|
121
|
-
|
122
|
-
describe Treat::Core::DataSet do
|
123
|
-
|
124
|
-
before do
|
125
|
-
@question = Treat::Core::Question.new(:is_key_sentence, :sentence, :continuous, 0, [0, 1])
|
126
|
-
@feature = Treat::Core::Feature.new(:word_count, 0)
|
127
|
-
@problem = Treat::Core::Problem.new(@question, @feature)
|
128
|
-
@tag = Treat::Core::Tag.new(:paragraph_length, 0,
|
129
|
-
"->(e) { e.parent_paragraph.word_count }")
|
130
|
-
@paragraph = Treat::Entities::Paragraph.new(
|
131
|
-
"Ranga and I went to the store. Meanwhile, Ryan was sleeping.")
|
132
|
-
@paragraph.do :segment, :tokenize
|
133
|
-
@sentence = @paragraph.sentences[0]
|
134
|
-
@data_set = Treat::Core::DataSet.new(@problem)
|
135
|
-
end
|
136
|
-
|
137
|
-
describe "#initialize" do
|
138
|
-
context "when supplied with a problem" do
|
139
|
-
it "should initialize an empty data set" do
|
140
|
-
data_set = Treat::Core::DataSet.new(@problem)
|
141
|
-
data_set.items.should eql []
|
142
|
-
data_set.problem.should eql @problem
|
143
|
-
end
|
144
|
-
end
|
145
|
-
context "when supplied with an improper argument" do
|
146
|
-
it "should raise an error" do
|
147
|
-
# The argument to initialize should be a Problem.
|
148
|
-
expect { data_set = Treat::Core::DataSet.new("foo") }.to raise_error
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
describe "#self.build" do
|
154
|
-
|
155
|
-
end
|
156
|
-
|
157
|
-
describe "#==(other_data_set)" do
|
158
|
-
context "when supplied with an equivalent data set" do
|
159
|
-
it "returns true" do
|
160
|
-
data_set1 = Treat::Core::DataSet.new(@problem)
|
161
|
-
data_set2 = Treat::Core::DataSet.new(@problem)
|
162
|
-
data_set1.should == data_set2
|
163
|
-
data_set1 << @sentence
|
164
|
-
data_set2 << @sentence
|
165
|
-
data_set1.should == data_set2
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
context "when supplied with a non-equivalent data set" do
|
170
|
-
it "returns false" do
|
171
|
-
# Get two slightly different problems.
|
172
|
-
question1 = Treat::Core::Question.new(
|
173
|
-
:is_key_sentence, :sentence, :continuous, 0, [0, 1])
|
174
|
-
question2 = Treat::Core::Question.new(
|
175
|
-
:is_key_word, :sentence, :continuous, 0, [1, 1])
|
176
|
-
problem1 = Treat::Core::Problem.new(question1, @feature)
|
177
|
-
problem2 = Treat::Core::Problem.new(question2, @feature)
|
178
|
-
# Then the problems shouldn't be equal anymore.
|
179
|
-
problem1.should_not == problem2
|
180
|
-
# Create data sets with the different problems.
|
181
|
-
data_set1 = Treat::Core::DataSet.new(problem1)
|
182
|
-
data_set2 = Treat::Core::DataSet.new(problem2)
|
183
|
-
# Then the data sets shouldn't be equal anymore.
|
184
|
-
data_set1.should_not == data_set2
|
185
|
-
# Create two data sets with the same problems.
|
186
|
-
data_set1 = Treat::Core::DataSet.new(@problem)
|
187
|
-
data_set2 = Treat::Core::DataSet.new(@problem)
|
188
|
-
# Then these should be equal.
|
189
|
-
data_set1.should == data_set2
|
190
|
-
# But when different items are added
|
191
|
-
data_set1 << Treat::Entities::Sentence.new(
|
192
|
-
"This sentence is not the same as the other.").tokenize
|
193
|
-
data_set2 << Treat::Entities::Sentence.new(
|
194
|
-
"This sentence is similar to the other.").tokenize
|
195
|
-
# They shouldn't be equal anymore.
|
196
|
-
data_set1.should_not == data_set2
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
end
|
201
|
-
|
202
|
-
describe "#merge" do
|
203
|
-
context "when supplied with two data sets refering to the same problem" do
|
204
|
-
it "merges the two together" do
|
205
|
-
# Create two data sets with the same problem.
|
206
|
-
data_set1 = Treat::Core::DataSet.new(@problem)
|
207
|
-
data_set2 = Treat::Core::DataSet.new(@problem)
|
208
|
-
# Add a sentence to each data set.
|
209
|
-
data_set1 << Treat::Entities::Sentence.new(
|
210
|
-
"This sentence is not the same as the other.").tokenize
|
211
|
-
data_set2 << Treat::Entities::Sentence.new(
|
212
|
-
"This sentence is similar to the other.").tokenize
|
213
|
-
# Merge the two data sets together.
|
214
|
-
data_set1.merge(data_set2)
|
215
|
-
# Check if the merge has occured properly.
|
216
|
-
data_set1.items.size.should eql 2
|
217
|
-
data_set1.items[1].should eql data_set2.items[0]
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
context "when supplied with two data sets refering to different problems" do
|
222
|
-
it "raises an error" do
|
223
|
-
# Get two slightly different questions.
|
224
|
-
question1 = Treat::Core::Question.new(
|
225
|
-
:is_key_sentence, :sentence, :continuous, 0, [0, 1])
|
226
|
-
question2 = Treat::Core::Question.new(
|
227
|
-
:is_key_word, :sentence, :continuous, 0, [1, 1])
|
228
|
-
# Create two problems with the different questions.
|
229
|
-
problem1 = Treat::Core::Problem.new(question1, @feature)
|
230
|
-
problem2 = Treat::Core::Problem.new(question2, @feature)
|
231
|
-
# Create two data sets with the different problems.
|
232
|
-
data_set1 = Treat::Core::DataSet.new(problem1)
|
233
|
-
data_set2 = Treat::Core::DataSet.new(problem2)
|
234
|
-
# Add elements to each of the data sets.
|
235
|
-
data_set1 << Treat::Entities::Sentence.new(
|
236
|
-
"This sentence is not the same as the other.").tokenize
|
237
|
-
data_set2 << Treat::Entities::Sentence.new(
|
238
|
-
"This sentence is similar to the other.").tokenize
|
239
|
-
# Try to merge them; but this should fail.
|
240
|
-
expect { data_set1.merge(data_set2) }.to raise_error
|
241
|
-
end
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
describe "#<<(entity)" do
|
246
|
-
context "when supplied with a proper entity" do
|
247
|
-
it "exports the features and tags and adds them to the data set" do
|
248
|
-
problem = Treat::Core::Problem.new(@question, @feature, @tag)
|
249
|
-
data_set = Treat::Core::DataSet.new(problem)
|
250
|
-
data_set << @sentence
|
251
|
-
data_set.items.tap { |e| e[0][:id] = 0 }.
|
252
|
-
should eql [{:tags=>[11], :features=>[7, 0], :id=>0}]
|
253
|
-
end
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
describe "#serialize" do
|
258
|
-
context "when asked to use a given adapter" do
|
259
|
-
it "calls the corresponding #to_something method" do
|
260
|
-
|
261
|
-
end
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
describe "#to_marshal, #self.from_marshal" do
|
266
|
-
context "when asked to successively serialize and deserialize data" do
|
267
|
-
it "completes a round trip without losing information" do
|
268
|
-
problem = Treat::Core::Problem.new(@question, @feature, @tag)
|
269
|
-
data_set = Treat::Core::DataSet.new(problem)
|
270
|
-
data_set << @sentence
|
271
|
-
data_set.to_marshal(file: 'test.dump')
|
272
|
-
Treat::Core::DataSet.from_marshal(
|
273
|
-
file: 'test.dump').should == data_set
|
274
|
-
FileUtils.rm('test.dump')
|
275
|
-
end
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
describe "#to_mongo" do
|
280
|
-
|
281
|
-
end
|
282
|
-
|
283
|
-
describe "#self.unserialize" do
|
284
|
-
context "when asked to use a given adapter" do
|
285
|
-
it "calls the corresponding #to_something method" do
|
286
|
-
|
287
|
-
end
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
describe "#self.from_mongo" do
|
292
|
-
|
293
|
-
end
|
294
|
-
|
295
|
-
end
|
296
|
-
|
297
|
-
describe Treat::Core::Problem do
|
298
|
-
|
299
|
-
before do
|
300
|
-
@question = Treat::Core::Question.new(:is_key_sentence,
|
301
|
-
:sentence, :continuous, 0, [0, 1])
|
302
|
-
@feature = Treat::Core::Feature.new(:word_count, 0)
|
303
|
-
@tag = Treat::Core::Tag.new(:paragraph_length, 0,
|
304
|
-
"->(e) { e.parent_paragraph.word_count }")
|
305
|
-
@paragraph = Treat::Entities::Paragraph.new(
|
306
|
-
"Ranga and I went to the store. Meanwhile, Ryan was sleeping.")
|
307
|
-
@paragraph.do :segment, :tokenize
|
308
|
-
@sentence = @paragraph.sentences[0]
|
309
|
-
@hash = {"question"=>{"name"=>:is_key_sentence, "target"=>:sentence,
|
310
|
-
"type"=>:continuous, "default"=>0, "labels"=>[0, 1]}, "features"=>[
|
311
|
-
{"proc_string"=>nil, "default"=>0, "name"=>:word_count, "proc"=>nil}],
|
312
|
-
"tags"=>[{"proc_string"=>"->(e) { e.parent_paragraph.word_count }",
|
313
|
-
"default"=>0, "name"=>:paragraph_length, "proc"=>nil}], "id"=>0}
|
314
|
-
end
|
315
|
-
|
316
|
-
describe "#initialize" do
|
317
|
-
context "when supplied with proper arguments" do
|
318
|
-
it "initializes the problem and gives access to parameters" do
|
319
|
-
problem = Treat::Core::Problem.new(@question, @feature, @tag)
|
320
|
-
problem.question.should eql @question
|
321
|
-
problem.features.should eql [@feature]
|
322
|
-
problem.tags.should eql [@tag]
|
323
|
-
problem.feature_labels.should eql [@feature.name]
|
324
|
-
problem.tag_labels.should eql [@tag.name]
|
325
|
-
# ID ??? FIXME
|
326
|
-
end
|
327
|
-
end
|
328
|
-
context "when supplied with unacceptable arguments" do
|
329
|
-
it "raises an error" do
|
330
|
-
# First argument should be instance of Question.
|
331
|
-
expect { Treat::Core::Problem.new('foo') }.to raise_error
|
332
|
-
# Arguments >= 2 should be instances of Export.
|
333
|
-
expect { Treat::Core::Problem.new(@question, 'foo') }.to raise_error
|
334
|
-
# Should have at least one Feature in the arguments.
|
335
|
-
expect { Treat::Core::Problem.new(@question, @tag) }.to raise_error
|
336
|
-
end
|
337
|
-
end
|
338
|
-
end
|
339
|
-
|
340
|
-
describe "#==(problem)" do
|
341
|
-
context "when supplied with an equal problem" do
|
342
|
-
it "should return true" do
|
343
|
-
Treat::Core::Problem.new(@question, @feature).
|
344
|
-
should == Treat::Core::Problem.new(@question, @feature)
|
345
|
-
Treat::Core::Problem.new(@question, @feature, @tag).
|
346
|
-
should == Treat::Core::Problem.new(@question, @feature, @tag)
|
347
|
-
end
|
348
|
-
end
|
349
|
-
context "when supplied with a different question" do
|
350
|
-
it "should return false" do
|
351
|
-
question = Treat::Core::Question.new(:is_key_sentence, :sentence)
|
352
|
-
feature = Treat::Core::Feature.new(:word_count, 999)
|
353
|
-
tag = Treat::Core::Tag.new(:paragraph_length, 999)
|
354
|
-
Treat::Core::Problem.new(@question, @feature).
|
355
|
-
should_not == Treat::Core::Problem.new(question, @feature)
|
356
|
-
Treat::Core::Problem.new(@question, @feature).
|
357
|
-
should_not == Treat::Core::Problem.new(@question, feature)
|
358
|
-
Treat::Core::Problem.new(@question, @feature, @tag).
|
359
|
-
should_not == Treat::Core::Problem.new(@question, @feature, tag)
|
360
|
-
end
|
361
|
-
end
|
362
|
-
end
|
363
|
-
|
364
|
-
describe "#export_tags(entity)" do
|
365
|
-
context "when called on a problem that has tags" do
|
366
|
-
context "and called with an entity of the proper type" do
|
367
|
-
it "returns an array of the tags" do
|
368
|
-
problem = Treat::Core::Problem.new(@question, @feature, @tag)
|
369
|
-
problem.export_tags(@sentence).should eql [11]
|
370
|
-
end
|
371
|
-
end
|
372
|
-
end
|
373
|
-
context "when called on a problem that doesn't have tags" do
|
374
|
-
it "raises an error" do
|
375
|
-
problem = Treat::Core::Problem.new(@question, @feature)
|
376
|
-
expect { problem.export_tags(@sentence) }.to raise_error
|
377
|
-
end
|
378
|
-
end
|
379
|
-
end
|
380
|
-
|
381
|
-
describe "#export_features(entity, include_answer = true)" do
|
382
|
-
|
383
|
-
context "when called with an entity of the proper type" do
|
384
|
-
context "and include_answer is set to true" do
|
385
|
-
context "and the answer is already set on the entity" do
|
386
|
-
it "returns an array of the exported features, with the answer" do
|
387
|
-
problem = Treat::Core::Problem.new(@question, @feature)
|
388
|
-
@sentence.set :is_key_sentence, 1
|
389
|
-
problem.export_features(@sentence).should eql [7, 1]
|
390
|
-
end
|
391
|
-
end
|
392
|
-
context "and the answer is not already set on the entity" do
|
393
|
-
it "returns an array of the exported features, with the question's default answer" do
|
394
|
-
problem = Treat::Core::Problem.new(@question, @feature)
|
395
|
-
problem.export_features(@sentence).should eql [7, @question.default]
|
396
|
-
end
|
397
|
-
end
|
398
|
-
end
|
399
|
-
context "and include_answer is set to false" do
|
400
|
-
it "returns an array of the exported features, without the answer" do
|
401
|
-
problem = Treat::Core::Problem.new(@question, @feature)
|
402
|
-
problem.export_features(@sentence, false).should eql [7]
|
403
|
-
end
|
404
|
-
end
|
405
|
-
end
|
406
|
-
context "when supplied with an entity that is not of the proper type" do
|
407
|
-
it "raises an error" do
|
408
|
-
problem = Treat::Core::Problem.new(@question, @feature)
|
409
|
-
word = Treat::Entities::Word.new('test')
|
410
|
-
expect { problem.export_features(word) }.to raise_error
|
411
|
-
end
|
412
|
-
end
|
413
|
-
end
|
414
|
-
|
415
|
-
describe "#to_hash" do
|
416
|
-
context "when called on a problem" do
|
417
|
-
it "returns a hash describing the problem" do
|
418
|
-
Treat::Core::Problem.new(@question, @feature, @tag).
|
419
|
-
to_hash.tap { |e| e['id'] = 0 }.should eql @hash
|
420
|
-
end
|
421
|
-
end
|
422
|
-
end
|
423
|
-
|
424
|
-
describe "#self.from_hash" do
|
425
|
-
context "when called with a hash describing a problem" do
|
426
|
-
it "returns a problem based on the hash" do
|
427
|
-
problem = Treat::Core::Problem.from_hash(@hash)
|
428
|
-
problem.question.name.should eql :is_key_sentence
|
429
|
-
problem.question.target.should eql :sentence
|
430
|
-
problem.question.type.should eql :continuous
|
431
|
-
problem.question.default.should eql 0
|
432
|
-
problem.question.labels.should eql [0, 1]
|
433
|
-
problem.features[0].proc_string.should eql nil
|
434
|
-
problem.features[0].default.should eql 0
|
435
|
-
problem.features[0].name.should eql :word_count
|
436
|
-
problem.features[0].proc.should eql nil
|
437
|
-
end
|
438
|
-
end
|
439
|
-
end
|
440
|
-
|
441
|
-
end
|
data/spec/phrase.rb
DELETED
@@ -1,112 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
describe Treat::Entities::Phrase do
|
4
|
-
|
5
|
-
describe "Buildable" do
|
6
|
-
|
7
|
-
describe "#build" do
|
8
|
-
|
9
|
-
context "when supplied with a sentence" do
|
10
|
-
|
11
|
-
it "creates a sentence with the text" do
|
12
|
-
sentence = "This is a sentence."
|
13
|
-
s = Treat::Entities::Phrase.build(sentence)
|
14
|
-
s.type.should eql :sentence
|
15
|
-
s.to_s.should eql sentence
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
context "when supplied with a phrase" do
|
21
|
-
|
22
|
-
it "creates a phrase with the text" do
|
23
|
-
phrase = "this is a phrase"
|
24
|
-
p = Treat::Entities::Phrase.build(phrase)
|
25
|
-
p.type.should eql :phrase
|
26
|
-
p.to_s.should eql phrase
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
describe "Extractable" do
|
36
|
-
|
37
|
-
describe "#named_entity" do
|
38
|
-
it "tags the named entity words in the phrase" do
|
39
|
-
# Not implemented.
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe "#time" do
|
44
|
-
it "returns a DateTime object representing the time in the phrase" do
|
45
|
-
Treat.languages.english.workers.extractors.time.each do |e|
|
46
|
-
t = 'october 2006'.time(e)
|
47
|
-
t.month.should eql 10
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe "Processable" do
|
54
|
-
|
55
|
-
describe "#tokenize" do
|
56
|
-
|
57
|
-
it "splits a phrase/sentence into tokens and adds them as children of the phrase" do
|
58
|
-
Treat.languages.english.workers.processors.tokenizers.each do |t|
|
59
|
-
@phrase = Treat::Entities::Phrase.new('a phrase to tokenize')
|
60
|
-
@phrase.tokenize(t)
|
61
|
-
@phrase.children.should eql @phrase.tokens
|
62
|
-
@phrase.tokens.map { |t| t.to_s }.should
|
63
|
-
eql ['A', 'sentence', 'to', 'tokenize']
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
describe "#parse" do
|
70
|
-
|
71
|
-
it "parses a phrase/sentence into its syntax tree, " +
|
72
|
-
"adding nested phrases and tokens as children of the phrase/sentence" do
|
73
|
-
Treat.languages.english.workers.processors.parsers.each do |p|
|
74
|
-
next #f p == :enju # slow?
|
75
|
-
@sentence = Treat::Entities::
|
76
|
-
Sentence.new('A sentence to tokenize.')
|
77
|
-
@sentence.parse(p)
|
78
|
-
@sentence.phrases.map { |t| t.to_s }.should
|
79
|
-
eql ["A sentence to tokenize.",
|
80
|
-
"A sentence to tokenize.",
|
81
|
-
"A sentence", "to tokenize",
|
82
|
-
"tokenize"]
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
|
90
|
-
describe "Lexicalizable" do
|
91
|
-
|
92
|
-
before do
|
93
|
-
@taggers = Treat.languages.english.workers.lexicalizers.taggers
|
94
|
-
end
|
95
|
-
|
96
|
-
describe "#tag" do
|
97
|
-
|
98
|
-
context "when called on a phrase" do
|
99
|
-
it "returns the tag 'P'" do
|
100
|
-
@taggers.each do |t|
|
101
|
-
p = 'a phrase'
|
102
|
-
p.tag(t)
|
103
|
-
p.tag(t).should eql 'P'
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
data/spec/word.rb
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
describe Treat::Entities::Word do
|
4
|
-
|
5
|
-
describe "Inflectors" do
|
6
|
-
|
7
|
-
before do
|
8
|
-
@inflectors = Treat.languages.
|
9
|
-
english.workers.inflectors
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "#stem" do
|
13
|
-
|
14
|
-
it "returns the stem of the word" do
|
15
|
-
@inflectors.stemmers.each do |s|
|
16
|
-
'running'.stem(s).should eql 'run'
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "#infinitive" do
|
23
|
-
it "returns the infinitive form of a verb" do
|
24
|
-
@inflectors.conjugators.each do |c|
|
25
|
-
'running'.infinitive(c).should eql 'run'
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# Nil if not verb?
|
31
|
-
describe "#present_participle" do
|
32
|
-
it "returns the present participle form of a verb" do
|
33
|
-
@inflectors.conjugators.each do |c|
|
34
|
-
'running'.infinitive(c).should eql 'run'
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe "#plural" do
|
40
|
-
it "returns the plural form of the word" do
|
41
|
-
@inflectors.declensors.each do |i|
|
42
|
-
# 'inflection'.plural(i).should eql 'inflections'
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe "#singular" do
|
48
|
-
it "returns the singular form of the word" do
|
49
|
-
@inflectors.declensors.each do |i|
|
50
|
-
next if i == :linguistics # Fix this
|
51
|
-
# 'inflections'.singular(i).should eql 'inflections'
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
describe "#ordinal_form" do
|
57
|
-
it "returns the ordinal form of a number" do
|
58
|
-
@inflectors.cardinalizers.each do |o|
|
59
|
-
20.cardinal.should eql 'twenty'
|
60
|
-
end
|
61
|
-
@inflectors.ordinalizers.each do |o|
|
62
|
-
20.ordinal.should eql 'twentieth'
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
describe "Lexicalizable" do
|
70
|
-
|
71
|
-
describe "#synonyms" do
|
72
|
-
|
73
|
-
it "returns the synonyms of the word" do
|
74
|
-
# Should the word be included in synonyms?
|
75
|
-
'glass'.synonyms[-1].should eql 'looking_glass'
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
describe "#antonyms" do
|
81
|
-
it "returns the antonyms of the word" do
|
82
|
-
'glass'.antonyms.should eql []
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
describe "#hypernyms" do
|
87
|
-
it "returns the hypernyms of the word" do
|
88
|
-
'glass'.hypernyms[-1].should eql 'glasswork'
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe "#hyponyms" do
|
93
|
-
it "returns the hyponyms of the word" do
|
94
|
-
'glass'.hyponyms[-1].should eql 'wineglass'
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
end
|
99
|
-
|
100
|
-
describe "Extractable" do
|
101
|
-
describe "#tf_idf" do
|
102
|
-
it "returns the TF*IDF score of the word" do
|
103
|
-
#c = Treat::Entities::Collection.build(
|
104
|
-
#Treat.paths.spec + 'samples/mathematicians')
|
105
|
-
#c.do(:chunk, :segment, :tokenize)
|
106
|
-
#c.words[30].tf_idf.should eql 0.2231
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|