Class: Phrasify::Text
- Inherits:
-
String
- Object
- String
- Phrasify::Text
- Defined in:
- lib/phrasify/text.rb
Instance Method Summary collapse
- #initialize(string, phrase_klass = Phrase) ⇒ Phrasify::Text constructor
- #parse ⇒ Phrasify::Text
-
#phraseable_words ⇒ Array<String>
Unique list of words that could have a matching phrase.
-
#phrases ⇒ Array[<Phrase>]
Phrases that could match text.
-
#words ⇒ Array<String>
Text broken down into words with all punctuation removed.
Constructor Details
#initialize(string, phrase_klass = Phrase) ⇒ Phrasify::Text
11 12 13 14 |
# File 'lib/phrasify/text.rb', line 11 def initialize(string, phrase_klass = Phrase) @phrase_klass = phrase_klass super(string) end |
Instance Method Details
#parse ⇒ Phrasify::Text
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/phrasify/text.rb', line 20 def parse phrases.inject(self) do |string, phrase| string = string.split(":phrasify:").inject("") do |new_string, part| # If this part of the string has already been matched we won't try # and match it again. if part.include?(":matched:") new_string += part else new_string += part.gsub( phrase.match, ":phrasify::matched:#{phrase.replace}:matched::phrasify:" ) end end end.gsub(":matched:", "").gsub(":phrasify:", "") end |
#phraseable_words ⇒ Array<String>
Unique list of words that could have a matching phrase
51 52 53 |
# File 'lib/phrasify/text.rb', line 51 def phraseable_words words - COMMON_WORDS end |
#phrases ⇒ Array[<Phrase>]
Phrases that could match text
59 60 61 |
# File 'lib/phrasify/text.rb', line 59 def phrases @phrase_klass.search(phraseable_words) end |
#words ⇒ Array<String>
Text broken down into words with all punctuation removed
43 44 45 |
# File 'lib/phrasify/text.rb', line 43 def words gsub(/[^A-Za-z0-9_-|\s]/, '').downcase.split(" ").uniq end |