Class: Phrasify::Text

Inherits:
String
  • Object
show all
Defined in:
lib/phrasify/text.rb

Instance Method Summary collapse

Constructor Details

#initialize(string, phrase_klass = Phrase) ⇒ Phrasify::Text

Parameters:

  • string (String)

    The string you want to match phrases against

  • phrase_klass (#search) (defaults to: Phrase)

    A class that responds to search with an array of strings



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

#parsePhrasify::Text

Returns:



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_wordsArray<String>

Unique list of words that could have a matching phrase

Returns:

  • (Array<String>)


51
52
53
# File 'lib/phrasify/text.rb', line 51

def phraseable_words
  words - COMMON_WORDS
end

#phrasesArray[<Phrase>]

Phrases that could match text

Returns:



59
60
61
# File 'lib/phrasify/text.rb', line 59

def phrases
  @phrase_klass.search(phraseable_words)
end

#wordsArray<String>

Text broken down into words with all punctuation removed

Returns:

  • (Array<String>)


43
44
45
# File 'lib/phrasify/text.rb', line 43

def words
  gsub(/[^A-Za-z0-9_-|\s]/, '').downcase.split(" ").uniq
end