Class: TextNlp::Expressions
- Inherits:
-
Object
- Object
- TextNlp::Expressions
- Defined in:
- lib/text_nlp/expressions.rb
Instance Attribute Summary collapse
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
- #<<(expression) ⇒ Object
- #any?(text) ⇒ Boolean
- #expressionize(text) ⇒ Object
- #find(text) ⇒ Object
-
#initialize(expressions = []) ⇒ Expressions
constructor
A new instance of Expressions.
Constructor Details
#initialize(expressions = []) ⇒ Expressions
Returns a new instance of Expressions.
8 9 10 11 |
# File 'lib/text_nlp/expressions.rb', line 8 def initialize(expressions = []) @root, @values = {}, [] expressions.each { |expr| self << expr } end |
Instance Attribute Details
#values ⇒ Object
Returns the value of attribute values.
6 7 8 |
# File 'lib/text_nlp/expressions.rb', line 6 def values @values end |
Instance Method Details
#<<(expression) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/text_nlp/expressions.rb', line 13 def <<(expression) node = @root expression.normalize! @values << expression tokens = expression.tokenize tokens_count = tokens.size tokens.each_with_index do |token,i| unless node.key?(token) node[token] = {} node[token][:parent] = node end node = node[token] if (i == (tokens_count-1)) # leaf node[:leaf] = 1 end end end |
#any?(text) ⇒ Boolean
31 32 33 |
# File 'lib/text_nlp/expressions.rb', line 31 def any?(text) (find(text).size > 0) end |
#expressionize(text) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/text_nlp/expressions.rb', line 35 def expressionize(text) expressions = find(text).sort { |e1,e2| e2.tokenize.size <=> e1.tokenize.size } text = text.tokenize.join(',') expressions.each { |expr| text.gsub!(expr.tokenize.join(','), expr) } text.split(',') end |
#find(text) ⇒ Object
42 43 44 |
# File 'lib/text_nlp/expressions.rb', line 42 def find(text) find_expressions(0,text.normalize.tokenize) end |