Subversion Repositories eFlore/Applications.cel

Rev

Rev 590 | Rev 1080 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
540 david 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%' ) {
100
				if ( ereg(' et al', $temp) ) {
101
					$temp = str_replace(' et al','zzzzz', $temp);
102
					$this->debug['parse_auth'][] = "4a (temp:$temp)";
103
				}
104
 
105
//				$temp = str_replace(temp,' et ',' '||'&'||' ');
106
//				$temp = replace(temp,' and ',' '||'&'||' ');
107
				$temp = str_replace(' et ',' & ', $temp );
108
				$temp = str_replace(' and ',' & ', $temp );
109
//				if ( $temp like '%zzzzz%' ) {
110
//				if ( ereg('zzzzz', $temp) ) {
111
				$temp = str_replace('zzzzz',' et al', $temp);
112
//				}
113
 
114
				$this->debug['parse_auth'][] = "5 (temp:$temp)";
115
 
116
				//remove commas before dates (only)
117
				//	like '%, 17%'
118
				if ( ereg(', 17', $temp) ) {
119
					$temp = str_replace(', 17',' 17', $temp);
120
					$this->debug['parse_auth'][] = "5a (temp:$temp)";
121
				}
122
 
123
				//	like '%, 18%'
124
				if ( ereg(', 18', $temp) ) {
125
					$temp = str_replace(', 18',' 18', $temp);
126
					$this->debug['parse_auth'][] = "5b (temp:$temp)";
127
				}
128
 
129
				//	like '%, 19%'
130
				if ( ereg(', 19', $temp) ) {
131
					$temp = str_replace(', 19',' 19', $temp);
132
					$this->debug['parse_auth'][] = "5c (temp:$temp)";
133
				}
134
 
135
				//	like '%, 20%'
136
				if ( ereg(', 20', $temp) ) {
137
					$temp = str_replace(', 20',' 20', $temp);
138
					$this->debug['parse_auth'][] = "5d (temp:$temp)";
139
				}
140
 
141
				// reduce multiple internal spaces to single space
142
				$temp = $this->reduce_spaces( $temp );
143
 
144
				//	like '% -%'
145
				$temp = str_replace(' -', '-', $temp);
146
 
147
				$this->debug['parse_auth'][] = "6 (temp:$temp)";
148
 
149
				foreach( explode(' ', $temp) as $this_word ) {
150
 
151
					$this->debug['parse_auth'][] = "7 (this_word:$this_word)";
152
 
153
					//	like '(%'
154
					if ( ereg('^\(', $this_word) ) {
155
						$elapsed_chars .= '(';
156
						$this_word = substr( $this_word, 1 );
157
						$this->debug['parse_auth'][] = "7a (this_word:$this_word) (elapsed_chars:$elapsed_chars)";
158
					}
159
 
160
					// Add back the word to the final translation
161
					$elapsed_chars .= $this_word . ' ';
162
					$this->debug['parse_auth'][] = "7c (this_word:$this_word) (elapsed_chars:$elapsed_chars)";
163
				}
164
 
165
				$elapsed_chars = $this->reduce_spaces( str_replace(' )', ')', $elapsed_chars) );
166
 
167
				return trim( $elapsed_chars ) ;
168
			}
169
 
170
		}
171
 
