Project

General

Profile

« Previous | Next » 

Revision 13871

inputs/.TNRS/schema.sql: taxon_match__fill(): split into separate DECLARE blocks for each field for clarity

View differences:

schema.sql
113 113
CREATE FUNCTION taxon_match__fill() RETURNS trigger
114 114
    LANGUAGE plpgsql
115 115
    AS $$
116
DECLARE
117
	"Specific_epithet_is_plant" boolean :=
118
		(CASE
119
		WHEN   new."Infraspecific_epithet_matched"	 IS NOT NULL
120
			OR new."Infraspecific_epithet_2_matched" IS NOT NULL
121
			OR new."Specific_epithet_score" >= 0.9 -- fuzzy match
122
			THEN true
123
		ELSE NULL -- ambiguous
124
		END);
125
	never_homonym boolean = COALESCE(new."Author_score" >= 0.6, false);
126
		-- author disambiguates
127
	family_is_homonym boolean = NOT never_homonym
128
		AND "TNRS".family_is_homonym(new."Family_matched");
129
	genus_is_homonym  boolean = NOT never_homonym
130
		AND "TNRS".genus_is_homonym(new."Genus_matched");
131
	matched_taxon_name_with_author text = NULLIF(concat_ws(' '
132
		, NULLIF(NULLIF(new."Name_matched_accepted_family", 'Unknown'),
133
			new."Name_matched")
134
		, NULLIF(new."Name_matched", 'No suitable matches found.')
135
		, new."Name_matched_author"
136
		), '');
137
	accepted_taxon_name_with_author text = NULLIF(concat_ws(' '
138
		, NULLIF(NULLIF(new."Accepted_name_family", 'Unknown'),
139
			new."Accepted_name")
140
		, new."Accepted_name"
141
		, new."Accepted_name_author"
142
		), '');
143 116
BEGIN
144
	/* exclude homonyms because these are not valid matches (i.e. TNRS provides
145
	a name, but the name is not meaningful because it is not unambiguous). */
146
	new.is_valid_match = new."Taxonomic_status" != 'Invalid'
147
		AND COALESCE(CASE
148
		WHEN new."Family_score" = 1 AND NOT family_is_homonym -- exact match
149
			THEN true
150
		ELSE -- consider genus
117
	DECLARE
118
		"Specific_epithet_is_plant" boolean :=
151 119
			(CASE
152
			WHEN new."Genus_score" =  1	   -- exact match
153
				THEN
120
			WHEN   new."Infraspecific_epithet_matched"	 IS NOT NULL
121
				OR new."Infraspecific_epithet_2_matched" IS NOT NULL
122
				OR new."Specific_epithet_score" >= 0.9 -- fuzzy match
123
				THEN true
124
			ELSE NULL -- ambiguous
125
			END);
126
		never_homonym boolean = COALESCE(new."Author_score" >= 0.6, false);
127
			-- author disambiguates
128
		family_is_homonym boolean = NOT never_homonym
129
			AND "TNRS".family_is_homonym(new."Family_matched");
130
		genus_is_homonym  boolean = NOT never_homonym
131
			AND "TNRS".genus_is_homonym(new."Genus_matched");
132
	BEGIN
133
		/* exclude homonyms because these are not valid matches (TNRS provides a
134
		name, but the name is not meaningful because it is not unambiguous) */
135
		new.is_valid_match = new."Taxonomic_status" != 'Invalid'
136
			AND COALESCE(CASE
137
			WHEN new."Family_score" = 1 AND NOT family_is_homonym -- exact match
138
				THEN true
139
			ELSE -- consider genus
154 140
				(CASE
155
				WHEN NOT genus_is_homonym THEN true
156
				ELSE "Specific_epithet_is_plant"
141
				WHEN new."Genus_score" =  1	   -- exact match
142
					THEN
143
					(CASE
144
					WHEN NOT genus_is_homonym THEN true
145
					ELSE "Specific_epithet_is_plant"
146
					END)
147
				WHEN new."Genus_score" >= 0.85 -- fuzzy match
148
					THEN "Specific_epithet_is_plant"
149
				ELSE NULL -- ambiguous
157 150
				END)
158
			WHEN new."Genus_score" >= 0.85 -- fuzzy match
159
				THEN "Specific_epithet_is_plant"
160
			ELSE NULL -- ambiguous
161
			END)
162
		END, false);
163
	new.scrubbed_unique_taxon_name = COALESCE(
164
		accepted_taxon_name_with_author, matched_taxon_name_with_author);
151
			END, false);
152
	END;
165 153
	
154
	DECLARE
155
		matched_taxon_name_with_author text = NULLIF(concat_ws(' '
156
			, NULLIF(NULLIF(new."Name_matched_accepted_family", 'Unknown'),
157
				new."Name_matched")
158
			, NULLIF(new."Name_matched", 'No suitable matches found.')
159
			, new."Name_matched_author"
160
			), '');
161
		accepted_taxon_name_with_author text = NULLIF(concat_ws(' '
162
			, NULLIF(NULLIF(new."Accepted_name_family", 'Unknown'),
163
				new."Accepted_name")
164
			, new."Accepted_name"
165
			, new."Accepted_name_author"
166
			), '');
167
	BEGIN
168
		new.scrubbed_unique_taxon_name = COALESCE(
169
			accepted_taxon_name_with_author, matched_taxon_name_with_author);
170
	END;
171
	
166 172
	RETURN new;
167 173
END;
168 174
$$;

Also available in: Unified diff