view src/de/mpiwg/anteater/species/common/CommonNameFindController.java @ 3:ae96e4bc7fb2

save found species to analysis files
author jdamerow
date Mon, 22 Oct 2012 14:21:14 -0700
parents 1c2b4f5e2c05
children dcc35f89dce3
line wrap: on
line source

package de.mpiwg.anteater.species.common;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.mpiwg.anteater.AnteaterConfiguration;
import de.mpiwg.anteater.species.common.impl.LinnaeusNameFinder;
import de.mpiwg.anteater.species.scientific.IScientificNamesFinder;
import de.mpiwg.anteater.species.scientific.ScientificNamesExtraction;
import de.mpiwg.anteater.species.scientific.impl.GNRDNameFinder;
import de.mpiwg.anteater.text.TextInformation;
import de.mpiwg.anteater.text.TextPart;
import de.mpiwg.anteater.xml.impl.AnalysisXMLManager;

public class CommonNameFindController {

public final static String COMPONENT_NAME = CommonNameFindController.class.getSimpleName();
	
	private AnteaterConfiguration configuration;

	public CommonNameFindController(AnteaterConfiguration configuration) {
		this.configuration = configuration;
	}
	
	public List<CommonNamesExtraction> findCommonNamesInXML(TextInformation info) {
		List<CommonNamesExtraction> results = new ArrayList<CommonNamesExtraction>();
		List<String> summaryAnalysisResults = new ArrayList<String>();
		List<String> supplinfAnalysisResults = new ArrayList<String>();
		
		// check if there are already stored results
		AnalysisXMLManager analysisManager = null;
		if (configuration.getAnalysisPath() != null && !configuration.getAnalysisPath().isEmpty()) {
			File file = new File(info.getFilepath());
			
			analysisManager = new AnalysisXMLManager(configuration.getAnalysisPath() + File.separator +  file.getName());
			
			configuration.getLogger().logMessageWithoutNewLine(COMPONENT_NAME, "Check analysis file for scientific names in summaries...");
			summaryAnalysisResults = analysisManager.getSummaryCommonNamesResults();
			configuration.getLogger().logMessage("found " + summaryAnalysisResults.size() + " result(s).");
		
			configuration.getLogger().logMessageWithoutNewLine(COMPONENT_NAME, "Check analysis file for scientific names in supplementary information...");
			supplinfAnalysisResults = analysisManager.getSupplementaryInfoCommonNamesResults();
			configuration.getLogger().logMessage("found " + supplinfAnalysisResults.size() + " result(s).");
		}
		
		ICommonNameFinder nameFinder = new LinnaeusNameFinder(configuration.getLogger());
		
		// if there are no results for summaries, ask GNRD name finding service.
		if (summaryAnalysisResults.size() == 0) {
			configuration.getLogger().logMessage(COMPONENT_NAME, "No results found for summaries, so will ask LinnaeusNameFinder.");
						
			for (TextPart sum : info.getSummaries()) {
				String sumResult = nameFinder.findCommonNames(sum.getText());
				if (sumResult != null) {
					summaryAnalysisResults.add(sumResult);
					
					// if there is an analysis folder, add result to analysis file
					if (analysisManager != null)
						analysisManager.addSummaryCommonNamesResult(sumResult);
				}
			}
		}
		
		// if there are no results for supplementary information, ask GNRD name fining service
		if (supplinfAnalysisResults.size() == 0) {
			configuration.getLogger().logMessage(COMPONENT_NAME, "No results found for supplementary information, so will ask LinnaeusNameFinder.");
			
			for (TextPart sInf : info.getSupplInfos()) {
				String supinfResult = nameFinder.findCommonNames(sInf.getText());
				if (supinfResult != null) {
					supplinfAnalysisResults.add(supinfResult);
					
					// if there is an analysis folder, add result to analysis file
					if (analysisManager != null)
						analysisManager.addSupplInfCommonNamesResult(supinfResult);
				}
			}
		}
		
		return null;
	}
	
}