treat 1.2.0 → 2.0.0rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. data/LICENSE +2 -2
  2. data/README.md +12 -21
  3. data/lib/treat/autoload.rb +44 -0
  4. data/lib/treat/config/config.rb +38 -0
  5. data/lib/treat/config/configurable.rb +51 -0
  6. data/lib/treat/config/data/config.rb +50 -0
  7. data/lib/treat/config/data/core.rb +52 -0
  8. data/lib/treat/config/data/databases.rb +10 -0
  9. data/lib/treat/config/data/entities.rb +15 -0
  10. data/lib/treat/config/data/languages/agnostic.rb +31 -0
  11. data/lib/treat/config/{languages → data/languages}/arabic.rb +0 -0
  12. data/lib/treat/config/{languages → data/languages}/chinese.rb +0 -0
  13. data/lib/treat/config/{languages → data/languages}/dutch.rb +1 -1
  14. data/lib/treat/config/data/languages/english.rb +95 -0
  15. data/lib/treat/config/data/languages/french.rb +148 -0
  16. data/lib/treat/config/data/languages/german.rb +135 -0
  17. data/lib/treat/config/{languages → data/languages}/greek.rb +1 -1
  18. data/lib/treat/config/data/languages/italian.rb +162 -0
  19. data/lib/treat/config/data/languages/polish.rb +11 -0
  20. data/lib/treat/config/{languages → data/languages}/portuguese.rb +1 -1
  21. data/lib/treat/config/{languages → data/languages}/russian.rb +1 -1
  22. data/lib/treat/config/data/languages/spanish.rb +291 -0
  23. data/lib/treat/config/data/languages/swedish.rb +289 -0
  24. data/lib/treat/config/data/libraries.rb +12 -0
  25. data/lib/treat/config/data/linguistics.rb +44 -0
  26. data/lib/treat/config/data/tags.rb +328 -0
  27. data/lib/treat/config/{workers → data/workers}/extractors.rb +2 -10
  28. data/lib/treat/config/{workers → data/workers}/formatters.rb +0 -0
  29. data/lib/treat/config/{workers → data/workers}/inflectors.rb +0 -0
  30. data/lib/treat/config/{workers → data/workers}/learners.rb +0 -0
  31. data/lib/treat/config/{workers → data/workers}/lexicalizers.rb +4 -3
  32. data/lib/treat/config/{workers → data/workers}/processors.rb +3 -3
  33. data/lib/treat/config/{workers → data/workers}/retrievers.rb +0 -0
  34. data/lib/treat/config/importable.rb +31 -0
  35. data/lib/treat/config/paths.rb +23 -0
  36. data/lib/treat/config/tags.rb +37 -0
  37. data/lib/treat/core/dsl.rb +55 -0
  38. data/lib/treat/{installer.rb → core/installer.rb} +10 -12
  39. data/lib/treat/core/server.rb +40 -0
  40. data/lib/treat/entities/entities.rb +101 -0
  41. data/lib/treat/entities/{abilities/doable.rb → entity/applicable.rb} +5 -3
  42. data/lib/treat/entities/{abilities → entity}/buildable.rb +118 -63
  43. data/lib/treat/entities/{abilities → entity}/checkable.rb +2 -2
  44. data/lib/treat/entities/{abilities → entity}/comparable.rb +6 -6
  45. data/lib/treat/entities/{abilities → entity}/countable.rb +2 -1
  46. data/lib/treat/entities/entity/debuggable.rb +86 -0
  47. data/lib/treat/entities/{abilities → entity}/delegatable.rb +16 -26
  48. data/lib/treat/entities/{abilities → entity}/exportable.rb +2 -2
  49. data/lib/treat/entities/{abilities → entity}/iterable.rb +4 -16
  50. data/lib/treat/entities/{abilities → entity}/magical.rb +22 -17
  51. data/lib/treat/entities/entity/registrable.rb +36 -0
  52. data/lib/treat/entities/{abilities → entity}/stringable.rb +18 -15
  53. data/lib/treat/entities/entity.rb +86 -77
  54. data/lib/treat/exception.rb +3 -0
  55. data/lib/treat/helpers/hash.rb +29 -0
  56. data/lib/treat/helpers/help.rb +35 -0
  57. data/lib/treat/helpers/object.rb +55 -0
  58. data/lib/treat/helpers/string.rb +124 -0
  59. data/lib/treat/{core → learning}/data_set.rb +11 -11
  60. data/lib/treat/{core → learning}/export.rb +3 -3
  61. data/lib/treat/{core → learning}/problem.rb +26 -16
  62. data/lib/treat/{core → learning}/question.rb +5 -9
  63. data/lib/treat/loaders/linguistics.rb +8 -9
  64. data/lib/treat/loaders/stanford.rb +5 -11
  65. data/lib/treat/modules.rb +33 -0
  66. data/lib/treat/proxies/array.rb +27 -0
  67. data/lib/treat/proxies/language.rb +47 -0
  68. data/lib/treat/proxies/number.rb +18 -0
  69. data/lib/treat/proxies/proxy.rb +25 -0
  70. data/lib/treat/proxies/string.rb +18 -0
  71. data/lib/treat/version.rb +10 -1
  72. data/lib/treat/{workers.rb → workers/categorizable.rb} +18 -19
  73. data/lib/treat/workers/extractors/keywords/tf_idf.rb +11 -11
  74. data/lib/treat/workers/extractors/language/what_language.rb +8 -6
  75. data/lib/treat/workers/extractors/name_tag/stanford.rb +10 -4
  76. data/lib/treat/workers/extractors/similarity/levenshtein.rb +36 -0
  77. data/lib/treat/workers/extractors/similarity/tf_idf.rb +27 -0
  78. data/lib/treat/workers/extractors/tf_idf/native.rb +4 -4
  79. data/lib/treat/workers/extractors/time/chronic.rb +2 -4
  80. data/lib/treat/workers/extractors/time/nickel.rb +19 -20
  81. data/lib/treat/workers/extractors/time/ruby.rb +2 -1
  82. data/lib/treat/workers/extractors/topic_words/lda.rb +12 -12
  83. data/lib/treat/workers/extractors/topics/reuters.rb +9 -13
  84. data/lib/treat/workers/formatters/readers/autoselect.rb +1 -1
  85. data/lib/treat/workers/formatters/readers/image.rb +19 -9
  86. data/lib/treat/workers/formatters/readers/odt.rb +2 -1
  87. data/lib/treat/workers/formatters/readers/pdf.rb +20 -3
  88. data/lib/treat/workers/formatters/readers/xml.rb +0 -1
  89. data/lib/treat/workers/formatters/serializers/mongo.rb +10 -20
  90. data/lib/treat/workers/formatters/serializers/xml.rb +17 -26
  91. data/lib/treat/workers/formatters/serializers/yaml.rb +5 -4
  92. data/lib/treat/workers/formatters/unserializers/mongo.rb +4 -4
  93. data/lib/treat/workers/formatters/unserializers/xml.rb +3 -4
  94. data/lib/treat/workers/formatters/unserializers/yaml.rb +3 -4
  95. data/lib/treat/workers/formatters/visualizers/dot.rb +1 -0
  96. data/lib/treat/workers/formatters/visualizers/standoff.rb +2 -3
  97. data/lib/treat/workers/formatters/visualizers/tree.rb +2 -3
  98. data/lib/treat/workers/{group.rb → groupable.rb} +9 -9
  99. data/lib/treat/workers/inflectors/cardinalizers/linguistics.rb +1 -3
  100. data/lib/treat/workers/inflectors/conjugators/linguistics.rb +5 -7
  101. data/lib/treat/workers/inflectors/declensors/english.rb +13 -20
  102. data/lib/treat/workers/inflectors/declensors/linguistics.rb +29 -28
  103. data/lib/treat/workers/inflectors/ordinalizers/linguistics.rb +0 -2
  104. data/lib/treat/workers/inflectors/stemmers/porter.rb +8 -10
  105. data/lib/treat/workers/inflectors/stemmers/porter_c.rb +7 -7
  106. data/lib/treat/workers/inflectors/stemmers/uea.rb +3 -8
  107. data/lib/treat/workers/learners/classifiers/id3.rb +17 -14
  108. data/lib/treat/workers/learners/classifiers/linear.rb +15 -27
  109. data/lib/treat/workers/learners/classifiers/mlp.rb +32 -19
  110. data/lib/treat/workers/learners/classifiers/svm.rb +28 -21
  111. data/lib/treat/workers/lexicalizers/categorizers/from_tag.rb +19 -3
  112. data/lib/treat/workers/lexicalizers/sensers/wordnet.rb +15 -7
  113. data/lib/treat/workers/lexicalizers/taggers/brill/patch.rb +4 -1
  114. data/lib/treat/workers/lexicalizers/taggers/brill.rb +8 -19
  115. data/lib/treat/workers/lexicalizers/taggers/lingua.rb +4 -15
  116. data/lib/treat/workers/lexicalizers/taggers/stanford.rb +22 -13
  117. data/lib/treat/workers/processors/chunkers/autoselect.rb +2 -3
  118. data/lib/treat/workers/processors/chunkers/html.rb +1 -6
  119. data/lib/treat/workers/processors/parsers/enju.rb +2 -4
  120. data/lib/treat/workers/processors/parsers/stanford.rb +13 -7
  121. data/lib/treat/workers/processors/segmenters/punkt.rb +25 -11
  122. data/lib/treat/workers/processors/segmenters/scalpel.rb +20 -0
  123. data/lib/treat/workers/processors/segmenters/srx.rb +42 -0
  124. data/lib/treat/workers/processors/segmenters/stanford.rb +5 -5
  125. data/lib/treat/workers/processors/segmenters/tactful.rb +21 -11
  126. data/lib/treat/workers/processors/tokenizers/ptb.rb +40 -30
  127. data/lib/treat/workers/processors/tokenizers/punkt.rb +14 -19
  128. data/lib/treat/workers/processors/tokenizers/stanford.rb +38 -22
  129. data/lib/treat/workers/retrievers/indexers/ferret.rb +6 -3
  130. data/lib/treat/workers/retrievers/searchers/ferret.rb +2 -2
  131. data/lib/treat/workers/workers.rb +6 -0
  132. data/lib/treat.rb +18 -32
  133. data/models/MANIFEST +1 -0
  134. data/spec/core/data_set.rb +174 -0
  135. data/spec/core/export.rb +52 -0
  136. data/spec/core/problem.rb +144 -0
  137. data/spec/core/question.rb +52 -0
  138. data/spec/{collection.rb → entities/collection.rb} +20 -35
  139. data/spec/{document.rb → entities/document.rb} +3 -54
  140. data/spec/{entity.rb → entities/entity.rb} +10 -9
  141. data/spec/entities/phrase.rb +33 -0
  142. data/spec/{token.rb → entities/token.rb} +0 -57
  143. data/spec/entities/word.rb +3 -0
  144. data/spec/{zone.rb → entities/zone.rb} +0 -26
  145. data/spec/helper.rb +116 -32
  146. data/spec/sandbox.rb +258 -25
  147. data/spec/treat.rb +26 -34
  148. data/spec/workers/agnostic.rb +137 -0
  149. data/spec/workers/english.rb +194 -0
  150. data/spec/workers/examples/english/economist/hungarys_troubles.txt +46 -0
  151. data/spec/workers/examples/english/economist/saving_the_euro.odt +0 -0
  152. data/spec/{samples → workers/examples/english}/mathematicians/archimedes.abw +0 -0
  153. data/spec/{samples → workers/examples/english}/mathematicians/euler.html +0 -0
  154. data/spec/{samples → workers/examples/english}/mathematicians/gauss.pdf +0 -0
  155. data/spec/{samples → workers/examples/english}/mathematicians/leibniz.txt +0 -0
  156. data/spec/{samples → workers/examples/english}/mathematicians/newton.doc +0 -0
  157. data/spec/workers/examples/english/phrase.xml +5 -0
  158. data/spec/workers/examples/english/test.txt +1 -0
  159. data/spec/workers/language.rb +280 -0
  160. data/spec/workers.rb +28 -0
  161. metadata +122 -105
  162. data/lib/treat/config/core/acronyms.rb +0 -5
  163. data/lib/treat/config/core/encodings.rb +0 -8
  164. data/lib/treat/config/core/entities.rb +0 -2
  165. data/lib/treat/config/core/language.rb +0 -3
  166. data/lib/treat/config/core/paths.rb +0 -8
  167. data/lib/treat/config/core/syntax.rb +0 -1
  168. data/lib/treat/config/core/verbosity.rb +0 -1
  169. data/lib/treat/config/databases/default.rb +0 -1
  170. data/lib/treat/config/databases/mongo.rb +0 -1
  171. data/lib/treat/config/languages/agnostic.rb +0 -34
  172. data/lib/treat/config/languages/english.rb +0 -60
  173. data/lib/treat/config/languages/french.rb +0 -18
  174. data/lib/treat/config/languages/german.rb +0 -18
  175. data/lib/treat/config/languages/italian.rb +0 -12
  176. data/lib/treat/config/languages/polish.rb +0 -12
  177. data/lib/treat/config/languages/spanish.rb +0 -12
  178. data/lib/treat/config/languages/swedish.rb +0 -12
  179. data/lib/treat/config/libraries/punkt.rb +0 -1
  180. data/lib/treat/config/libraries/reuters.rb +0 -1
  181. data/lib/treat/config/libraries/stanford.rb +0 -1
  182. data/lib/treat/config/linguistics/categories.rb +0 -4
  183. data/lib/treat/config/linguistics/punctuation.rb +0 -33
  184. data/lib/treat/config/tags/aligned.rb +0 -221
  185. data/lib/treat/config/tags/enju.rb +0 -71
  186. data/lib/treat/config/tags/paris7.rb +0 -17
  187. data/lib/treat/config/tags/ptb.rb +0 -15
  188. data/lib/treat/config/workers/list.rb +0 -1
  189. data/lib/treat/config.rb +0 -135
  190. data/lib/treat/core.rb +0 -5
  191. data/lib/treat/entities/abilities/copyable.rb +0 -47
  192. data/lib/treat/entities/abilities/debuggable.rb +0 -83
  193. data/lib/treat/entities/abilities/registrable.rb +0 -46
  194. data/lib/treat/entities/collection.rb +0 -40
  195. data/lib/treat/entities/document.rb +0 -10
  196. data/lib/treat/entities/group.rb +0 -18
  197. data/lib/treat/entities/section.rb +0 -13
  198. data/lib/treat/entities/token.rb +0 -47
  199. data/lib/treat/entities/zone.rb +0 -12
  200. data/lib/treat/entities.rb +0 -6
  201. data/lib/treat/helpers/didyoumean.rb +0 -57
  202. data/lib/treat/helpers/escaping.rb +0 -15
  203. data/lib/treat/helpers/formatting.rb +0 -41
  204. data/lib/treat/helpers/objtohash.rb +0 -8
  205. data/lib/treat/helpers/platform.rb +0 -15
  206. data/lib/treat/helpers/reflection.rb +0 -17
  207. data/lib/treat/helpers/temporary.rb +0 -27
  208. data/lib/treat/helpers/verbosity.rb +0 -19
  209. data/lib/treat/helpers.rb +0 -5
  210. data/lib/treat/loaders.rb +0 -10
  211. data/lib/treat/proxies.rb +0 -106
  212. data/lib/treat/workers/formatters/unserializers/autoselect.rb +0 -17
  213. data/lib/treat/workers/inflectors/declensors/active_support.rb +0 -31
  214. data/lib/treat/workers/processors/tokenizers/tactful.rb +0 -68
  215. data/spec/core.rb +0 -441
  216. data/spec/phrase.rb +0 -112
  217. data/spec/word.rb +0 -111
@@ -1,10 +1,10 @@
1
- module Treat::Workers::Group
1
+ module Treat::Workers::Groupable
2
2
 
3
3
  # Lazily load the worker classes in the group.
4
4
  def const_missing(const)
5
5
  bits = self.ancestors[0].to_s.split('::')
6
- bits.collect! { |bit| ucc(bit) }
7
- file = bits.join('/') + "/#{ucc(const)}"
6
+ bits.collect! { |bit| bit.ucc }
7
+ file = bits.join('/') + "/#{const.ucc}"
8
8
  if not File.readable?(Treat.paths.lib + "#{file}.rb")
9
9
  raise Treat::Exception,
10
10
  "File '#{file}.rb' corresponding to " +
@@ -26,7 +26,7 @@ module Treat::Workers::Group
26
26
  # Populates once the list of the workers in the group
27
27
  # by crawling the filesystem.
28
28
  def list
29
- mod = ucc(cl(self))
29
+ mod = self.mn.ucc
30
30
  if @@list[mod].nil?
31
31
  @@list[mod] = []
32
32
  dirs = Dir[Treat.paths.lib + "treat/workers/*/#{mod}/*.rb"]
@@ -43,7 +43,7 @@ module Treat::Workers::Group
43
43
  def has_target?(target, strict = false)
44
44
  is_target = false
45
45
  self.targets.each do |entity_type|
46
- t = cc(entity_type)
46
+ t = entity_type.cc
47
47
  entity_type = Treat::Entities.const_get(t)
48
48
  if target < entity_type ||
49
49
  entity_type == target
@@ -57,10 +57,10 @@ module Treat::Workers::Group
57
57
  # the algorithm is added, it will be automatically
58
58
  # installed on all the targets of the group.
59
59
  def add(class_name, &block)
60
- c = cc(class_name).intern
60
+ c = class_name.cc.intern
61
61
  klass = self.const_set(c, Class.new)
62
62
  method = self.method
63
- @@list[ucc(cl(self))] << class_name
63
+ @@list[self.mn.ucc] << class_name
64
64
  klass.send(:define_singleton_method,
65
65
  method) do |entity, options={}|
66
66
  block.call(entity, options)
@@ -75,7 +75,7 @@ module Treat::Workers::Group
75
75
 
76
76
  # Modify the extended class.
77
77
  def self.extended(group)
78
-
78
+
79
79
  group.module_eval do
80
80
 
81
81
  class << self
@@ -114,7 +114,7 @@ module Treat::Workers::Group
114
114
  @method = nil
115
115
  def self.method
116
116
  return @method if @method
117
- m = ucc(cl(self)).dup
117
+ m = self.mn.ucc.dup
118
118
  if m[-4..-1] == 'zers'
119
119
  if type == :annotator
120
120
  m[-5..-1] = m[-6] == 'l' ? '' : 'y'
@@ -3,9 +3,7 @@
3
3
  # number in words in cardinal form.
4
4
  #
5
5
  # Project website: http://deveiate.org/projects/Linguistics/
6
- module Treat::Workers::Inflectors::Cardinalizers::Linguistics
7
-
8
- require 'treat/loaders/linguistics'
6
+ class Treat::Workers::Inflectors::Cardinalizers::Linguistics
9
7
 
10
8
  DefaultOptions = {
11
9
  :language => Treat.core.language.default
@@ -4,18 +4,16 @@
4
4
  # Project website: http://deveiate.org/projects/Linguistics/
5
5
  module Treat::Workers::Inflectors::Conjugators::Linguistics
6
6
 
7
- require 'treat/loaders/linguistics'
8
-
9
7
  DefaultOptions = {
10
8
  :strict => false
11
9
  }
12
10
 
13
11
  Forms = {
14
- :present_participle =>
12
+ 'present_participle' =>
15
13
  {:mode => 'participle', :tense => 'present'},
16
- :infinitive => {:mode => 'infinitive'},
17
- :plural_verb => {:count => 'plural'},
18
- :singular_verb => {:count => 'singular'}
14
+ 'infinitive' => {:mode => 'infinitive'},
15
+ 'plural_verb' => {:count => 'plural'},
16
+ 'singular_verb' => {:count => 'singular'}
19
17
  }
20
18
 
21
19
  # Conjugate a verb using ruby linguistics with the specified
@@ -35,7 +33,7 @@ module Treat::Workers::Inflectors::Conjugators::Linguistics
35
33
  cat = entity.check_has(:category)
36
34
  return if cat != 'verb' && options[:strict]
37
35
 
38
- options = Forms[options[:form]] if options[:form]
36
+ options = Forms[options[:form].to_s] if options[:form]
39
37
 
40
38
  klass = Treat::Loaders::Linguistics.load(entity.language)
41
39
  if options[:mode] == 'infinitive'
@@ -1,38 +1,31 @@
1
- # This class is a wrapper for the Inflect module,
2
- # copied from the unmaintained 'english' ruby gem,
3
- # created by Thomas Sawyer.
1
+ # Inflection using the inflect module copied from
2
+ # the unmaintained 'english' ruby gem.
4
3
  #
5
- # Released under the MIT License.
6
- #
7
- # http://english.rubyforge.org
4
+ # License: MIT
5
+ # Website: http://english.rubyforge.org
8
6
  class Treat::Workers::Inflectors::Declensors::English
9
7
 
10
- require 'treat/inflectors/declensors/english/inflect'
8
+ require_relative 'english/inflect'
9
+
10
+ # Part of speech that can be declensed.
11
+ POS = ['noun', 'adjective', 'determiner']
11
12
 
12
13
  # Retrieve the declensions (singular, plural)
13
14
  # of an english word using a class lifted from
14
15
  # the 'english' ruby gem.
15
16
  def self.declense(entity, options)
16
-
17
17
  cat = entity.check_has(:category)
18
- unless ['noun', 'adjective', 'determiner'].
19
- include?(cat)
20
- return
21
- end
22
-
18
+ return unless POS.include?(cat)
23
19
  unless options[:count]
24
- raise Treat::Exception,
25
- "Must supply option count (:singular or :plural)."
20
+ raise Treat::Exception, 'Must supply ' +
21
+ 'option count ("singular" or "plural").'
26
22
  end
27
-
28
23
  string = entity.to_s
29
-
30
- if options[:count] == :plural
24
+ if options[:count].to_s == 'plural'
31
25
  Inflect.plural(string)
32
- elsif options[:count] == :singular
26
+ elsif options[:count].to_s == 'singular'
33
27
  Inflect.singular(string)
34
28
  end
35
-
36
29
  end
37
30
 
38
31
  end
@@ -1,49 +1,50 @@
1
- # This class is a wrapper for the functions included
2
- # in the 'linguistics' gem that allow to obtain the
3
- # declensions of a word.
1
+ # Inflection using the the 'linguistics' gem (will attempt
2
+ # to load the Linguistics module corresponding to the
3
+ # language of the entity.
4
4
  #
5
- # Project website: http://deveiate.org/projects/Linguistics/
5
+ # Website: http://deveiate.org/projects/Linguistics/
6
6
  class Treat::Workers::Inflectors::Declensors::Linguistics
7
7
 
8
- require 'treat/loaders/linguistics'
9
-
8
+ # Part of speech that can be declensed.
9
+ POS = ['noun', 'adjective', 'determiner']
10
+
10
11
  # Retrieve a declension of a word using the 'linguistics' gem.
11
12
  #
12
13
  # Options:
13
14
  #
14
15
  # - (Identifier) :count => :singular, :plural
15
16
  def self.declense(entity, options = {})
16
-
17
- cat = entity.check_has(:category)
18
- unless ['noun', 'adjective', 'determiner'].
19
- include?(cat)
20
- return
21
- end
22
-
17
+
18
+ cat = entity.get(:category)
19
+ return if cat && !POS.include?(cat)
23
20
  unless options[:count]
24
- raise Treat::Exception,
25
- "Must supply option count (:singular or :plural)."
21
+ raise Treat::Exception, 'Must supply ' +
22
+ ':count option ("singular" or "plural").'
26
23
  end
27
24
 
28
- klass = Treat::Loaders::Linguistics.load(entity.language)
25
+ klass = Treat::Loaders::
26
+ Linguistics.load(entity.language)
29
27
  string = entity.to_s
30
-
31
- if options[:count] == 'plural'
32
-
33
- if entity.has?(:category) &&
34
- ['noun', 'adjective', 'verb'].
35
- include?(entity.category)
28
+
29
+ if options[:count].to_s == 'plural'
30
+ if (entity.has?(:category))
31
+ result = ''
36
32
  silence_warnings do
37
- klass.send(
38
- :"plural_#{entity.category}",
33
+ result = klass.send(
34
+ :"plural_#{entity.category}",
39
35
  string)
40
36
  end
37
+ return result
41
38
  else
42
- klass.plural(string)
39
+ return klass.plural(string)
43
40
  end
44
-
41
+
42
+ else
43
+ raise Treat::Exception,
44
+ "Ruby Linguistics does not support " +
45
+ "singularization of words."
45
46
  end
46
-
47
+
47
48
  end
48
49
 
49
- end
50
+ end
@@ -5,8 +5,6 @@
5
5
  # Project website: http://deveiate.org/projects/Linguistics/
6
6
  class Treat::Workers::Inflectors::Ordinalizers::Linguistics
7
7
 
8
- require 'treat/loaders/linguistics'
9
-
10
8
  DefaultOptions = {
11
9
  :language => Treat.core.language.default
12
10
  }
@@ -1,15 +1,13 @@
1
- # Stem a word using a native Ruby implementation of the
2
- # Porter stemming algorithm, ported to Ruby from a
3
- # version coded up in Perl. This is a simplified
4
- # implementation; for a true and fast Porter stemmer,
5
- # see Treat::Workers::Inflectors::Stemmers::PorterC.
1
+ # Stemming using a native Ruby implementation of the
2
+ # Porter stemming algorithm, a rule-based suffix-stripping
3
+ # stemmer which is very widely used and is considered the
4
+ # de-facto standard algorithm used for English stemming.
6
5
  #
7
- # Authored by Ray Pereda (raypereda@hotmail.com).
8
- # Unknown license.
6
+ # Original paper: Porter, 1980. An algorithm for suffix
7
+ # stripping. Program, vol. 14, no. 3, p. 130-137.
9
8
  #
10
- # Original paper: Porter, 1980. An algorithm for suffix stripping,
11
- # Program, Vol. 14, no. 3, pp 130-137,
12
- # Original C implementation: http://www.tartarus.org/~martin/PorterStemmer.
9
+ # Authors: Ray Pereda (raypereda@hotmail.com).
10
+ # License: Unknown.
13
11
  class Treat::Workers::Inflectors::Stemmers::Porter
14
12
 
15
13
  # Returns the stem of a word using a native Porter stemmer.
@@ -1,11 +1,11 @@
1
- # Stems words using the 'ruby-stemmer' gem, which
2
- # wraps a C version of the Porter stemming algorithm.
1
+ # Stemming using a wrapper for a C implementation of the
2
+ # Porter stemming algorithm, a rule-based suffix-stripping
3
+ # stemmer which is very widely used and is considered the
4
+ # de-facto standard algorithm used for English stemming.
3
5
  #
4
- # Project website: https://github.com/aurelian/ruby-stemmer
5
- # Original paper: Porter, 1980. An algorithm for suffix stripping,
6
- # Program, Vol. 14, no. 3, pp 130-137,
7
- # Original C implementation: http://www.tartarus.org/~martin/PorterStemmer.
8
- module Treat::Workers::Inflectors::Stemmers::PorterC
6
+ # Original paper: Porter, 1980. An algorithm for suffix
7
+ # stripping. Program, vol. 14, no. 3, p. 130-137.
8
+ class Treat::Workers::Inflectors::Stemmers::PorterC
9
9
 
10
10
  # Require the 'ruby-stemmer' gem.
11
11
  silence_warnings { require 'lingua/stemmer' }
@@ -1,15 +1,10 @@
1
- # Stems a word using the UEA algorithm, implemented
2
- # by the 'uea-stemmer' gem.
3
- #
4
- # "Similar to other stemmers, UEA-Lite operates on a
5
- # set of rules which are used as steps. There are two
1
+ # Stemming using the UEA algorithm, a stemmer that operates
2
+ # on a set of rules which are used as steps. There are two
6
3
  # groups of rules: the first to clean the tokens, and
7
- # the second to alter suffixes."
4
+ # the second to alter suffixes.
8
5
  #
9
- # Project website: https://github.com/ealdent/uea-stemmer
10
6
  # Original paper: Jenkins, Marie-Claire, Smith, Dan,
11
7
  # Conservative stemming for search and indexing, 2005.
12
- # http://www.uea.ac.uk/polopoly_fs/1.85493!stemmer25feb.pdf
13
8
  class Treat::Workers::Inflectors::Stemmers::UEA
14
9
 
15
10
  # Require the 'uea-stemmer' gem.
@@ -1,3 +1,10 @@
1
+ # Classification based on Ross Quinlan's ID3
2
+ # (Iterative Dichotomiser 3) decision tree
3
+ # algorithm.
4
+ #
5
+ # Original paper: Quinlan, J. R. 1986.
6
+ # Induction of Decision Trees. Mach. Learn.
7
+ # 1, 1 (Mar. 1986), 81-106.
1
8
  class Treat::Workers::Learners::Classifiers::ID3
2
9
 
3
10
  require 'decisiontree'
@@ -5,24 +12,20 @@ class Treat::Workers::Learners::Classifiers::ID3
5
12
  @@classifiers = {}
6
13
 
7
14
  def self.classify(entity, options = {})
8
-
9
- set = options[:training]
10
- cl = set.problem
11
-
12
- if !@@classifiers[cl]
15
+ dset = options[:training]
16
+ prob = dset.problem
17
+ if !@@classifiers[prob]
13
18
  dec_tree = DecisionTree::ID3Tree.new(
14
- cl.feature_labels.map { |l| l.to_s },
15
- set.items.map { |i| i[:features]},
16
- cl.question.default, cl.question.type)
19
+ prob.feature_labels.map { |l| l.to_s },
20
+ dset.items.map { |i| i[:features] },
21
+ prob.question.default, prob.question.type)
17
22
  dec_tree.train
18
- @@classifiers[cl] = dec_tree
23
+ @@classifiers[prob] = dec_tree
19
24
  else
20
- dec_tree = @@classifiers[cl]
21
- dec_tree.graph('testingbitch')
25
+ dec_tree = @@classifiers[prob]
22
26
  end
23
- dec_tree.predict(
24
- cl.export_features(entity, false)
25
- )
27
+ vect = prob.export_features(entity, false)
28
+ dec_tree.predict(vect)
26
29
  end
27
30
 
28
31
  end
@@ -11,35 +11,23 @@ class Treat::Workers::Learners::Classifiers::Linear
11
11
  }
12
12
 
13
13
  def self.classify(entity, options = {})
14
-
15
14
  options = DefaultOptions.merge(options)
16
- set = options[:training]
17
- problem = set.problem
18
-
19
- if !@@classifiers[problem]
20
- labels = problem.question.labels
21
- unless labels
22
- raise Treat::Exception,
23
- "LibLinear requires that you provide the possible " +
24
- "labels to assign to classification items when " +
25
- "specifying the question."
26
- end
27
- param = LParameter.new
28
- param.solver_type = options[:solver_type]
29
- param.eps = options[:eps]
30
- bias = options[:bias]
31
- data = set.items.map do |item|
32
- self.array_to_hash(item[:features])
33
- end
34
- prob = LProblem.new(labels, data, bias)
35
- @@classifiers[problem] =
36
- LModel.new(prob, param)
15
+ dset = options[:training]
16
+ prob, items = dset.problem, dset.items
17
+ if !@@classifiers[prob]
18
+ lparam = LParameter.new
19
+ lparam.solver_type = options[:solver_type]
20
+ lparam.eps = options[:eps]
21
+ lbls = items.map { |it| it[:features][-1] }
22
+ exs = items.map { |it| it[:features][0..-2] }.
23
+ map { |ary| self.array_to_hash(ary) }
24
+ lprob = LProblem.new(lbls, exs, options[:bias])
25
+ model = LModel.new(lprob, lparam)
26
+ @@classifiers[prob] = model
37
27
  end
38
-
39
- @@classifiers[problem].predict(
40
- self.array_to_hash(problem.
41
- export_features(entity, false)))
42
-
28
+ features = prob.export_features(entity, false)
29
+ @@classifiers[prob].predict(
30
+ self.array_to_hash(features))
43
31
  end
44
32
 
45
33
  def self.array_to_hash(array)
@@ -1,30 +1,43 @@
1
- # Currently, this MLP is limited to 1 output.
1
+ # Classification based on a multilayer perceptron.
2
2
  class Treat::Workers::Learners::Classifiers::MLP
3
3
 
4
- require 'ai4r'
4
+ require 'ruby_fann/neural_network'
5
5
 
6
- @@mlps = {}
6
+ DefaultOptions = {
7
+ num_inputs: 3,
8
+ hidden_neurons: [2, 8, 4, 3, 4],
9
+ num_outputs: 1,
10
+ max_neurons: 1000,
11
+ neurons_between_reports: 1,
12
+ desired_error: 0.1
13
+ }
14
+
15
+ @@classifiers = {}
7
16
 
8
17
  def self.classify(entity, options = {})
9
-
10
- set = options[:training]
11
- cl = set.problem
12
-
13
- if !@@mlps[cl]
14
- net = Ai4r::NeuralNetwork::Backpropagation.new(
15
- [cl.feature_labels.size, 3, 1])
16
- set.items.each do |item|
17
- inputs = item[:features][0..-2]
18
- outputs = [item[:features][-1]]
19
- net.train(inputs, outputs)
18
+ options = DefaultOptions.merge(options)
19
+ dset = options[:training]
20
+ prob, items = dset.problem, dset.items
21
+ if !@@classifiers[prob]
22
+ fann = RubyFann::Standard.new(options)
23
+ inputs = items.map { |it| it[:features][0..-2] }
24
+ outputs = items.map { |it| [it[:features][-1]] }
25
+ training = silence_stdout do
26
+ RubyFann::TrainData.new(inputs:
27
+ inputs, desired_outputs: outputs)
20
28
  end
21
- @@mlps[cl] = net
29
+ params = [options[:max_neurons],
30
+ options[:neurons_between_reports],
31
+ options[:desired_error]]
32
+ fann.train_on_data(training, *params)
33
+ @@classifiers[prob] = fann
22
34
  else
23
- net = @@mlps[cl]
35
+ fann = @@classifiers[prob]
24
36
  end
25
-
26
- net.eval(cl.export_features(entity, false))[0]
27
-
37
+ vect = prob.export_features(entity, false)
38
+ Treat.core.verbosity.silence ?
39
+ silence_stdout { fann.run(vect)[0] } :
40
+ fann.run(vect)[0]
28
41
  end
29
42
 
30
43
  end
@@ -1,31 +1,38 @@
1
1
  class Treat::Workers::Learners::Classifiers::SVM
2
2
 
3
- require 'svm'
3
+ require 'libsvm'
4
4
 
5
5
  @@classifiers = {}
6
+
7
+ DefaultOptions = {
8
+ cache_size: 1, # in MB
9
+ eps: 0.001,
10
+ c: 10
11
+ }
6
12
 
13
+ # - (Numeric) :cache_size => cache size in MB.
14
+ # - (Numeric) :eps => tolerance of termination criterion
15
+ # - (Numeric) :c => C parameter
7
16
  def self.classify(entity, options = {})
8
-
9
- set = options[:training]
10
- problem = set.problem
11
-
12
- if !@@classifiers[problem]
13
- labels = problem.question.labels
14
- unless labels
15
- raise Treat::Exception,
16
- "LibSVM requires that you provide the possible " +
17
- "labels to assign to classification items when " +
18
- "specifying the question."
19
- end
20
- data = set.items.map { |item| item[:features] }
21
- prob = Problem.new(labels, data)
22
- param = Parameter.new(:kernel_type => LINEAR, :C => 10)
23
- @@classifiers[problem] = Model.new(prob, param)
17
+ options = DefaultOptions.merge(options)
18
+ dset = options[:training]
19
+ prob, items = dset.problem, dset.items
20
+ if !@@classifiers[prob]
21
+ lprob = Libsvm::Problem.new
22
+ lparam = Libsvm::SvmParameter.new
23
+ lparam.cache_size = options[:cache_size]
24
+ lparam.eps = options[:eps]
25
+ lparam.c = options[:c]
26
+ llabels = items.map { |it| it[:features][-1] }
27
+ lexamples = items.map { |it| it[:features][0..-2] }.
28
+ map { |ary| Libsvm::Node.features(ary) }
29
+ lprob.set_examples(llabels, lexamples)
30
+ model = Libsvm::Model.train(lprob, lparam)
31
+ @@classifiers[prob] = model
24
32
  end
25
-
26
- @@classifiers[problem].predict_probability(
27
- problem.export_features(entity, false))[0]
28
-
33
+ features = prob.export_features(entity, false)
34
+ @@classifiers[prob].predict(
35
+ Libsvm::Node.features(features))
29
36
  end
30
37
 
31
38
  end
@@ -1,6 +1,22 @@
1
- # Finds the general part of speech of an entity
2
- # (:sentence, :noun_phrase, :verb, :adverb, etc.)
3
- # from its tag (e.g. 'S', 'NP', 'VBZ', 'ADV', etc.).
1
+ # Uses a hand-made table to convert part-of-speech tags
2
+ # to descriptive English word or phrase categories.
3
+ #
4
+ # Original paper: 1) Manning, Christopher and Schütze, Hinrich,
5
+ # 1999. Foundations of Statistical Natural Language
6
+ # Processing. MIT Press, p. 141-142.
7
+ #
8
+ # 2) Schiller et al. 1999. Guidelines fur das Tagging
9
+ # deutscher Textcorpora. Universitat Tubingen. Institut
10
+ # fur maschinelle Sprachverarbeitung.
11
+ #
12
+ # 3) Anne Abeille, Francois Toussenel, et Martine
13
+ # Cheradame. 2004. Corpus Le Monde: Annotations en
14
+ # constituants. Guide pour les correcteurs. LLF et
15
+ # UFRL and Universite Paris 7.
16
+
17
+ # 4) Xue and Palmer. 2008. Annotating the Propositions in
18
+ # the Penn Chinese Treebank. University of Pennsylvania,
19
+ # Department of Computer Information and Science.
4
20
  class Treat::Workers::Lexicalizers::Categorizers::FromTag
5
21
 
6
22
  Pttc = Treat.tags.aligned.phrase_tags_to_category
@@ -1,8 +1,13 @@
1
- # Obtain lexical information about a word using the
2
- # ruby 'wordnet' gem.
1
+ # Sense information (synonyms, antonyms, hypernyms
2
+ # and hyponyms) obtained through a Ruby parser that
3
+ # accesses Wordnet flat files.
4
+ #
5
+ # Original paper: George A. Miller (1995). WordNet:
6
+ # A Lexical Database for English. Communications of
7
+ # the ACM Vol. 38, No. 11: 39-41.
3
8
  class Treat::Workers::Lexicalizers::Sensers::Wordnet
4
9
 
5
- # Require the 'wordnet' gem.
10
+ # Require the 'wordnet' gem (install as 'rwordnet').
6
11
  require 'wordnet'
7
12
 
8
13
  # Patch for bug.
@@ -13,7 +18,7 @@ class Treat::Workers::Lexicalizers::Sensers::Wordnet
13
18
  end
14
19
 
15
20
  # Require an adaptor for Wordnet synsets.
16
- require 'treat/workers/lexicalizers/sensers/wordnet/synset'
21
+ require_relative 'wordnet/synset'
17
22
 
18
23
  # Noun, adjective and verb indexes.
19
24
  @@indexes = {}
@@ -45,13 +50,16 @@ class Treat::Workers::Lexicalizers::Sensers::Wordnet
45
50
 
46
51
  lemma.synsets.each do |synset|
47
52
  synsets <<
48
- Treat::Workers::Lexicalizers::Sensers::Wordnet::Synset.new(synset)
53
+ Treat::Workers::Lexicalizers::
54
+ Sensers::Wordnet::Synset.new(synset)
49
55
  end
50
56
 
51
57
  ((synsets.collect do |ss|
52
58
  ss.send(options[:nym])
53
- end - [word.value]).flatten).uniq
54
-
59
+ end - [word.value]).
60
+ flatten).uniq.map do |token|
61
+ token.gsub('_', ' ')
62
+ end
55
63
  end
56
64
 
57
65
  end
@@ -11,7 +11,10 @@ rescue TypeError
11
11
  patch = true
12
12
  # Unset the class Word for the duration
13
13
  # of loading the tagger.
14
- Object.const_unset(:Word); retry
14
+ Object.instance_eval do
15
+ remove_const(:Word)
16
+ end
17
+ retry
15
18
  else
16
19
  raise Treat::Exception,
17
20
  'Something went wrong due to a name clash with the "rbtagger" gem.' +