Class: TwitterToCsv::BoolWordFieldParser
- Inherits:
-
Object
- Object
- TwitterToCsv::BoolWordFieldParser
- Defined in:
- lib/twitter_to_csv/bool_word_field_parser.rb
Constant Summary collapse
- TOKEN_SEPARATOR =
/[^a-zA-Z0-9-]+/
Class Method Summary collapse
- .check(pattern, text) ⇒ Object
- .descend_check(logic, tokens) ⇒ Object
- .descend_parse(struct, tokens) ⇒ Object
- .parse(string) ⇒ Object
Class Method Details
.check(pattern, text) ⇒ Object
45 46 47 48 49 |
# File 'lib/twitter_to_csv/bool_word_field_parser.rb', line 45 def self.check(pattern, text) logic = pattern[:logic] tokens = text.downcase.split(TOKEN_SEPARATOR).reject {|t| t.length == 0 }.join(" ") !!descend_check(logic, tokens) end |
.descend_check(logic, tokens) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/twitter_to_csv/bool_word_field_parser.rb', line 51 def self.descend_check(logic, tokens) if logic.is_a?(String) # See if the token(s) are present. tokens =~ /\b#{Regexp::escape logic}\b/ elsif logic.length == 1 # Recurse further. descend_check logic.first, tokens elsif logic.length == 3 # Apply the given logical operation. first = descend_check(logic.first, tokens) last = descend_check(logic.last, tokens) if logic[1] == :and first && last elsif logic[1] == :or first || last else raise InvalidLogicError.new("Unknown operation: #{logic[1]}") end else raise InvalidLogicError.new("Invalid expression length of #{logic.length}") end end |
.descend_parse(struct, tokens) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/twitter_to_csv/bool_word_field_parser.rb', line 18 def self.descend_parse(struct, tokens) while tokens.length > 0 token = tokens.shift if token == ")" return elsif token == "(" if struct.length > 0 sub_struct = [] struct << sub_struct descend_parse(sub_struct, tokens) end elsif %w[AND OR].include?(token) sub_struct = [] struct << :and if token == "AND" struct << :or if token == "OR" struct << sub_struct descend_parse(sub_struct, tokens) else if struct[0] struct[0] += " " + token.downcase else struct << token.downcase end end end end |
.parse(string) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/twitter_to_csv/bool_word_field_parser.rb', line 9 def self.parse(string) parts = string.split(":") name = parts.shift tokens = parts.join(":").gsub(/\)/, " ) ").gsub(/\(/, " ( ").split(/\s+/).reject {|s| s.length == 0 } struct = [] descend_parse(struct, tokens) { :name => name, :logic => struct } end |