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

it.tidalwave.bluebill.taxonomy.birds.aou.Aou7thImporter Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: Aou7thImporter.java,v f39a9725b54c 2010/07/24 21:08:01 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.birds.aou;

import it.tidalwave.util.logging.Logger;
import javax.annotation.Nonnull;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.openrdf.repository.Repository;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import it.tidalwave.bluebill.taxonomy.TaxonomyConcept;
import it.tidalwave.bluebill.taxonomy.birds.BirdTaxonomyImporter;
import it.tidalwave.bluebill.taxonomy.birds.DisplayNameInitializer;
import it.tidalwave.bluebill.taxonomy.birds.NorthAmericanEnglishAndFrenchDisplayNameInitializer;
import it.tidalwave.util.EmptyInitializer;
import java.io.InputStream;
import static it.tidalwave.bluebill.taxonomy.elmo.ElmoTaxonomyVocabulary.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: Aou7thImporter.java,v f39a9725b54c 2010/07/24 21:08:01 fabrizio $
 *
 **********************************************************************************************************************/
public class Aou7thImporter extends BirdTaxonomyImporter
  {
    private static final String CLASS = Aou7thImporter.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Taxonomy run (@Nonnull final Repository repository,
                         @Nonnull final String taxonomyName)
      throws Exception
      {
        itisCodeMap.initialize();
        init(repository);
        
        final String root = ID_TAXONOMY_PREFIX + "AOU/7th";
        final Taxonomy taxonomy = taxonomyManager.createTaxonomy("AOU 7th edition", root, repository);

        final String avesId = ID_TAXONOMY_PREFIX + "AOU/7th" + "/Animalia/Chordata/Aves";
        final TaxonomyConcept aves = taxonomy.createTopConcept("Aves", avesId);
        setType(aves, classType);
        
        final InputStream is = getClass().getResourceAsStream("AOUlist.csv");
        final BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"));

        br.readLine();
        br.readLine();

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

            if (s == null)
              {
                break;
              }

            System.err.println(s);
            final Scanner scanner = new Scanner(s).useDelimiter(",");
            final String scientificName = dequoted(scanner.next());
            String englishName = dequoted(scanner.next());
            String frenchName = dequoted(scanner.next());
            final String orderName = dequoted(scanner.next());
            final String familyName = dequoted(scanner.next());

            final Scanner scanner2 = new Scanner(scientificName).useDelimiter(" ");
            final String genusName = scanner2.next();
            final String speciesName = scanner2.next();
            final String subSpeciesName = scanner2.hasNext() ? scanner2.next() : null;

            logger.info("%s %s %s %s %s %s %s %s", scientificName, englishName, frenchName, orderName, familyName, genusName, speciesName, subSpeciesName);

            if (orderName.contains("Incertae Sedis") || familyName.contains("Incertae Sedis"))
              {
                continue;
              }

            String path = "/Animalia/Chordata/Aves";
            path += "/" + orderName;
            final TaxonomyConcept order = findOrCreateSubConcept(aves, orderName, root, path, orderType, EmptyInitializer.instance());
            path += "/" + familyName;
            final TaxonomyConcept family = findOrCreateSubConcept(order, familyName, root, path, familyType, EmptyInitializer.instance());
            path += "/" + genusName;
            final TaxonomyConcept genus = findOrCreateSubConcept(family, genusName, root, path, genusType, EmptyInitializer.instance());
            path += "/" + speciesName;
            final TaxonomyConcept species = findOrCreateSubConcept(genus, speciesName, root, path, speciesType, new NorthAmericanEnglishAndFrenchDisplayNameInitializer(englishName, frenchName));

            if (subSpeciesName != null)
              {
                path += "/" + subSpeciesName;
                try
                  {
                    findOrCreateSubConcept(species, subSpeciesName, root, path, subSpeciesType, new NorthAmericanEnglishAndFrenchDisplayNameInitializer(englishName, frenchName));
                  }
                catch (RuntimeException e)
                  {
                    brokenSpecies.add(path + ": " + e);
                    throw e;
                  }
              }
          }

        br.close();
        taxonUniqueIdManager.close();

        return taxonomy;
      }

    @Nonnull
    protected String dequoted (final @Nonnull String string)
      {
        return string.replaceAll("^\"", "").replaceAll("\"$", "");
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy