class PhoneParser

Parses text and attempts to locate phone numbers

Constants

BASE_MATCHING

Base matching for a possible phone number digit

GR_REGEX

The final regex used to match phone numbers for GR

LEET_REPLACEMENTS

Replacements used for l33t for MR

LEET_SPEAK

L33t speak versions of numbers

MR_REGEX

The final regex used to match phone numbers for MR

MULTI_SPACE

Handles multiple spaces

PHONETICS

Phonetic versions of numbers

REGEX_LEET_SPEAK

Regex for l33t, both with spaces and otherwise

REGEX_LEET_SPEAK_SPACED
REGEX_PHONETICS

Regex for phonetics, both with spaces and otherwise

REGEX_PHONETICS_SPACED
REPLACEMENTS

Replacements used for phonetics for MR

Public Instance Methods

count_phone_number_instances(text, options) click to toggle source

Counts the number of phone number instances that occur within the block of text

# File lib/ramparts/parsers/phone_parser.rb, line 8
def count_phone_number_instances(text, options)
  raise ArgumentError, ARGUMENT_ERROR_TEXT unless text.is_a? String

  parsed_text = parse_phone_number(text, options)

  # Uses the map reduce algorithm
  phone_number_instances(MR_ALGO, parsed_text, options).length
end
find_phone_number_instances(text, options) click to toggle source

Finds phone number instances within the block of text

# File lib/ramparts/parsers/phone_parser.rb, line 26
def find_phone_number_instances(text, options)
  raise ArgumentError, ARGUMENT_ERROR_TEXT unless text.is_a? String

  text = text.downcase

  # Finds the phone number instances using the glorified regex algorithm
  phone_number_instances(GR_ALGO, text, options)
end
replace_phone_number_instances(text, options, &block) click to toggle source

Replaces phone number instances within the block of text with the insertable

# File lib/ramparts/parsers/phone_parser.rb, line 18
def replace_phone_number_instances(text, options, &block)
  raise ArgumentError, ARGUMENT_ERROR_TEXT unless text.is_a? String

  instances = find_phone_number_instances(text, options)
  replace(text, instances.reverse!, &block)
end

Private Instance Methods

parse_phone_number(text, options) click to toggle source

Parses the phone number for MR, uses a variety of options

# File lib/ramparts/parsers/phone_parser.rb, line 117
def parse_phone_number(text, options)
  text = text.delete(' ') if options.fetch(:remove_spaces, true)
  text = text.downcase.gsub(/#{REGEX_PHONETICS}/, REPLACEMENTS)
  text = text.gsub(/#{REGEX_LEET_SPEAK}/, LEET_REPLACEMENTS) if options.fetch(:parse_leet, true)
  text.gsub(/[^\w]/, '-').gsub(/[a-z]/, '.')
end
phone_number_instances(algo, text, _options) click to toggle source

Returns the phone number instances using the specified algorithm

# File lib/ramparts/parsers/phone_parser.rb, line 125
def phone_number_instances(algo, text, _options)
  # Determines which algorithm to use
  regex = algo == MR_ALGO ? MR_REGEX : GR_REGEX

  instances = scan(text, regex, :phone)
  instances
end