All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.taxonomy.birds.birdsofindia.BirdsOfIndia2009Importer Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueBill Resources - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it
 * SCM: https://java.net/hg/bluebill~resources-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.birds.birdsofindia;

import it.tidalwave.bluebill.taxonomy.Taxon.Builder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import java.util.Locale;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryException;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import it.tidalwave.bluebill.taxonomy.Taxon;
import it.tidalwave.bluebill.taxonomy.birds.BirdLocaleInfo;
import it.tidalwave.bluebill.taxonomy.birds.BirdTaxonomyImporter;
import it.tidalwave.bluebill.taxonomy.birds.impl.PatchedDefaultMutableDisplayable;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.bluebill.taxonomy.Taxon.Rank.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@Slf4j
public class BirdsOfIndia2009Importer extends BirdTaxonomyImporter
  {
    private static final String[] LANGUAGES = { "en" };
    
    private Map genusMap = new HashMap();

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    class LanguageInitializer extends PatchedDefaultMutableDisplayable
      {
        public LanguageInitializer (final @Nonnull String[] languages,
                                    final @Nonnull String englishName,
                                    final @Nonnull BirdLocaleInfo localeInfo,
                                    final @Nonnull String scientificName)
          {
            for (final String language : languages)
              {
                String localizedName = "";

                if ("en".equals(language))
                  {
                    localizedName = englishName;
                  }

                setDisplayName(localizedName, new Locale(language));
              }
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Taxonomy run (final @Nonnull Repository repository,
                         final @Nonnull String idPrefix)
      throws Exception
      {
        init(repository);

        final Taxonomy taxonomy = createTaxonomy(repository, idPrefix, BirdsOfIndia2009Importer.class, LANGUAGES);
        final Taxon aves = createAves(taxonomy, idPrefix);
        
        final InputStream is = getClass().getResourceAsStream(sourceResourceName);
        final BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        process(br, taxonomy, aves, birdLocaleInfo, idPrefix);
        br.close();
        taxonUniqueIdManager.close();

        return taxonomy;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void process (final @Nonnull BufferedReader br,
                          final @Nonnull Taxonomy taxonomy,
                          final @Nonnull Taxon aves,
                          final @Nonnull BirdLocaleInfo localeInfo,
                          final @Nonnull String idPrefix)
      throws IOException, RepositoryException
      {
        initialize();
        
        for (int i = 0; i < 10; i++)
          {
            br.readLine();
          }
    
        Taxon order = null;
        Taxon family = null;
        String orderPath = "";
        String familyPath = "";
        String previous = "";
        String orderEn = "";

        for (;;)
          {
            String s = br.readLine();

            if (s == null)
              {
                break;
              }

            s = s.trim().replace("(Isabelline Shrike)W", "(Isabelline Shrike) W")
                        .replace("Green Avadavat V r", "Green Avadavat Vr")
                        .replace("Long-billed Bush Warbler N r", "Long-billed Bush Warbler Nr")
                        .replaceAll("Oenanthe picata.", "Oenanthe picata");
            s = taxonTranslator.fixTypos(s);

            log.trace(">>>> {}", s);
            
            if (s.equals("")) 
              {
                previous = s;
                continue;
              }
            
            if (!s.contains(" ") || s.contains(","))
              {
                previous = s;
                continue;   
              }

            final Scanner scanner = new Scanner(s).useDelimiter(" ");
            
            if (s.toLowerCase().startsWith("order"))
              {
                scanner.next();
                final String orderName = capitalized(scanner.next());
                log.info("**************** ORDER: *{}*", orderName);
                orderPath = "/Animalia/Chordata/Aves/" + orderName;

                if (!previous.equals("") && !previous.toLowerCase().startsWith("family"))
                  {
                    orderEn = previous;
                  }
                
                order = findOrCreateSubTaxon(aves, idPrefix, orderPath).withRank(ORDER).
                                                                        withScientificName(orderName).
                                                                        withSpecificEpythet(orderName).
                                                                        withDisplayName(orderEn, Locale.ENGLISH).
                                                                        build();
              }
            
            else if (s.toLowerCase().startsWith("family"))
              {
                scanner.next();
                final String familyName = capitalized(scanner.next());
                log.info("**************** FAMILY: *{}*", familyName);
                familyPath = orderPath + "/" + familyName;
                family = findOrCreateSubTaxon(order, idPrefix, familyPath).withRank(FAMILY).
                                                                           withScientificName(familyName).
                                                                           withSpecificEpythet(familyName).
                                                                           build();
              }
            
            else
              {
                 log.info(">>>>> " + s);
                final StringBuffer englishName = new StringBuffer();
                String separator = "";
                
                for (;;)
                  {
                    final String ss = scanner.next().replace("*", "");
                    
                    if (ss.length() < 3)
                      {
                        break;  
                      }
                    
                    englishName.append(separator).append(ss);
                    separator = " ";
                                        
                    if (Arrays.asList("Lesser Noddy", "Sykes’s Warbler", "Chinese Leaf Warbler").contains(englishName.toString()))
                      {
                        break;  
                      }
                  }
                
                final String genusName = scanner.next();
                final String speciesName = scanner.next().trim();
                
                final String scientificName = genusName + " " + speciesName;

                final String genusPath = familyPath + "/" + genusName;
                final String speciesPath = genusPath + "/" + speciesName;

                final LanguageInitializer commonNames = new LanguageInitializer(LANGUAGES, englishName.toString(), localeInfo, scientificName);
                Taxon genus = genusMap.get(genusName);
                
                if (genus == null)
                  {
                    genus = findOrCreateSubTaxon(family, idPrefix, genusPath).withRank(GENUS).
                                                                              withScientificName(genusName).
                                                                              withSpecificEpythet(genusName).
                                                                              build();
                  }
                
                final Taxon species = findOrCreateSubTaxon(genus, idPrefix, speciesPath).withRank(SPECIES).
                                                                                         withScientificName(genusName + " " + speciesName).
                                                                                         withSpecificEpythet(speciesName).
                                                                                         withDisplayNames(commonNames).
                                                                                         build();
              }
          }
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy