view src/de/mpiwg/anteater/places/PlaceFinderController.java @ 0:036535fcd179

anteater
author jdamerow
date Fri, 14 Sep 2012 10:30:43 +0200
parents
children
line wrap: on
line source

package de.mpiwg.anteater.places;

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

import de.mpiwg.anteater.AnteaterConfiguration;
import de.mpiwg.anteater.places.impl.PlacemakerPlaceFinder;
import de.mpiwg.anteater.text.TextInformation;
import de.mpiwg.anteater.text.TextPart;
import de.mpiwg.anteater.text.TextType;
import de.mpiwg.anteater.xml.IPlaceFinderParser;
import de.mpiwg.anteater.xml.impl.AnalysisXMLManager;
import de.mpiwg.anteater.xml.impl.PlacemakerXMLParser;

public class PlaceFinderController {

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

	public PlaceFinderController(AnteaterConfiguration configuration) {
		this.configuration = configuration;
	}
	
	public List<PlacesExtraction> findPlacesInXML(TextInformation info) {
		List<PlacesExtraction> results = new ArrayList<PlacesExtraction>();
		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 places in summaries...");
			summaryAnalysisResults = analysisManager.getSummaryPlacesResults();
			configuration.getLogger().logMessage("found " + summaryAnalysisResults.size() + " result(s).");
		
			configuration.getLogger().logMessageWithoutNewLine(COMPONENT_NAME, "Check analysis file for places in supplementary information...");
			supplinfAnalysisResults = analysisManager.getSupplementaryInfoPlacesResults();
			configuration.getLogger().logMessage("found " + supplinfAnalysisResults.size() + " result(s).");
		}
		
		IPlaceFinder placeFinder = new PlacemakerPlaceFinder(configuration.getLogger());
		
		// if there are no results for summaries, ask place finding service.
		if (summaryAnalysisResults.size() == 0) {
			configuration.getLogger().logMessage(COMPONENT_NAME, "No results found for summaries, so will ask Placemaker.");
						
			for (TextPart sum : info.getSummaries()) {
				String sumResult = placeFinder.findPlaces(sum.getText());
				if (sumResult != null && !sumResult.isEmpty()) {
					summaryAnalysisResults.add(sumResult);
					
					// if there is an analysis folder, add result to analysis file
					if (analysisManager != null)
						analysisManager.addSummaryPlacesResult(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 Placemaker.");
			
			for (TextPart sum : info.getSupplInfos()) {
				String supinfResult = placeFinder.findPlaces(sum.getText());
				if (supinfResult != null && !supinfResult.isEmpty()) {
					supplinfAnalysisResults.add(supinfResult);
					
					// if there is an analysis folder, add result to analysis file
					if (analysisManager != null)
						analysisManager.addSupplInfPlacesResult(supinfResult);
				}
			}
		}
		
		// create objects
		configuration.getLogger().logMessage(COMPONENT_NAME, "Creating analysis results...");
		int idx = 0;
		for (String summaryResult : summaryAnalysisResults) {
			IPlaceFinderParser parser = new PlacemakerXMLParser(summaryResult);
			
			List<PlaceInformation> places = parser.parsePlaces();
			PlacesExtraction placeResult = new PlacesExtraction();
			placeResult.setType(TextType.TYPE_SUMMARY);
			placeResult.setPlaceInformation(places);
			placeResult.setTextIdx(idx);
			
			results.add(placeResult);
			idx++;
		}
		
		idx = 0;
		for (String suplinfResult : supplinfAnalysisResults) {
			IPlaceFinderParser parser = new PlacemakerXMLParser(suplinfResult);
			
			List<PlaceInformation> scientificNames = parser.parsePlaces();
			PlacesExtraction placeResult = new PlacesExtraction();
			placeResult.setType(TextType.TYPE_SUPLINF);
			placeResult.setPlaceInformation(scientificNames);
			placeResult.setTextIdx(idx);
			
			results.add(placeResult);
			idx++;
		}
		
		return results;
	}
}