Class: Taxamatch::Base

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

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



17
18
19
20
# File 'lib/taxamatch_rb.rb', line 17

def initialize
  @parser = Taxamatch::Atomizer.new
  @dlm = Taxamatch::DamerauLevenshteinMod.new
end

Instance Method Details

#match_authors(preparsed_1, preparsed_2) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/taxamatch_rb.rb', line 82

def match_authors(preparsed_1, preparsed_2)
  au1 = preparsed_1[:all_authors]
  au2 = preparsed_2[:all_authors]
  yr1 = preparsed_1[:all_years]
  yr2 = preparsed_2[:all_years]
  Taxamatch::Authmatch.authmatch(au1, au2, yr1, yr2)
end

#match_genera(genus1, genus2) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/taxamatch_rb.rb', line 55

def match_genera(genus1, genus2)
  genus1_length = genus1[:normalized].size
  genus2_length = genus2[:normalized].size
  match = false
  ed = @dlm.distance(genus1[:normalized], genus2[:normalized],1,3) #TODO put block = 2
  return {'edit_distance' => ed, 'phonetic_match' => false, 'match' => false} if ed/[genus1_length, genus2_length].min > 0.2
  return {'edit_distance' => ed, 'phonetic_match' => true, 'match' => true} if genus1[:phonetized] == genus2[:phonetized] 

  match = true if ed <= 3 && ([genus1_length, genus2_length].min > ed * 2) && (ed < 2 || genus1[0] == genus2[0])
  {'edit_distance' => ed, 'match' => match, 'phonetic_match' => false} 
end

#match_matches(genus_match, species_match, infraspecies_matches = []) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/taxamatch_rb.rb', line 90

def match_matches(genus_match, species_match, infraspecies_matches = []) 
  match = species_match
  match['edit_distance'] += genus_match['edit_distance']
  match['match'] = false if match['edit_distance'] > 4
  match['match'] &&= genus_match['match']
  match['phonetic_match'] &&= genus_match['phonetic_match']
  match
end

#match_multinomial(preparsed_1, preparsed_2) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/taxamatch_rb.rb', line 47

def match_multinomial(preparsed_1, preparsed_2)
  gen_match = match_genera(preparsed_1[:genus], preparsed_2[:genus])
  sp_match = match_species(preparsed_1[:species], preparsed_2[:species])
  total_length = preparsed_1[:genus][:epitheton].size + preparsed_2[:genus][:epitheton].size + preparsed_1[:species][:epitheton].size + preparsed_2[:species][:epitheton].size
  match = match_matches(gen_match, sp_match)
  match.merge({'score' => (1 - match['edit_distance']/(total_length/2))})
end

#match_species(sp1, sp2) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/taxamatch_rb.rb', line 67

def match_species(sp1, sp2)
  sp1_length = sp1[:normalized].size
  sp2_length = sp2[:normalized].size
  sp1[:phonetized] = Taxamatch::Phonetizer.normalize_ending sp1[:phonetized]
  sp2[:phonetized] = Taxamatch::Phonetizer.normalize_ending sp2[:phonetized]
  match = false
  ed = @dlm.distance(sp1[:normalized], sp2[:normalized], 1, 4) #TODO put block 4
  return {'edit_distance' => ed, 'phonetic_match' => false, 'match' => false} if ed/[sp1_length, sp2_length].min > 0.3334
  #puts 's: %s, %s, %s' % [sp1[:normalized], sp2[:normalized], ed]
  return {'edit_distance' => ed, 'phonetic_match' => true, 'match' => true} if sp1[:phonetized] == sp2[:phonetized]

  match = true if ed <= 4 && ([sp1_length, sp2_length].min >= ed * 2) && (ed < 2 || sp1[:normalized][0] == sp2[:normalized][0]) && (ed < 4 || sp1[:normalized][0...3] == sp2[:normalized][0...3])
  { 'edit_distance' => ed, 'match' => match, 'phonetic_match' => false}
end

#match_uninomial(preparsed_1, preparsed_2) ⇒ Object



43
44
45
# File 'lib/taxamatch_rb.rb', line 43

def match_uninomial(preparsed_1, preparsed_2)
  return false
end

#taxamatch(str1, str2, return_boolean = true) ⇒ Object

takes two scientific names and returns true if names match and false if they don’t



24
25
26
27
28
29
# File 'lib/taxamatch_rb.rb', line 24

def taxamatch(str1, str2, return_boolean = true) 
  preparsed_1 = @parser.parse(str1)
  preparsed_2 = @parser.parse(str2)
  match = taxamatch_preparsed(preparsed_1, preparsed_2) rescue nil
  return_boolean && match ? match['match'] : match
end

#taxamatch_preparsed(preparsed_1, preparsed_2) ⇒ Object

takes two hashes of parsed scientific names, analyses them and returns back this function is useful when species strings are preparsed.



33
34
35
36
37
38
39
40
41
# File 'lib/taxamatch_rb.rb', line 33

def taxamatch_preparsed(preparsed_1, preparsed_2)
  result = nil
  result =  match_uninomial(preparsed_1, preparsed_2) if preparsed_1[:uninomial] && preparsed_2[:uninomial] 
  result =  match_multinomial(preparsed_1, preparsed_2) if preparsed_1[:genus] && preparsed_2[:genus]
  if result && result['match']
    result['match'] = match_authors(preparsed_1, preparsed_2) == 0 ? false : true 
  end
  return result
end