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
@@ -1,40 +0,0 @@
|
|
1
|
-
module Treat::Entities
|
2
|
-
# Represents a collection of texts.
|
3
|
-
class Collection < Entity
|
4
|
-
|
5
|
-
# Initialize the collection with a folder
|
6
|
-
# containing the texts of the collection.
|
7
|
-
def initialize(folder = nil, id = nil)
|
8
|
-
super('', id)
|
9
|
-
if folder
|
10
|
-
if !FileTest.directory?(folder)
|
11
|
-
FileUtils.mkdir(folder)
|
12
|
-
end
|
13
|
-
set :folder, folder if folder
|
14
|
-
i = folder + '/.index'
|
15
|
-
if FileTest.directory?(i)
|
16
|
-
set :index, i
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
# Works like the default <<, but if the
|
22
|
-
# file being added is a collection or a
|
23
|
-
# document, then copy that collection or
|
24
|
-
# document into this collection's folder.
|
25
|
-
def <<(entities, copy = true)
|
26
|
-
unless entities.is_a?(Array)
|
27
|
-
entities = [entities]
|
28
|
-
end
|
29
|
-
entities.each do |entity|
|
30
|
-
if [:document, :collection].
|
31
|
-
include?(entity.type) && copy &&
|
32
|
-
@features[:folder] != nil
|
33
|
-
entity = entity.copy_into(self)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
super(entities)
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
end
|
data/lib/treat/entities/group.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module Treat::Entities
|
2
|
-
|
3
|
-
# Represents a group of tokens.
|
4
|
-
class Group < Entity; end
|
5
|
-
|
6
|
-
# Represents a group of words
|
7
|
-
# with a sentence ender (.!?)
|
8
|
-
class Sentence < Group; end
|
9
|
-
|
10
|
-
# Represents a group of words,
|
11
|
-
# with no sentence ender.
|
12
|
-
class Phrase < Group; end
|
13
|
-
|
14
|
-
# Represents a non-linguistic
|
15
|
-
# fragment (e.g. stray symbols).
|
16
|
-
class Fragment < Group; end
|
17
|
-
|
18
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
module Treat::Entities
|
2
|
-
# Represents a section.
|
3
|
-
class Section < Entity; end
|
4
|
-
|
5
|
-
# Represents a page of text.
|
6
|
-
class Page < Section; end
|
7
|
-
|
8
|
-
# Represents a block of text
|
9
|
-
class Block < Section; end
|
10
|
-
|
11
|
-
# Represents a list.
|
12
|
-
class List < Section; end
|
13
|
-
end
|
data/lib/treat/entities/token.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
module Treat::Entities
|
2
|
-
|
3
|
-
# Represents a terminal element
|
4
|
-
# (leaf) in the text structure.
|
5
|
-
class Token < Entity; end
|
6
|
-
|
7
|
-
# Represents a word. Strictly,
|
8
|
-
# this is /^[[:alpha:]\-']+$/.
|
9
|
-
class Word < Token; end
|
10
|
-
|
11
|
-
# Represents an enclitic.
|
12
|
-
# Strictly, this is any of
|
13
|
-
# 'll 'm 're 's 't or 've.
|
14
|
-
class Enclitic < Token; end
|
15
|
-
|
16
|
-
# Represents a number. Strictly,
|
17
|
-
# this is /^#?([0-9]+)(\.[0-9]+)?$/.
|
18
|
-
class Number < Token
|
19
|
-
def to_i; to_s.to_i; end
|
20
|
-
def to_f; to_s.to_f; end
|
21
|
-
end
|
22
|
-
|
23
|
-
# Represents a punctuation sign.
|
24
|
-
# Strictly, this is /^[[:punct:]\$]+$/.
|
25
|
-
class Punctuation < Token; end
|
26
|
-
|
27
|
-
# Represents a character that is neither
|
28
|
-
# a word, an enclitic, a number or a
|
29
|
-
# punctuation character (e.g. @#$%&*).
|
30
|
-
class Symbol < Token; end
|
31
|
-
|
32
|
-
# Represents a url. This is (imperfectly)
|
33
|
-
# defined as /^(http|https):\/\/[a-z0-9]
|
34
|
-
# +([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}
|
35
|
-
# (([0-9]{1,5})?\/.*)?$/ix
|
36
|
-
class Url < Token; end
|
37
|
-
|
38
|
-
# Represents a valid RFC822 address.
|
39
|
-
# This is (imperfectly) defined as
|
40
|
-
# /.+\@.+\..+/ (fixme maybe?)
|
41
|
-
class Email < Token; end
|
42
|
-
|
43
|
-
# Represents a token whose type
|
44
|
-
# cannot be identified.
|
45
|
-
class Unknown; end
|
46
|
-
|
47
|
-
end
|
data/lib/treat/entities/zone.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
module Treat::Entities
|
2
|
-
# Represents a zone of text.
|
3
|
-
class Zone < Entity; end
|
4
|
-
|
5
|
-
# Represents a title, subtitle,
|
6
|
-
# logical header of a text.
|
7
|
-
class Title < Zone; end
|
8
|
-
|
9
|
-
# Represents a paragraph (group
|
10
|
-
# of sentences and/or phrases).
|
11
|
-
class Paragraph < Zone; end
|
12
|
-
end
|
data/lib/treat/entities.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
# Search the list to see if there are
|
2
|
-
# words similar to #name in the #list
|
3
|
-
# If yes, return a string saying
|
4
|
-
# "Did you mean ... ?" with the names.
|
5
|
-
def did_you_mean?(list, name)
|
6
|
-
return '' # Fix
|
7
|
-
list = list.map { |e| e.to_s }
|
8
|
-
name = name.to_s
|
9
|
-
sugg = []
|
10
|
-
list.each do |element|
|
11
|
-
l = levenshtein(element,name)
|
12
|
-
if l > 0 && l < 2
|
13
|
-
sugg << element
|
14
|
-
end
|
15
|
-
end
|
16
|
-
unless sugg.size == 0
|
17
|
-
if sugg.size == 1
|
18
|
-
msg += " Perhaps you meant '#{sugg[0]}' ?"
|
19
|
-
else
|
20
|
-
sugg_quote = sugg[0..-2].map do
|
21
|
-
|x| '\'' + x + '\''
|
22
|
-
end
|
23
|
-
msg += " Perhaps you meant " +
|
24
|
-
"#{sugg_quote.join(', ')}," +
|
25
|
-
" or '#{sugg[-1]}' ?"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
msg
|
29
|
-
end
|
30
|
-
|
31
|
-
alias :dym? :did_you_mean?
|
32
|
-
|
33
|
-
# Return the levensthein distance between
|
34
|
-
# two strings taking into account the costs
|
35
|
-
# of insertion, deletion, and substitution.
|
36
|
-
# Used by did_you_mean? to detect typos.
|
37
|
-
def levenshtein(first, other, ins=1, del=1, sub=1)
|
38
|
-
return nil if first.nil? || other.nil?
|
39
|
-
dm = []
|
40
|
-
dm[0] = (0..first.length).collect { |i| i * ins}
|
41
|
-
fill = [0] * (first.length - 1).abs
|
42
|
-
for i in 1..other.length
|
43
|
-
dm[i] = [i * del, fill.flatten]
|
44
|
-
end
|
45
|
-
for i in 1..other.length
|
46
|
-
for j in 1..first.length
|
47
|
-
dm[i][j] = [
|
48
|
-
dm[i-1][j-1] +
|
49
|
-
(first[i-1] ==
|
50
|
-
other[i-1] ? 0 : sub),
|
51
|
-
dm[i][j-1] + ins,
|
52
|
-
dm[i-1][j] + del
|
53
|
-
].min
|
54
|
-
end
|
55
|
-
end
|
56
|
-
dm[other.length][first.length]
|
57
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# This is ugly, we should change it.
|
2
|
-
EscapeChar = '^^'
|
3
|
-
EscapedEscapeChar = '\^\^'
|
4
|
-
|
5
|
-
def escape_floats!(s)
|
6
|
-
s.gsub!(/([0-9]+)\.([0-9]+)/) do
|
7
|
-
$1 + EscapeChar + $2
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def unescape_floats!(s)
|
12
|
-
s.gsub!(/([0-9]+)#{EscapedEscapeChar}([0-9]+)/) do
|
13
|
-
$1 + '.' + $2
|
14
|
-
end
|
15
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# A cache to optimize camel casing.
|
2
|
-
@@cc_cache = {}
|
3
|
-
|
4
|
-
# A cache to optimize un camel casing.
|
5
|
-
@@ucc_cache = {}
|
6
|
-
|
7
|
-
# Convert un_camel_case to CamelCase.
|
8
|
-
def camel_case(o_phrase)
|
9
|
-
phrase = o_phrase.to_s.dup
|
10
|
-
return @@cc_cache[o_phrase] if @@cc_cache[o_phrase]
|
11
|
-
|
12
|
-
if Treat.core.acronyms.include?(phrase)
|
13
|
-
phrase = phrase.upcase
|
14
|
-
else
|
15
|
-
phrase.gsub!(/^[a-z]|_[a-z]/) { |a| a.upcase }
|
16
|
-
phrase.gsub!('_', '')
|
17
|
-
end
|
18
|
-
@@cc_cache[o_phrase] = phrase
|
19
|
-
end
|
20
|
-
|
21
|
-
alias :cc :camel_case
|
22
|
-
|
23
|
-
# Convert CamelCase to un_camel_case.
|
24
|
-
def un_camel_case(o_phrase)
|
25
|
-
phrase = o_phrase.to_s.dup
|
26
|
-
return @@ucc_cache[o_phrase] if @@ucc_cache[o_phrase]
|
27
|
-
if Treat.core.acronyms.include?(phrase.downcase)
|
28
|
-
phrase = phrase.downcase
|
29
|
-
else
|
30
|
-
phrase.gsub!(/[A-Z]/) { |p| '_' + p.downcase }
|
31
|
-
phrase = phrase[1..-1] if phrase[0] == '_'
|
32
|
-
end
|
33
|
-
@@ucc_cache[o_phrase] = phrase
|
34
|
-
end
|
35
|
-
|
36
|
-
alias :ucc :un_camel_case
|
37
|
-
|
38
|
-
# Retrieve the Class from a Module::Class.
|
39
|
-
def class_name(n); n.to_s.split('::')[-1]; end
|
40
|
-
|
41
|
-
alias :cl :class_name
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# Detect the platform we're running on.
|
2
|
-
def detect_platform
|
3
|
-
p = RUBY_PLATFORM.downcase
|
4
|
-
return :mac if p.include?("darwin")
|
5
|
-
return :windows if p.include?("mswin")
|
6
|
-
return :linux if p.include?("linux")
|
7
|
-
return :unknown
|
8
|
-
end
|
9
|
-
|
10
|
-
# Set up the right NULL device.
|
11
|
-
if detect_platform == :windows
|
12
|
-
NULL_DEVICE = 'NUL'
|
13
|
-
else
|
14
|
-
NULL_DEVICE = '/dev/null'
|
15
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# Return the name of the method that
|
2
|
-
# called the method that calls this method.
|
3
|
-
def caller_method(n = 3)
|
4
|
-
at = caller(n).first
|
5
|
-
/^(.+?):(\d+)(?::in `(.*)')?/ =~ at
|
6
|
-
Regexp.last_match[3].
|
7
|
-
gsub('block in ', '').intern
|
8
|
-
end
|
9
|
-
|
10
|
-
Object.module_eval do
|
11
|
-
# Unset a constant publicly.
|
12
|
-
def self.const_unset(const)
|
13
|
-
Object.instance_eval do
|
14
|
-
remove_const(const)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
# Require file utilities for creating and
|
2
|
-
# deleting temporary files.
|
3
|
-
require 'fileutils'
|
4
|
-
|
5
|
-
# Create a temporary file which is deleted
|
6
|
-
# after execution of the block.
|
7
|
-
def create_temp_file(ext, value = nil, &block)
|
8
|
-
fname = Treat.paths.tmp +
|
9
|
-
"#{Random.rand(10000000).to_s}.#{ext}"
|
10
|
-
File.open(fname, 'w') do |f|
|
11
|
-
f.write(value) if value
|
12
|
-
block.call(f.path)
|
13
|
-
end
|
14
|
-
ensure
|
15
|
-
File.delete(fname)
|
16
|
-
end
|
17
|
-
|
18
|
-
# Create a temporary directory, which is
|
19
|
-
# deleted after execution of the block.
|
20
|
-
def create_temp_dir(&block)
|
21
|
-
dname = Treat.paths.tmp +
|
22
|
-
"#{Random.rand(10000000).to_s}"
|
23
|
-
Dir.mkdir(dname)
|
24
|
-
block.call(dname)
|
25
|
-
ensure
|
26
|
-
FileUtils.rm_rf(dname)
|
27
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# Runs a block of code without warnings.
|
2
|
-
def silence_warnings(&block)
|
3
|
-
warn_level = $VERBOSE
|
4
|
-
$VERBOSE = nil
|
5
|
-
result = block.call
|
6
|
-
$VERBOSE = warn_level
|
7
|
-
result
|
8
|
-
end
|
9
|
-
|
10
|
-
# Runs a block of code while blocking stdout.
|
11
|
-
def silence_stdout(log = '/dev/null')
|
12
|
-
unless Treat.core.verbosity.silence
|
13
|
-
yield; return
|
14
|
-
end
|
15
|
-
old = $stdout.dup
|
16
|
-
$stdout.reopen(File.new(log, 'w'))
|
17
|
-
yield
|
18
|
-
$stdout = old
|
19
|
-
end
|
data/lib/treat/helpers.rb
DELETED
data/lib/treat/loaders.rb
DELETED
data/lib/treat/proxies.rb
DELETED
@@ -1,106 +0,0 @@
|
|
1
|
-
# Proxies install builders on core Ruby objects;
|
2
|
-
# when a method defined by Treat is called on these
|
3
|
-
# objects, the Ruby object is cast to a Treat entity
|
4
|
-
# and the method is called on the resultant type.
|
5
|
-
module Treat::Core::Proxies
|
6
|
-
|
7
|
-
# Provides a base functionality for proxies.
|
8
|
-
module Proxy
|
9
|
-
|
10
|
-
# Build the entity corresponding to the proxied
|
11
|
-
# object and send the method call to the entity.
|
12
|
-
def method_missing(sym, *args, &block)
|
13
|
-
if sym == :do || Treat::Workers.lookup(sym)
|
14
|
-
to_entity.send(sym, *args)
|
15
|
-
else
|
16
|
-
super(sym, *args, &block)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# Create an unknown type of entity by default.
|
21
|
-
def to_entity(builder = nil)
|
22
|
-
Treat::Entities::Unknown(self.to_s)
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
# Install Treat functions on String objects.
|
28
|
-
module String
|
29
|
-
|
30
|
-
# Include base proxy functionality.
|
31
|
-
include Treat::Core::Proxies::Proxy
|
32
|
-
|
33
|
-
# Return the entity corresponding to the string.
|
34
|
-
def to_entity
|
35
|
-
Treat::Entities::Entity.from_string(self)
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
# Install Treat functions on Numeric objects.
|
41
|
-
module Numeric
|
42
|
-
|
43
|
-
# Include base proxy functionality.
|
44
|
-
include Treat::Core::Proxies::Proxy
|
45
|
-
|
46
|
-
# Return the entity corresponding to the number.
|
47
|
-
def to_entity(builder = nil)
|
48
|
-
Treat::Entities::Number.from_numeric(self)
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
# Include Treat methods on strings.
|
54
|
-
::String.class_eval do
|
55
|
-
include Treat::Core::Proxies::String
|
56
|
-
end
|
57
|
-
|
58
|
-
# Include Treat methods on numerics.
|
59
|
-
::Numeric.class_eval do
|
60
|
-
include Treat::Core::Proxies::Numeric
|
61
|
-
end
|
62
|
-
|
63
|
-
# This is kind of ugly; need to find a
|
64
|
-
# better solution eventually (?)
|
65
|
-
Treat::Entities::Entity.class_eval do
|
66
|
-
|
67
|
-
# Rename the true language detection
|
68
|
-
# method to :language_proxied, and
|
69
|
-
# only call it if language detection
|
70
|
-
# is turned on in the configuration.
|
71
|
-
alias :language_proxied :language
|
72
|
-
|
73
|
-
# Proxy the #language method, defined on
|
74
|
-
# all textual entities, in order to catch
|
75
|
-
# the method call if language detection is
|
76
|
-
# turned off and return the default language
|
77
|
-
# in that case.
|
78
|
-
def language(extractor = nil, options = {})
|
79
|
-
|
80
|
-
return Treat.core.language.default if
|
81
|
-
!Treat.core.language.detect
|
82
|
-
|
83
|
-
if is_a?(Treat::Entities::Symbol) ||
|
84
|
-
is_a?(Treat::Entities::Number)
|
85
|
-
return Treat.core.language.default
|
86
|
-
end
|
87
|
-
|
88
|
-
dlvl = Treat.core.language.detect_at
|
89
|
-
dklass = Treat::Entities.const_get(cc(dlvl))
|
90
|
-
|
91
|
-
if self.class.compare_with(
|
92
|
-
dklass) < 1 && has_parent?
|
93
|
-
anc = ancestor_with_type(dlvl)
|
94
|
-
return anc.language if anc
|
95
|
-
end
|
96
|
-
|
97
|
-
extractor ||= Treat.workers.
|
98
|
-
extractors.language.default
|
99
|
-
|
100
|
-
language_proxied(extractor, options)
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
class Treat::Workers::Formatters::Unserializers::Autoselect
|
2
|
-
|
3
|
-
def self.unserialize(document, options = {})
|
4
|
-
file = document.file
|
5
|
-
if file.index('yml') || file.index('yaml')
|
6
|
-
document.unserialize(:yaml, options)
|
7
|
-
elsif file.index('xml')
|
8
|
-
document.unserialize(:xml, options)
|
9
|
-
elsif file.index('mongo')
|
10
|
-
document.unserialize(:mongo, options)
|
11
|
-
else
|
12
|
-
raise Treat::Exception,
|
13
|
-
"Unreadable serialized format for file #{file}."
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# This class is a wrapper for the ActiveSupport
|
2
|
-
# declension tools.
|
3
|
-
class Treat::Workers::Inflectors::Declensors::English
|
4
|
-
|
5
|
-
require 'active_support/inflector/inflections'
|
6
|
-
|
7
|
-
# Declense a word using ActiveSupport::Inflector::Inflections
|
8
|
-
def self.declense(entity, options)
|
9
|
-
|
10
|
-
cat = entity.check_has(:category)
|
11
|
-
unless ['noun', 'adjective', 'determiner'].
|
12
|
-
include?(cat)
|
13
|
-
return
|
14
|
-
end
|
15
|
-
|
16
|
-
unless options[:count]
|
17
|
-
raise Treat::Exception,
|
18
|
-
"Must supply option count (:singular or :plural)."
|
19
|
-
end
|
20
|
-
|
21
|
-
string = entity.to_s
|
22
|
-
|
23
|
-
if options[:count] == :plural
|
24
|
-
ActiveSupport::Inflector::Inflections.pluralize(string)
|
25
|
-
elsif options[:count] == :singular
|
26
|
-
ActiveSupport::Inflector::Inflections.singularize(string)
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
# A tokenizer class lifted from the 'tactful-tokenizer' gem.
|
2
|
-
#
|
3
|
-
# Copyright © 2010 Matthew Bunday. All rights reserved.
|
4
|
-
# Released under the GNU GPL v3. Modified by Louis Mullie.
|
5
|
-
#
|
6
|
-
# Project website: https://github.com/SlyShy/Tactful_Tokenizer
|
7
|
-
class Treat::Workers::Processors::Tokenizers::Tactful
|
8
|
-
|
9
|
-
ReTokenize = [
|
10
|
-
# Uniform Quotes
|
11
|
-
[/''|``/, '"'],
|
12
|
-
# Separate punctuation from words.
|
13
|
-
[/(^|\s)(')/, '\1\2'],
|
14
|
-
[/(?=[\("`{\[:;&#*@\.])(.)/, '\1 '],
|
15
|
-
[/(.)(?=[?!\)";}\]*:@\.'])|(?=[\)}\]])(.)|(.)(?=[({\[])|((^|\s)-)(?=[^-])/, '\1 '],
|
16
|
-
# Treat double-hyphen as a single token.
|
17
|
-
[/([^-])(--+)([^-])/, '\1 \2 \3'],
|
18
|
-
[/(\s|^)(,)(?=(\S))/, '\1\2 '],
|
19
|
-
# Only separate a comma if a space follows.
|
20
|
-
[/(.)(,)(\s|$)/, '\1 \2\3'],
|
21
|
-
# Combine dots separated by whitespace to be a single token.
|
22
|
-
[/\.\s\.\s\./, '...'],
|
23
|
-
# Separate "No.6"
|
24
|
-
[/([\W]\.)(\d+)/, '\1 \2'],
|
25
|
-
# Separate words from ellipses
|
26
|
-
[/([^\.]|^)(\.{2,})(.?)/, '\1 \2 \3'],
|
27
|
-
[/(^|\s)(\.{2,})([^\.\s])/, '\1\2 \3'],
|
28
|
-
[/(^|\s)(\.{2,})([^\.\s])/, '\1 \2\3'],
|
29
|
-
##### Some additional fixes.
|
30
|
-
# Fix %, $, &
|
31
|
-
[/(\d)%/, '\1 %'],
|
32
|
-
[/\$(\.?\d)/, '$ \1'],
|
33
|
-
[/(\W)& (\W)/, '\1&\2'],
|
34
|
-
[/(\W\W+)&(\W\W+)/, '\1 & \2'],
|
35
|
-
# Fix (n 't) -> ( n't)
|
36
|
-
[/n 't( |$)/, " n't\\1"],
|
37
|
-
[/N 'T( |$)/, " N'T\\1"],
|
38
|
-
# Treebank tokenizer special words
|
39
|
-
[/([Cc])annot/, '\1an not']
|
40
|
-
|
41
|
-
]
|
42
|
-
|
43
|
-
|
44
|
-
# Tokenize the entity using a rule-based algorithm
|
45
|
-
# that has been lifted from the 'tactful-tokenizer'
|
46
|
-
# gem.
|
47
|
-
def self.tokenize(entity, options = {})
|
48
|
-
|
49
|
-
entity.check_hasnt_children
|
50
|
-
|
51
|
-
s = entity.to_s
|
52
|
-
|
53
|
-
escape_floats!(s)
|
54
|
-
|
55
|
-
ReTokenize.each do |rules|
|
56
|
-
s.gsub!(rules[0], rules[1])
|
57
|
-
end
|
58
|
-
|
59
|
-
s.split(' ').each do |token|
|
60
|
-
|
61
|
-
unescape_floats!(token)
|
62
|
-
entity << Treat::Entities::Token.
|
63
|
-
from_string(token)
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|