Class: DwcAgent::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/dwc_agent/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dwc_agent/parser.rb', line 10

def initialize
  options = { 
    prefer_comma_as_separator: true,
    separator: SPLIT_BY,
    title: TITLE
  }
  @namae = Namae::Parser.new(options)

  @strip_out_regex = Regexp.new STRIP_OUT.to_s
  @residual_terminators_regex = Regexp.new SPLIT_BY.to_s + %r{\s*\z}.to_s
  @char_subs_regex = Regexp.new [CHAR_SUBS.keys.join('\\')].to_s
  @phrase_subs_regex = Regexp.new (PHRASE_SUBS.keys.join('|')).to_s
  @complex_separators_regex = Regexp.new COMPLEX_SEPARATORS.to_s
  @add_separators_regex = Regexp.new %r{([A-Z]{1}\.)([[:alpha:]]{2,})}.to_s
end

Class Method Details

.instanceObject



5
6
7
# File 'lib/dwc_agent/parser.rb', line 5

def instance
  Thread.current[:dwc_agent_parser] ||= new
end

Instance Method Details

#parse(name) ⇒ Array

Parses the passed-in string and returns a list of names.

Parameters:

  • names (String)

    the name or names to be parsed

Returns:

  • (Array)

    the list of parsed names



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dwc_agent/parser.rb', line 30

def parse(name)
  return [] if name.nil? || name == ""
  name.gsub!(@strip_out_regex, ' ')
  name.gsub!(@char_subs_regex, CHAR_SUBS)
  name.gsub!(@phrase_subs_regex, PHRASE_SUBS)
  name.gsub!(@add_separators_regex, '\1 \2')
  name.gsub!(@complex_separators_regex, '\1 | \2')
  name.gsub!(@residual_terminators_regex, '')
  name.squeeze!(' ')
  name.strip!
  @namae.parse(name)
end