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

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

import it.tidalwave.util.NotFoundException;
import java.io.IOException;
import javax.annotation.Nonnull;
import java.util.Locale;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileInputStream;
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.Initializer;
import it.tidalwave.util.logging.Logger;
import java.io.InputStream;
import org.openrdf.repository.RepositoryException;

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

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    class LanguageInitializer implements Initializer
      {
        @Nonnull
        private final String[] languages;

        @Nonnull
        private final String italianName;

        @Nonnull
        private final String englishName;

        @Nonnull
        private final BirdLocaleInfo localeInfo;

        @Nonnull
        private final String scientificName;

        public LanguageInitializer (final @Nonnull String[] languages,
                                    final @Nonnull String italianName,
                                    final @Nonnull String englishName,
                                    final @Nonnull BirdLocaleInfo localeInfo,
                                    final @Nonnull String scientificName)
          {
            this.languages = languages;
            this.italianName = italianName;
            this.englishName = englishName;
            this.localeInfo = localeInfo;
            this.scientificName = scientificName;
          }
    
        @Nonnull
        public TaxonomyConcept initialize (final @Nonnull TaxonomyConcept taxon)
          {
            for (final String language : languages)
              {
                String localizedName = "";

                try
                  {
                    // Italian and english are taken from the EBN checklist;
                    if ("it".equals(language))
                      {
                        localizedName = italianName;
                      }
                    else if ("en".equals(language))
                      {
                        localizedName = englishName;
                      }
                    else
                      {
                        localizedName = localeInfo.getLocalizedName(scientificName, language);
                      }
                  }
                catch (NotFoundException e)
                  {
                    try
                      {
                        localizedName = localeInfo.getLocalizedName(italianName, language);
                      }
                    catch (NotFoundException e2)
                      {
                        if ("it".equals(language))
                          {
                            localizedName = italianName;
                          }
                        else if ("en".equals(language))
                          {
                            localizedName = englishName;
                          }
                        else
                          {
                            localizedName = "n.a. (" + scientificName + ")";
                          }

                        logger.fine("                 Missing translation for: " + language);
                        addMissingTranslation(scientificName + " / " + italianName, language);
                      }
                  }

                RDFUtils.setDisplayName(taxon.as(Entity.class), localizedName, new Locale(language));
              }

            return taxon;
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Taxonomy run (@Nonnull final Repository repository,
                         @Nonnull final String taxonomyName,
                         @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"));
        process2003(br, taxonomy, aves, birdLocaleInfo, id);
        br.close();
        taxonUniqueIdManager.close();

        return taxonomy;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void process2003 (@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();

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

        final String[] languages = { "en", "en_US", "it", "de", "da", "no", "fi", "sv", "fr", "es", "nl" };

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

            if (s == null)
              {
                break;
              }

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

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

            if (s.equals("$$$$$$$$$$") || s.equals("$$0$$$$$$$$") 
                || s.startsWith("segnando nella")
                || s.startsWith("X: specie osservata")
                || s.startsWith("H: specie solo sentita")
                || s.startsWith("#: specie osservata"))
              {
                continue;
              }

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

            if (s.startsWith("-"))
              {
                scanner.next();
                final String orderName = capitalized(scanner.next().trim());
                final String familyName = capitalized(scanner.next().trim());
                logger.info("**************** ORDER & FAMILY: *%s* *%s*", orderName, familyName);

                orderPath = "/Animalia/Chordata/Aves/" + orderName;
                familyPath = orderPath + "/" + familyName;
                
                order = findOrCreateSubConcept(aves, orderName, id, orderPath, orderType, EmptyInitializer.instance());
                family = findOrCreateSubConcept(order, familyName, id, familyPath, familyType, EmptyInitializer.instance());
//                subFamily = null;
              }
            else
              {
                final String italianName = scanner.next();                
                final String scientificName = scanner.next().replace("(", "").replace(")", ""); // FIXME
                final String englishName = scanner.next();
                
                final Scanner scanner2 = new Scanner(scientificName).useDelimiter(" ");
                final String genusName = scanner2.next();
                final String speciesName = scanner2.next();

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

                final TaxonomyConcept genus = findOrCreateSubConcept(family, genusName, id, genusPath, genusType, EmptyInitializer.instance());
                final LanguageInitializer languageInitializer = new LanguageInitializer(languages, italianName, englishName, localeInfo, scientificName);
                final TaxonomyConcept species = findOrCreateSubConcept(genus, speciesName, id, speciesPath, speciesType, languageInitializer);
              }
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @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