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

it.tidalwave.bluebill.taxonomy.birds.ebn.italia.EbnItalia1998Importer 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: EbnItalia1998Importer.java,v d3b28eab0cdd 2010/08/01 20:17:04 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.birds.ebn.italia;

import java.io.IOException;
import javax.annotation.Nonnull;
import java.util.Locale;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.openrdf.elmo.Entity;
import org.openrdf.repository.Repository;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import it.tidalwave.bluebill.taxonomy.TaxonomyConcept;
import it.tidalwave.bluebill.taxonomy.birds.BirdLocaleInfo;
import it.tidalwave.bluebill.taxonomy.birds.BirdTaxonomyImporter;
import it.tidalwave.openrdf.elmo.RDFUtils;
import it.tidalwave.util.EmptyInitializer;
import it.tidalwave.util.logging.Logger;
import java.io.InputStream;
import org.openrdf.repository.RepositoryException;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: EbnItalia1998Importer.java,v d3b28eab0cdd 2010/08/01 20:17:04 fabrizio $
 *
 **********************************************************************************************************************/
public class EbnItalia1998Importer extends BirdTaxonomyImporter
  {
    private static final String CLASS = EbnItalia1998Importer.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Taxonomy run (@Nonnull final Repository repository,
                         @Nonnull final String taxonomyName,
                         @Nonnull final BirdLocaleInfo localeInfo,
                         @Nonnull final String id)
      throws Exception
      {
        init(repository);

        final Taxonomy taxonomy = taxonomyManager.createTaxonomy(taxonomyName, id, repository);

        final String avesId = id + "/Animalia/Chordata/Aves";
        final TaxonomyConcept aves = taxonomy.createTopConcept("Aves", avesId);
        setType(aves, classType);

        final InputStream is = getClass().getResourceAsStream(sourceResourceName);
        final BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        process1998(br, taxonomy, aves, localeInfo, id);
        br.close();
        taxonUniqueIdManager.close();

        return taxonomy;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void process1998 (@Nonnull final BufferedReader br,
                              @Nonnull final Taxonomy taxonomy,
                              @Nonnull final TaxonomyConcept aves,
                              @Nonnull final BirdLocaleInfo localeInfo,
                              @Nonnull final String id)
      throws IOException, RepositoryException
      {
        initialize();
        
        br.readLine();
        br.readLine();
        br.readLine();
        br.readLine();
        br.readLine();

        TaxonomyConcept order = null;
        TaxonomyConcept family = null;
        TaxonomyConcept subFamily = null;
        String orderPath = "";
        String familyPath = "";
        String subFamilyPath = "";

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

            if (s == null)
              {
                break;
              }

            s = s.trim();
            s = catalogueOfLifeTranslator.fixTypos(s);

            if (s.equals(""))
              {
                continue;
              }

//            System.err.println(s);
            final Scanner scanner = new Scanner(s).useDelimiter("\\t");

            if (allUpperCase(s))
              {
                final String italianName = capitalized(scanner.next().trim());
                final String scientificName = capitalized(scanner.next().trim());
                logger.fine("2: *%s* *%s*", italianName, scientificName);

                if (scientificName.endsWith("es"))
                  {
                    orderPath = "/Animalia/Chordata/Aves/" + scientificName;
                    order = findOrCreateSubConcept(aves, scientificName, id, orderPath, orderType, EmptyInitializer.instance());
                    RDFUtils.setDisplayName(order.as(Entity.class), italianName, Locale.ITALIAN);
                    family = null;
                    subFamily = null;
                  }
                else if (scientificName.endsWith("dae") || 
                         scientificName.equals("Tetraoninae") ||
                         scientificName.equals("Phasianinae") ||
                         scientificName.equals("Larinae") ||
                         scientificName.equals("Sterninae") ||
                         scientificName.equals("Numidinae"))
                  {
                    if ("Rynchopidae".equals(scientificName))
                      {
                        break; // FIXME
                      }

                    familyPath = orderPath + "/" + scientificName;
                    family = findOrCreateSubConcept(order, scientificName, id, familyPath, familyType, EmptyInitializer.instance());
                    RDFUtils.setDisplayName(family.as(Entity.class), italianName, Locale.ITALIAN);
                    subFamily = null;
                  }
                else if (scientificName.endsWith("nae"))
                  {
                    subFamilyPath = familyPath + "/" + scientificName;
                    logger.warning("IGNORING SUBFAMILY: %s", scientificName); // FIXME
                    continue;
//                        subFamily = findOrCreateSubConcept(family, scientificName, subFamilyPath, subFamilyType);
//                        RDFUtils.setDisplayName(subFamily.as(Entity.class), italianName, Locale.ITALIAN);
                  }
                else if (scientificName.endsWith("ni"))
                  {
                  }
                else
                  {
                    throw new RuntimeException(scientificName);
                  }
              }
            else
              {
                final String italianName = scanner.next();

                if (italianName.equals("Cigno minore*"))
                  {
                    continue; // FIXME
                  }

                if (italianName.equals("Cigno della tundra*"))
                  {
                    continue; // FIXME
                  }

                if (italianName.equals("Chiurlo piccolo americano*"))
                  {
                    continue; // FIXME
                  }

                if (italianName.equals("Gabbiano di Kumlien*"))
                  {
                    continue; // FIXME
                  }

                final String scientificName = scanner.next();
//                final String scientificName = scanner.next().replace("(", "").replace(")", " ");
                final String englishName = scanner.next();
                logger.fine("3: *%s* *%s* *%s*", italianName, scientificName, englishName);
                final Scanner scanner2 = new Scanner(scientificName).useDelimiter(" ");
                final String genusName = scanner2.next();
                final String speciesName = scanner2.next();

                final String genusPath = familyPath + "/" + genusName;
                final TaxonomyConcept genus = findOrCreateSubConcept(family, genusName, id, genusPath, genusType, EmptyInitializer.instance());
                final String speciesPath = genusPath + "/" + speciesName;
                final TaxonomyConcept species = findOrCreateSubConcept(genus, speciesName, id, speciesPath, speciesType, EmptyInitializer.instance());

                RDFUtils.setDisplayName(species.as(Entity.class), italianName, Locale.ITALIAN);
                RDFUtils.setDisplayName(species.as(Entity.class), englishName, Locale.ENGLISH);
              }
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private static boolean allUpperCase (@Nonnull final String string)
      {
        for (final char c : string.toCharArray())
          {
            if (Character.isLowerCase(c))
              {
                return false;
              }
          }
        
        return true;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private static String capitalized (@Nonnull final String string)
      {
        return string.substring(0, 1).toUpperCase() + string.substring(1).toLowerCase();
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy