Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
862 aurelien 1
<?php
2
 
3
/**
4
 * Taxamatch-Webservice PHP v1.0.0
5
 * @author Michael Giddens
6
 * @link http://www.silverbiology.com
7
 */
8
 
9
 /* Adapation par David Delon Decembre 2010 : gestion sous espece
10
 */
11
 
12
 
13
	/**
14
	 * Class NameParser
15
	 * Used to convert a string to a standarized format.
16
	 */
17
	class NameParser {
18
 
19
		/**
20
		 * Whether to debug or nor
21
		 * @var bool|integer
22
		 */
23
		public $debug_flag;
24
 
25
 
26
 
27
		/**
28
		 * Constructor
29
		 */
30
		public function __construct( ) {
31
		}
32
 
33
		/**
34
		 * Sets value to the method property
35
		 * @param mixed $name class property name
36
		 * @param mixed $value class property value
37
		 */
38
		public function set($name,$value) {
39
			$this->$name = $value;
40
		}
41
 
42
 
43
		/**
44
		 * Reduce Spaces
45
		 * This will reduce the string to only allow once space between characters
46
		 * @param string $str : string to reduce space
47
		 * @return string : string with only once space between characters
48
		 */
49
		private function reduce_spaces( $str ) {
50
 
51
			$str = preg_replace("/ {2,}/", ' ', $str );
52
			$str = trim( $str );
53
 
54
			return( $str );
55
		}
56
 
57
 
58
		/**
59
		 * Function: parse_auth
60
		 * Purpose: Produce a parsed version of authority of a taxon name
61
		 * @author Tony Rees (Tony.Rees@csiro.au)
62
		 * Date created: March 2008
63
		 * Inputs: authority string as str
64
		 * Remarks:
65
		 *  (1) Performs authority expension of known abbreviated authornames using
66
		 *   table "auth_abbrev_test1" (must be available and populated with relevant content)
67
		 *  (2) Recognises "and", "et", "&" as equivalents (special case for "et al.") - all parsed to ampersand
68
		 *  (3) Recognises (e.g.) "Smith 1980" and "Smith, 1980" as equivalents - comma is removed in these cases
69
		 *  (4) Recognises (e.g.) "F. J. R. Taylor, 1980" and "F.J.R. Taylor, 1980" as equivalents -
70
		 *      extra space after full stops is ignored in these cases
71
		 *  (5) Returns uppercase string, diacritical marks intact
72
		 *
73
		 * @param string $str : authority string
74
		 * @param integer $upcase : convert to uppercase if $upcase = 1
75
		 * @return string : parsed author string
76
		 */
77
		public function parse_auth( $str, $upcase=1 ) {
78
 
79
			$this->debug['parse_auth'][] = "1";
80
			$temp = $str = trim($str);
81
 
82
  		if ( ($str == NULL) || ($str == '') ) {
83
				$this->debug['parse_auth'][] = "1a";
84
		    return '';
85
			}
86
 
87
			if ( ( $temp == null ) || ( $temp == '') ) {
88
				$this->debug['parse_auth'][] = "2a";
89
				return('');
90
			} else {
91
 
92
				$this->debug['parse_auth'][] = "2b";
93
 
94
				// add space after full stops, except at end (NB, will also add spece before some close brackets)
95
				$temp = rtrim( str_replace('.', '. ', $temp) );
96
				$this->debug['parse_auth'][] = "4 (temp:$temp)";
97
 
98
				//normalise "et", "and" to ampersand (et al. is a special case)
99
//				if ( $temp like '% et al%' ) {
995 aurelien 100
				if ( preg_match('/ et al/', $temp) ) {
862 aurelien 101
					$temp = str_replace(' et al','zzzzz', $temp);
102
					$this->debug['parse_auth'][] = "4a (temp:$temp)";
103
				}
104
 
105
				$temp = str_replace(' et ',' & ', $temp );
106
				$temp = str_replace(' and ',' & ', $temp );
995 aurelien 107
 
862 aurelien 108
				$temp = str_replace('zzzzz',' et al', $temp);
109
 
110
				$this->debug['parse_auth'][] = "5 (temp:$temp)";
111
 
112
				//remove commas before dates (only)
113
				//	like '%, 17%'
995 aurelien 114
				if ( preg_match('/, 17/', $temp) ) {
862 aurelien 115
					$temp = str_replace(', 17',' 17', $temp);
116
					$this->debug['parse_auth'][] = "5a (temp:$temp)";
117
				}
118
 
119
				//	like '%, 18%'
995 aurelien 120
				if ( preg_match('/, 18/', $temp) ) {
862 aurelien 121
					$temp = str_replace(', 18',' 18', $temp);
122
					$this->debug['parse_auth'][] = "5b (temp:$temp)";
123
				}
124
 
125
				//	like '%, 19%'
995 aurelien 126
				if ( preg_match('/, 19/', $temp) ) {
862 aurelien 127
					$temp = str_replace(', 19',' 19', $temp);
128
					$this->debug['parse_auth'][] = "5c (temp:$temp)";
129
				}
130
 
131
				//	like '%, 20%'
995 aurelien 132
				if ( preg_match('/, 20/', $temp) ) {
862 aurelien 133
					$temp = str_replace(', 20',' 20', $temp);
134
					$this->debug['parse_auth'][] = "5d (temp:$temp)";
135
				}
136
 
137
				// reduce multiple internal spaces to single space
138
				$temp = $this->reduce_spaces( $temp );
139
 
140
				//	like '% -%'
141
				$temp = str_replace(' -', '-', $temp);
142
 
143
				$this->debug['parse_auth'][] = "6 (temp:$temp)";
144
 
145
				foreach( explode(' ', $temp) as $this_word ) {
146
 
147
					$this->debug['parse_auth'][] = "7 (this_word:$this_word)";
148
 
149
					//	like '(%'
995 aurelien 150
					if ( preg_match('/^\(/', $this_word) ) {
862 aurelien 151
						$elapsed_chars .= '(';
152
						$this_word = substr( $this_word, 1 );
153
						$this->debug['parse_auth'][] = "7a (this_word:$this_word) (elapsed_chars:$elapsed_chars)";
154
					}
155
 
156
					// Add back the word to the final translation
157
					$elapsed_chars .= $this_word . ' ';
158
					$this->debug['parse_auth'][] = "7c (this_word:$this_word) (elapsed_chars:$elapsed_chars)";
159
				}
160
 
161
				$elapsed_chars = $this->reduce_spaces( str_replace(' )', ')', $elapsed_chars) );
162
 
163
				return trim( $elapsed_chars ) ;
164
			}
165
 
166
		}
167
 
168
		/**
169
		 * Function: parse
170
		 * Purpose: Produces parsed version of an input string (scientific name components)
171
		 * @author Tony Rees (Tony.Rees@csiro.au)
172
		 * Date created: June 2007-November 2008
173
		 * Inputs: input string as str (this version presumes genus, genus+species, or
174
		 * genus+species+authority)
175
		 * Outputs: parsed version of input string, for match purposes
176
		 * Remarks:
177
		 *    (1) Removes known text elements e.g.
178
		 *      'aff.', 'cf.', 'subsp.', subgenera if enclosed in brackets, etc. as desired
179
		 *    (2) Removes accented and non A-Z characters other than full stops
180
		 *       (in scientific name portions)
181
		 *    (3) Returns uppercase scientific name (genus + species only)
182
		 *       plus unaltered (presumed) authority
183
		 *     examples;
184
		 *       Anabaena cf. flos-aquae Ralfs ex Born. et Flah. => ANABAENA FLOSAQUAE Ralfs
185
		 *       ex Born. et Flah.
186
		 *       Abisara lemÈe-pauli => ABISARA LEMEEPAULI
187
		 *       Fuc/us Vesiculos2us => FUCUS VESICULOSUS
188
		 *       Buffo ignicolor LacÈpËde, 1788 => BUFFO IGNICOLOR LacÈpËde, 1788
189
		 *       Barbatia (Mesocibota) bistrigata (Dunker, 1866) => BARBATIA BISTRIGATA (Dunker, 1866)
190
		 *    (4) Thus version does not handle genus+author, or genus+species+infraspecies
191
		 *       (second" good" term is presumed to be species epithet, anything after is
192
		 *       considered to be start of the authority), however could be adapted further as required
193
         *         and actually it was done in this version for Tela Botanica
194
		 *    (5) There is a separate function "parse_auth" for normalizing authorities when required
195
		 *      (e.g. for authority comparisons)
196
		 *
197
		 * @param string $str : input string ( genus, genus+species, or genus+species+authority )
198
		 * @return string : parsed string
199
		 */
200
		public function parse( $str = NULL ) {
201
 
202
			unset($this->debug['parse']);
203
 
204
 
205
			$temp = '';
206
			$first_str_part = NULL;
207
			$second_str_part = NULL;
208
			$temp_genus = '';
209
			$temp_species = '';
210
			$temp_genus_species = '';
211
			$temp_authority = '';
212
			$temp_infra = '';
213
 
214
			$this->debug['parse'][] = "1";
215
 
216
			if ( ($str == NULL) || ( trim($str) == '') ) {
217
				$this->debug[] = "N1a<br>";
218
				return '';
219
			} else {
220
				//	trim any leading, trailing spaces or line feeds
221
				$temp = trim( $str );
222
				$this->debug['parse'][] = "1b";
223
			}
224
 
225
			if ( $temp == NULL || $temp == '') {
226
				$this->debug['parse'][] = "2a";
227
				return '';
228
			} else {
229
				$this->debug['parse'][] = "2b";
230
 
231
				// replace any HTML ampersands
232
				$set = array('%', '&', 'amp;%', 'AMP;%');
233
				$temp = str_replace( $set, '&', $temp );
234
 
235
				$this->debug['parse'][] = "2b1 (temp:$temp)";
236
 
237
				// remove any content in angle brackets (e.g. html tags - <i>, </i>, etc.)
238
				$html_pattern = "(\<(/?[^\>]+)\>)";
239
//? This should not just handle html tags but all <*>
240
				$temp = preg_replace( $html_pattern, '', $temp);
241
				$this->debug['parse'][] = "2b2 (temp:$temp)";
242
 
243
				// if second term (only) is in round brackets, presume it is a subgenus or a comment and remove it
244
				// examples: Barbatia (Mesocibota) bistrigata (Dunker, 1866) => Barbatia bistrigata (Dunker, 1866)
245
				// Barbatia (?) bistrigata (Dunker, 1866) => Barbatia bistrigata (Dunker, 1866)
246
				// (obviously this will not suit genus + author alone, where first part of authorname is in brackets,
247
				// however this is very rare?? and in any case we are not supporting genus+authority in this version)
248
//if ( $temp like '% (%)%'
249
				$temp = preg_replace( "/ \(\w*\W*\)/", '', $temp, 1 );
250
//? Not sure if this will catch if
251
				$this->debug['parse'][] = "2b3 (temp:$temp)";
252
 
253
				// if second term (only) is in square brackets, presume it is a comment and remove it
254
				// example: Aphis [?] ficus Theobald, [1918] => Aphis ficus Theobald, [1918]
255
//if ( $temp like '% [%]%'
256
				$temp = preg_replace( "/ \[\w*\W*\]/", '', $temp, 1 );
257
//? Not sure if this will catch if
258
				$this->debug['parse'][] = "2b4 (temp:$temp)";
259
 
260
				// drop indicators of questionable id's - presume all are lowercase for now (could extend as needed)
261
				$temp = preg_replace( "/ cf /", " ", $temp );
262
				$temp = preg_replace( "/ cf\. /", " ", $temp );
263
				$temp = preg_replace( "/ near /", " ", $temp );
264
				$temp = preg_replace( "/ aff\. /", " ", $temp );
265
				$temp = preg_replace( "/ sp\. /", " ", $temp );
266
				$temp = preg_replace( "/ spp\. /", " ", $temp );
267
				$temp = preg_replace( "/ spp /", " ", $temp );
268
 
269
				$this->debug['parse'][] = "2b5 (temp:$temp)";
270
 
271
				// eliminate or close up any stray spaces introduced by the above
272
				$temp = $this->reduce_spaces( $temp );
273
 
274
				$this->debug['parse'][] = "2b6 (temp:$temp)";
275
 
276
				// now presume first element is genus, second (if present) is species, remainder
277
				//   (if present) is authority
278
				// look for genus name
279
				$ar = explode( " ", $temp, 2);
280
				if ( count( $ar ) ) {
281
					$temp_genus = $ar[0];
282
					$temp = @$ar[1];
283
				} else {
284
					$temp_genus = $temp;
285
					$temp = '';
286
				}
287
 
288
				$this->debug['parse'][] = "2b7 (temp_genus:$temp_genus) (temp:$temp)";
289
 
290
				// look for species epithet and authority
291
				$ar = explode( " ", $temp, 2);
292
				if ( count( $ar ) ) {
293
					$temp_species = $ar[0];
294
					$temp_authority = @$ar[1];
295
				} else {
296
					$temp_species = $temp;
297
					$temp_authority = '';
298
				}
299
               	// look for subspecies
300
 
301
                $infras =array('subsp.','var.');
302
 
303
                $temp_authority = preg_replace( "/ssp./", "subsp.", $temp_authority);
304
                $temp_authority = preg_replace( "/ssp /", "subsp.", $temp_authority);
305
                $temp_authority = preg_replace( "/subsp /", "subsp.", $temp_authority);
306
                $temp_authority = preg_replace( "/var /", "var.", $temp_authority);
307
 
308
                foreach ($infras as $infra) {
309
                    $pos = strpos($temp_authority, $infra);
310
                    if ($pos === false) {
311
                        continue;
312
                    }
313
                    else {
314
                        $temp_infra=substr($temp_authority,$pos+strlen($infra));
315
                        $temp_authority=substr($temp_authority,0,$pos);
316
                        $temp_infra=trim($temp_infra);
317
                        $temp_infra_type=$infra;
318
                        // look for infra epithet and authority
319
                        $ar = explode(" ", $temp_infra, 2);
320
                        if ( count( $ar ) ) {
321
                            $temp_infra = $ar[0];
322
                            $temp_infra_authority = @$ar[1];
323
                        }
324
                        break; // on s'arrete au premier trouve
325
                    }
326
                }
327
 
328
				$this->debug['parse'][] = "2b8 (temp_genus:$temp_genus) (temp_species:$temp_species) (temp_authority:$temp_authority) (temp_infra:$temp_infra) (temp_infra_authority:$temp_infra_authority) (temp:$temp)";
329
 
330
 
331
				// replace selected ligatures here (Genus names can contain Æ, OE ligature)
332
				$temp_genus = str_replace( 'Æ', 'AE', $temp_genus);
333
				$temp_species = str_replace( 'Æ', 'AE', $temp_species);
334
				$temp_infra = str_replace( 'Æ', 'AE', $temp_infra );
335
 
336
 
337
				$this->debug['parse'][] = "2b9 (temp_genus:$temp_genus) (temp_species:$temp_species) (temp_authority:$temp_authority) (temp_infra:$temp_infra) (temp_infra_authority:$temp_infra_authority) (temp:$temp)";
338
 
339
                $temp_genus= trim($temp_genus);
340
				$temp_species= trim($temp_species);
341
				$temp_infra= trim($temp_infra );
342
 
343
				// reduce any new multiple internal spaces to single space, if present
344
                $temp_genus= $this->reduce_spaces( $temp_genus );
345
				$temp_species= $this->reduce_spaces( $temp_species );
346
				$temp_infra= $this->reduce_spaces( $temp_infra );
347
 
348
				$this->debug['parse'][] = "2b10 (temp_genus:$temp_genus) (temp_species:$temp_species) (temp_authority:$temp_authority) (temp_infra:$temp_infra) (temp_infra_authority:$temp_infra_authority) (temp:$temp)";
349
 
350
                if (isset($temp_authority) && ($temp_authority!='') ) {
351
                    $temp_authority=$this->parse_auth($temp_authority);
352
                }
353
 
354
                if (isset($temp_infra_authority) && ($temp_infra_authority!='') ) {
355
                    $temp_infra_authority=$this->parse_auth($temp_infra_authority);
356
                }
357
 
358
 
359
				$this->debug['parse'][] = "2b11 (temp_genus:$temp_genus) (temp_species:$temp_species) (temp_authority:$temp_authority) (temp_infra:$temp_infra) (temp_infra_authority:$temp_infra_authority) (temp:$temp)";
360
 
361
				return array("genus"=>$temp_genus, "species"=>$temp_species, "authority"=>$temp_authority, "infra"=>$temp_infra, "infra_authority"=>$temp_infra_authority, "infra_type"=>$temp_infra_type);
362
 
363
			}
364
 
365
		} // End NameParser
366
 
367
 
368
 
369
	} // End Class
370
 
371
?>