172
		/**
173
		 * Function: parse
174
		 * Purpose: Produces parsed version of an input string (scientific name components)
175
		 * @author Tony Rees (Tony.Rees@csiro.au)
176
		 * Date created: June 2007-November 2008
177
		 * Inputs: input string as str (this version presumes genus, genus+species, or
178
		 * genus+species+authority)
179
		 * Outputs: parsed version of input string, for match purposes
180
		 * Remarks:
181
		 *    (1) Removes known text elements e.g.
182
		 *      'aff.', 'cf.', 'subsp.', subgenera if enclosed in brackets, etc. as desired
183
		 *    (2) Removes accented and non A-Z characters other than full stops
184
		 *       (in scientific name portions)
185
		 *    (3) Returns uppercase scientific name (genus + species only)
186
		 *       plus unaltered (presumed) authority
187
		 *     examples;
188
		 *       Anabaena cf. flos-aquae Ralfs ex Born. et Flah. => ANABAENA FLOSAQUAE Ralfs
189
		 *       ex Born. et Flah.
190
		 *       Abisara lemÈe-pauli => ABISARA LEMEEPAULI
191
		 *       Fuc/us Vesiculos2us => FUCUS VESICULOSUS
192
		 *       Buffo ignicolor LacÈpËde, 1788 => BUFFO IGNICOLOR LacÈpËde, 1788
193
		 *       Barbatia (Mesocibota) bistrigata (Dunker, 1866) => BARBATIA BISTRIGATA (Dunker, 1866)
194
		 *    (4) Thus version does not handle genus+author, or genus+species+infraspecies
195
		 *       (second" good" term is presumed to be species epithet, anything after is
196
		 *       considered to be start of the authority), however could be adapted further as required
197
         *         and actually it was done in this version for Tela Botanica
198
		 *    (5) There is a separate function "parse_auth" for normalizing authorities when required
199
		 *      (e.g. for authority comparisons)
200
		 *
201
		 * @param string $str : input string ( genus, genus+species, or genus+species+authority )
202
		 * @return string : parsed string
203
		 */
