-
Notifications
You must be signed in to change notification settings - Fork 178
/
taxon_name.rb
199 lines (180 loc) · 6.54 KB
/
taxon_name.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class TaxonName < ActiveRecord::Base
belongs_to :taxon
belongs_to :source
belongs_to :creator, :class_name => 'User'
belongs_to :updater, :class_name => 'User'
validates_presence_of :taxon
validates_associated :taxon
validates_length_of :name, :within => 1..256
validates_uniqueness_of :name,
:scope => [:lexicon, :taxon_id],
:message => "already exists for this taxon in this lexicon",
:case_sensitive => false
validates_uniqueness_of :source_identifier,
:scope => [:source_id],
:message => "already exists",
:allow_blank => true
#TODO is the validates uniqueness correct? Allows duplicate TaxonNames to be created with same
#source_url but different taxon_ids
before_validation :strip_tags, :strip_name, :remove_rank_from_name, :normalize_lexicon
before_validation do |tn|
tn.name = tn.name.capitalize if tn.lexicon == LEXICONS[:SCIENTIFIC_NAMES]
end
after_create {|name| name.taxon.set_scientific_taxon_name}
after_save :update_unique_names
after_destroy {|name| name.taxon.send_later(:update_unique_name, :dj_priority => 1) if name.taxon}
LEXICONS = {
:SCIENTIFIC_NAMES => 'Scientific Names',
:AFRIKAANS => 'Afrikaans',
:BENGALI => 'Bengali',
:CEBUANO => 'Cebuano',
:CREOLE_FRENCH => 'creole (French)',
:CREOLE_PORTUGUESE => 'creole (Portuguese)',
:DAVAWENYO => 'Davawenyo',
:DUTCH => 'Dutch',
:ENGLISH => 'English',
:FRENCH => 'French',
:GELA => 'Gela',
:GERMAN => 'German',
:HAWAIIAN => 'Hawaiian',
:HEBREW => 'Hebrew',
:HILIGAYNON => 'Hiligaynon',
:ILOKANO => 'Ilokano',
:ITALIAN => 'Italian',
:JAPANESE => 'Japanese',
:KOREAN => 'Korean',
:MALTESE => 'Maltese',
:MAORI => 'Maori',
:MISIMA_PANEATI => 'Misima-paneati',
:NORWEGIAN => 'Norwegian',
:PANGASINAN => 'Pangasinan',
:PORTUGUESE => 'Portuguese',
:RUMANIAN => 'Rumanian',
:RUSSIAN => 'Russian',
:SPANISH => 'Spanish',
:SWEDISH => 'Swedish',
:TAGALOG => 'Tagalog',
:TAHITIAN => 'Tahitian',
:TOKELAUAN => 'Tokelauan',
:TURKISH => 'Turkish',
:WARAY_WARAY => 'Waray-Waray'
}
DEFAULT_LEXICONS = [
LEXICONS[:SCIENTIFIC_NAMES],
LEXICONS[:ENGLISH]
]
LEXICONS.each do |k,v|
class_eval <<-EOT
def is_#{k.to_s.downcase}?
lexicon == "#{v}"
end
EOT
const_set k.to_s.upcase, v
end
alias :is_scientific? :is_scientific_names?
def to_s
"<TaxonName #{self.id}: #{self.name} in #{self.lexicon}>"
end
def strip_name
self.name.strip! if self.name
true
end
def strip_tags
self.name.gsub!(/<.*?>/, '')
true
end
def remove_rank_from_name
return unless self.lexicon == LEXICONS[:SCIENTIFIC_NAMES]
self.name = Taxon.remove_rank_from_name(self.name)
true
end
def normalize_lexicon
return true if lexicon.blank?
self.lexicon = LEXICONS[lexicon.upcase.to_sym] if LEXICONS[lexicon.upcase.to_sym]
true
end
def update_unique_names
return true unless name_changed?
non_unique_names = TaxonName.all(:include => :taxon,
:conditions => {:name => name}, :select => "DISTINCT ON (taxon_id) *")
non_unique_names.each do |taxon_name|
taxon_name.taxon.update_unique_name if taxon_name.taxon
end
true
end
def self.choose_common_name(taxon_names)
return nil if taxon_names.blank?
common_names = taxon_names.reject { |tn| tn.is_scientific_names? }
return nil if common_names.blank?
common_names = common_names.sort_by(&:id)
engnames = common_names.select {|n| n.is_english?}
unknames = common_names.select {|n| n.lexicon == 'unspecified'}
if engnames.length > 0
engnames.first
elsif unknames.length > 0
unknames.first
else
common_names.first
end
end
def self.choose_scientific_name(taxon_names)
return nil if taxon_names.blank?
taxon_names.select { |tn| tn.is_valid? && tn.is_scientific_names? }.first
end
def self.choose_default_name(taxon_names)
return nil if taxon_names.blank?
name = choose_common_name(taxon_names)
name ||= choose_scientific_name(taxon_names)
name ||= taxon_names.first
name
end
def self.find_external(q, options = {})
ratatosk = case options[:src]
when 'ubio'
Ratatosk::Ratatosk.new(:name_providers => [Ratatosk::NameProviders::UBioNameProvider.new])
when 'col'
Ratatosk::Ratatosk.new(:name_providers => [Ratatosk::NameProviders::ColNameProvider.new])
when 'nzor'
Ratatosk::Ratatosk.new(:name_providers => [Ratatosk::NameProviders::NZORNameProvider.new])
else
Ratatosk
end
# fetch names and save them
ratatosk.find(q).map do |ext_name|
unless ext_name.valid?
if existing_taxon = ratatosk.find_existing_taxon(ext_name.taxon)
ext_name.taxon = existing_taxon
end
end
ext_name.save ? ext_name : nil
end.compact
end
def self.find_single(name, options = {})
lexicon = options.delete(:lexicon) # || TaxonName::LEXICONS[:SCIENTIFIC_NAMES]
name = name.split('_').join(' ')
name = Taxon.remove_rank_from_name(name)
conditions = {:name => name}
conditions[:lexicon] = lexicon if lexicon
begin
taxon_names = TaxonName.all(:conditions => conditions, :limit => 10, :include => :taxon)
rescue ActiveRecord::StatementInvalid => e
raise e unless e.message =~ /invalid byte sequence/
conditions[:name] = Iconv.iconv('UTF8', 'LATIN1', name).first
taxon_names = TaxonName.all(:conditions => conditions, :limit => 10, :include => :taxon)
end
unless options[:iconic_taxa].blank?
taxon_names.reject {|tn| options[:iconic_taxa].include?(tn.taxon.iconic_taxon_id)}
end
taxon_names.detect{|tn| tn.is_valid?} || taxon_names.first
end
def self.strip_author(name)
name = name.gsub(' hor ', ' ')
name = name.gsub(' de ', ' ')
name = name.gsub(/\(.*?\).*/, '')
name = name.gsub(/\[.*?\]/, '')
name = name.gsub(/[\w\.,]+\s+\d+.*/, '')
name = name.gsub(/\w[\.,]+.*/, '')
name = name.gsub(/\s+[A-Z].*/, '')
name.strip
end
end