Class: TextDetector::Detector::Regexp

Inherits:
Base
  • Object
show all
Defined in:
lib/text_detector/detector/regexp.rb

Instance Attribute Summary

Attributes inherited from Base

#dictionary

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from TextDetector::Detector::Base

Instance Method Details

#detect(text) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/text_detector/detector/regexp.rb', line 6

def detect(text)
  matched = @re.match(TextDetector.normalize(text))
  if matched
    offset = matched.offset(0)
    text.slice(offset[0], offset[1] - offset[0])
  else
    nil
  end
end

#detect_all(text) ⇒ Object



16
17
18
19
20
21
# File 'lib/text_detector/detector/regexp.rb', line 16

def detect_all(text)
  TextDetector.normalize(text).to_enum(:scan, @re).map do
    offset = ::Regexp.last_match.offset(0)
    text.slice(offset[0], offset[1] - offset[0])
  end
end