204
		public function parse( $str = NULL ) {
205
 
206
			unset($this->debug['parse']);
207
 
208
 
209
			$temp = '';
210
			$first_str_part = NULL;
211
			$second_str_part = NULL;
212
			$temp_genus = '';
213
			$temp_species = '';
214
			$temp_genus_species = '';
215
			$temp_authority = '';
216
			$temp_infra = '';
217
 
218
			$this->debug['parse'][] = "1";
219
 
220
			if ( ($str == NULL) || ( trim($str) == '') ) {
221
				$this->debug[] = "N1a<br>";
222
				return '';
223
			} else {
224
				//	trim any leading, trailing spaces or line feeds
225
				$temp = trim( $str );
226
				$this->debug['parse'][] = "1b";
227
			}
228
 
229
			if ( $temp == NULL || $temp == '') {
230
				$this->debug['parse'][] = "2a";
231
				return '';
232
			} else {
233
				$this->debug['parse'][] = "2b";
234
 
235
				// replace any HTML ampersands
236
				$set = array('%', '&', 'amp;%', 'AMP;%');
237
				$temp = str_replace( $set, '&', $temp );
238
 
239
				$this->debug['parse'][] = "2b1 (temp:$temp)";
240
 
241
				// remove any content in angle brackets (e.g. html tags - <i>, </i>, etc.)
242
				$html_pattern = "(\<(/?[^\>]+)\>)";
243
//? This should not just handle html tags but all <*>
244
				$temp = preg_replace( $html_pattern, '', $temp);
245
				$this->debug['parse'][] = "2b2 (temp:$temp)";
246
 
247
				// if second term (only) is in round brackets, presume it is a subgenus or a comment and remove it
248
				// examples: Barbatia (Mesocibota) bistrigata (Dunker, 1866) => Barbatia bistrigata (Dunker, 1866)
249
				// Barbatia (?) bistrigata (Dunker, 1866) => Barbatia bistrigata (Dunker, 1866)
250
				// (obviously this will not suit genus + author alone, where first part of authorname is in brackets,
251
				// however this is very rare?? and in any case we are not supporting genus+authority in this version)
252
//if ( $temp like '% (%)%'
253
				$temp = preg_replace( "/ \(\w*\W*\)/", '', $temp, 1 );
254
//? Not sure if this will catch if
255
				$this->debug['parse'][] = "2b3 (temp:$temp)";
256
 
257
				// if second term (only) is in square brackets, presume it is a comment and remove it
258
				// example: Aphis [?] ficus Theobald, [1918] => Aphis ficus Theobald, [1918]
259
//if ( $temp like '% [%]%'
260
				$temp = preg_replace( "/ \[\w*\W*\]/", '', $temp, 1 );
261
//? Not sure if this will catch if
262
				$this->debug['parse'][] = "2b4 (temp:$temp)";
263
 
264
				// drop indicators of questionable id's - presume all are lowercase for now (could extend as needed)
265
				$temp = preg_replace( "/ cf /", " ", $temp );
266
				$temp = preg_replace( "/ cf\. /", " ", $temp );
267
				$temp = preg_replace( "/ near /", " ", $temp );
268
				$temp = preg_replace( "/ aff\. /", " ", $temp );
269
				$temp = preg_replace( "/ sp\. /", " ", $temp );
270
				$temp = preg_replace( "/ spp\. /", " ", $temp );
271
				$temp = preg_replace( "/ spp /", " ", $temp );
272
 
273
				$this->debug['parse'][] = "2b5 (temp:$temp)";
274
 
275
				// eliminate or close up any stray spaces introduced by the above
276
				$temp = $this->reduce_spaces( $temp );
277
 
278
				$this->debug['parse'][] = "2b6 (temp:$temp)";
279
 
280
				// now presume first element is genus, second (if present) is species, remainder
281
				//   (if present) is authority
282
				// look for genus name
283
				$ar = explode( " ", $temp, 2);
284
				if ( count( $ar ) ) {
285
					$temp_genus = $ar[0];
286
					$temp = @$ar[1];
287
				} else {
288
					$temp_genus = $temp;
289
					$temp = '';
290
				}
291
 
292
				$this->debug['parse'][] = "2b7 (temp_genus:$temp_genus) (temp:$temp)";
293
 
294
				// look for species epithet and authority
295
				$ar = explode( " ", $temp, 2);
296
				if ( count( $ar ) ) {
297
					$temp_species = $ar[0];
298
					$temp_authority = @$ar[1];
299
				} else {
300
					$temp_species = $temp;
301
					$temp_authority = '';
302
				}
303
               	// look for subspecies
304
 
305
                $infras =array('subsp.','var.');
306
 
590 david 307
                $temp_authority = preg_replace( "/ssp./", "subsp.", $temp_authority);
308
                $temp_authority = preg_replace( "/ssp /", "subsp.", $temp_authority);
540 david 309
                $temp_authority = preg_replace( "/subsp /", "subsp.", $temp_authority);
310
                $temp_authority = preg_replace( "/var /", "var.", $temp_authority);
311
 
312
                foreach ($infras as $infra) {
313
                    $pos = strpos($temp_authority, $infra);
314
                    if ($pos === false) {
315
                        continue;
316
                    }
317
                    else {
318
                        $temp_infra=substr($temp_authority,$pos+strlen($infra));
319
                        $temp_authority=substr($temp_authority,0,$pos);
320
                        $temp_infra=trim($temp_infra);
321
                        $temp_infra_type=$infra;
322
                        // look for infra epithet and authority
323
                        $ar = explode(" ", $temp_infra, 2);
324
                        if ( count( $ar ) ) {
325
                            $temp_infra = $ar[0];
326
                            $temp_infra_authority = @$ar[1];
327
                        }
328
                        break; // on s'arrete au premier trouve
329
                    }
330
                }
331
 
332
				$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)";
333
 
334
 
335
				// replace selected ligatures here (Genus names can contain Æ, OE ligature)
336
				$temp_genus = str_replace( 'Æ', 'AE', $temp_genus);
337
				$temp_species = str_replace( 'Æ', 'AE', $temp_species);
338
				$temp_infra = str_replace( 'Æ', 'AE', $temp_infra );
339
 
340
 
341
				$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)";
342
 
343
                $temp_genus= trim($temp_genus);
344
				$temp_species= trim($temp_species);
345
				$temp_infra= trim($temp_infra );
346
 
347
				// reduce any new multiple internal spaces to single space, if present
348
                $temp_genus= $this->reduce_spaces( $temp_genus );
349
				$temp_species= $this->reduce_spaces( $temp_species );
350
				$temp_infra= $this->reduce_spaces( $temp_infra );
351
 
352
				$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)";
353
 
354
                if (isset($temp_authority) && ($temp_authority!='') ) {
355
                    $temp_authority=$this->parse_auth($temp_authority);
356
                }
357
 
358
                if (isset($temp_infra_authority) && ($temp_infra_authority!='') ) {
359
                    $temp_infra_authority=$this->parse_auth($temp_infra_authority);
360
                }
361
 
362
 
363
				$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)";
364
 
365
				return array("genus"=>$temp_genus, "species"=>$temp_species, "authority"=>$temp_authority, "infra"=>$temp_infra, "infra_authority"=>$temp_infra_authority, "infra_type"=>$temp_infra_type);
366
 
367
			}
368
 
369
		} // End NameParser
370
 
371
 
372
 
373
	} // End Class
374
 
375
?>