Class: Cardreader::Manage

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

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Manage

Returns a new instance of Manage.



4
5
6
7
8
9
# File 'lib/cardreader/manage.rb', line 4

def initialize(key)
	@vision = Vision.new(key)
	@lang = Lang.new(key)
	@results = {person: "", organization: "", location: "", 
               mail: "", url: "", phone: ""}
end

Instance Method Details

#detect(image) ⇒ Object



35
36
37
38
39
40
# File 'lib/cardreader/manage.rb', line 35

def detect(image)
	@detected_text = @vision.post(image)
	preprocess
	text = @lang.post(@detected_text)
	make(text)
end

#distill(keyword, reg) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/cardreader/manage.rb', line 11

def distill(keyword, reg)
	matched = @detected_text.match(reg)
	
	if matched
		matched = matched[0]
		@detected_text.gsub!(/#{matched.gsub(/\+/,'')}/, "")
		@results[keyword] = matched
	end
end

#make(text) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cardreader/manage.rb', line 42

def make(text)
	text.each do |t|
		case t["type"]
		when "PERSON"
			@results[:person] = t["name"]
		when "ORGANIZATION"
			@results[:organization] = " " + t["name"] + @results[:organization]
		when "LOCATION"
			@results[:location] = " " + t["name"] + @results[:location]
		else
		end
	end	
	@results.to_json
end

#preprocessObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cardreader/manage.rb', line 21

def preprocess
   reg= /[^\s]+[\s]*(Corp|Ltd|Inc|Co|株式会社)/
	distill(:company, reg)
	
	reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
	distill(:mail, reg)
	
	reg =  /(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?/
	distill(:url, reg)
	
   reg = /(\+[\dO]{2}[\-])?[\dO]{2,5}[\-(][\dO]{1,4}[\-)][\dO]{4}/
	distill(:phone, reg)
end