Class: Doko

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Doko

Returns a new instance of Doko.



13
14
15
16
17
18
19
20
21
22
# File 'lib/doko.rb', line 13

def initialize(str)
  if str.match( /^#{URI.regexp}$/ )
    str = open(str).read
  end
  if str.match(/<html/i)
    @text =  (Nokogiri::HTML(str)/"body").text
  else
    @text = str
  end
end

Class Method Details

.parse(str) ⇒ Object



9
10
11
# File 'lib/doko.rb', line 9

def self.parse(str)
  self.new(str).parse
end

Instance Method Details

#parseObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/doko.rb', line 24

def parse
  body = @text
  body.tr!("0-9","0-9")
  body.tr!("()","()")
  body.tr!("",",")
  body.tr!(" "," ")
  body.tr!("",".")
  blackchars =  ",()\n"

  addrs = body.scan(/\b([^\s,()]{2,3}(都|道|府|県)[^\s,()]{1,8}(市|区|町|村)[^#{blackchars}]+)/).map{ |m|
    clean(m[0])
  }
  if addrs.empty?
    addrs = body.scan(/([^\s]{1,6}(市|区).{1,8}(区|町|村)[^\s,()]{2,10}\d)/).map{ |m|
      clean(m[0])
    }
  end
  addrs.select{ |a|
    !a.match(//)
  }
end