package org.greenstone.atlas.server;

import gate.util.Out;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.greenstone.atlas.client.Place;

public class PageScanner
{
	// Stores the gazeteer structure (used to verify if words are a place name
	// or not)
	protected GazetteerTrieType2 _gazetteer = null;

	// Stores all of the places in the page that is being examined
	protected ArrayList<Place> _places = new ArrayList<Place>();
	protected HashMap<String, ArrayList<Place>> _placeNameMap = new HashMap<String, ArrayList<Place>>();
	protected String _markedUpText = null;
	protected HashMap<String, ArrayList<Place>> _placeCache = new HashMap<String, ArrayList<Place>>();

	protected GateScanner _gateScanner = new GateScanner();

	// Parameters for score calculations
	// *********************************
	protected double _penaltyPercentage = 0.5;
	protected double _parentBonusPercentage = 0.25;
	protected double _indirectReferencePenaltyPercentage = 0.25;
	protected double _parentLimitPercentage = 0.05;

	protected ArrayList<String> _prevDoc = null;
	protected String _prevFileName = null;

	/**
	 * Default constructer. It creates the place data structure and gazetteer
	 * trie structure
	 */
	public PageScanner(String path)
	{
		System.err.println("Loading path = " + path);
		try
		{
			PlaceInformation.init();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		System.err.println("Starting loading gazetteer");
		_gazetteer = new GazetteerTrieType2(path + "/dataen.txt");
	}

	/**
	 * Examines the given text to find place names and score them
	 * 
	 * @param text
	 *            is the text to examine
	 */
	public ArrayList<Place> examineTextWithGate(String text, String htmlString)
	{
		_places.clear();
		
		if(htmlString != null && text == null)
		{
			text = HTMLParser.removeTags(htmlString);
		}

		if (text == null || text.length() < 1 || htmlString == null || htmlString.length() < 1)
		{
			return new ArrayList<Place>();
		}

		HTMLParser htmlParser = new HTMLParser(htmlString);

		ArrayList<String> betweenChars = htmlParser.getFullBetweenWordList();
		ArrayList<String> htmlWords = htmlParser.getFullHTMLWordList();
		
		HashMap<Integer, Integer> placeNameIndexesAndLength = new HashMap<Integer, Integer>();
		
		try
		{
			// Classify all the text with Gate
			HashMap<String, Word> classifiedWords = _gateScanner.classifyText(text);

			// Stores the current words being examined
			StringBuilder currentPotentialPlaceNames = new StringBuilder();
			
			// Get the next word from the HTML
			for (int i = 0; i < htmlWords.size(); i++)
			{		
				String currentWord = htmlWords.get(i);
				
				Word classifiedWord = classifiedWords.get(currentWord);
				if (classifiedWord == null)
				{
					continue;
				}
				
				String classification = classifiedWord.getNextClassification();

				// If the word does not begin with an uppercase letter then ignore it
				if (classification == null || Character.isLowerCase(currentWord.charAt(0)) || !(classification.equals("NNP")))
				{
					continue;
				}

				// Add the first word to the list of words to be examined
				currentPotentialPlaceNames.append(currentWord);

				// While the gazetteer does not reach a dead end, add another word and examine them
				int count = 1;
				String lastGoodPlaceName = null;
				while (_gazetteer.checkPlaceName(currentPotentialPlaceNames.toString()) != -1)
				{
					// If the words are a place name then store it
					if (_gazetteer.checkPlaceName(currentPotentialPlaceNames.toString()) == 1)
					{
						lastGoodPlaceName = currentPotentialPlaceNames.toString();
					}

					// If it is not the end of the words the add the next word
					if (i + count < htmlWords.size())
					{
						currentPotentialPlaceNames.append(betweenChars.get(i + count) + htmlWords.get(i + count++));
					}
					else
					{
						break;
					}
				}

				// If there was a place name found then find its information and score it
				if (lastGoodPlaceName != null)
				{
					placeNameIndexesAndLength.put(i, count);

					ArrayList<Place> placeList = PlaceInformation.getPlaces(lastGoodPlaceName);

					if (placeList == null)
					{
						continue;
					}

					if (_placeNameMap.containsKey(lastGoodPlaceName))
					{
						for (Place p : _placeNameMap.get(lastGoodPlaceName))
						{
							p.directReference();
						}
					}

					for (Place p : placeList)
					{
						p.directReference();
						addScore(p, 256);
					}
				}

				currentPotentialPlaceNames = new StringBuilder();
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
			

		// Tidy up the places found by removing overlapping places
		for(int i = 0; i < htmlWords.size(); i++)
		{
			if(placeNameIndexesAndLength.get(i) != null)
			{
				int length = placeNameIndexesAndLength.get(i);
				for(int j = 1; j < length; j++)
				{
					if(placeNameIndexesAndLength.get(i + j) != null)
					{
						placeNameIndexesAndLength.remove(i + j);
					}
				}
			}
		}

		StringBuilder htmlBuilder = new StringBuilder();

		int placeEnd = -1;
		for(int i = 0; i < htmlWords.size(); i++)
		{
			if(i == placeEnd)
			{
				htmlBuilder.append("</span>");
			}		
			
			if(i < betweenChars.size() && betweenChars.get(i) != null)
			{
				htmlBuilder.append(betweenChars.get(i));
			}
			
			if(placeNameIndexesAndLength.get(i) != null)
			{
				htmlBuilder.append("<span class=\"place\">");
				placeEnd = i + (placeNameIndexesAndLength.get(i) - 1);
			}
			htmlBuilder.append(htmlWords.get(i));
		}

		_markedUpText = htmlBuilder.toString();
		
		if (_places.size() > 0)
		{
			adjustScores();
		}

		return _places;
	}

	public ArrayList<Place> examineTextWithoutGate(ArrayList<ArrayList<String>> lines)
	{
		_places.clear();
		try
		{
			// Stores the current words being examined
			StringBuilder currentWords = new StringBuilder();

			// Read each line from the file
			for (ArrayList<String> words : lines)
			{
				// Examine the words
				for (int j = 0; j < words.size(); j++)
				{
					// If the word does not begin with an uppercase letter then
					// ignore it
					if (Character.isLowerCase(words.get(j).charAt(0)) || !(words.get(j).length() > 1 && Character.isLowerCase(words.get(j).charAt(1))))
					{
						continue;
					}

					// Used to store a good place name
					String lastGoodPlaceName = null;

					// Add the first word to the list of words to be examined
					currentWords.append(words.get(j));

					// While the gazetteer does not reach a dead end, add
					// another word and examine them
					int count = 1;
					while (_gazetteer.checkPlaceName(currentWords.toString()) != -1)
					{
						// If the words are a place name then store it
						if (_gazetteer.checkPlaceName(currentWords.toString()) == 1)
						{
							lastGoodPlaceName = currentWords.toString();
							// System.out.println("Current place name part => "
							// + lastGoodPlaceName);
						}

						// If it is not the end of the words the add the next
						// word
						if (j + count < words.size())
						{
							currentWords.append(" " + words.get(j + count++));
						}
						else
						{
							break;
						}
					}

					// If there was a place name found then find its information
					// and score it
					if (lastGoodPlaceName != null)
					{
						ArrayList<Place> placeList = PlaceInformation.getPlaces(lastGoodPlaceName);

						if (placeList == null)
						{
							continue;
						}

						if (_placeNameMap.containsKey(lastGoodPlaceName))
						{
							for (Place p : _placeNameMap.get(lastGoodPlaceName))
							{
								p.directReference();
							}
						}

						for (Place p : placeList)
						{
							p.directReference();
							addScore(p, 256);
						}
					}
					currentWords = new StringBuilder();
				}
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}

		adjustScores();

		return _places;
	}

	public ArrayList<String> getPlaceNames(String text)
	{
		ArrayList<String> placeNames = new ArrayList<String>();
		_places.clear();
		try
		{
			// The file to read from
			BufferedReader file = new BufferedReader(new StringReader(text));

			// Stores the current words being examined
			StringBuilder currentWords = new StringBuilder();

			// Stores the current line being examined
			String currentLine = null;

			// The list of words in the line
			ArrayList<String> words = new ArrayList<String>();

			// Read each line from the file
			while ((currentLine = file.readLine()) != null)
			{
				words.clear();

				// Find the words in those lines and add them to the list
				words.addAll(MarkupService.findWords(currentLine));

				// Examine the words
				for (int j = 0; j < words.size(); j++)
				{
					// If the word does not begin with an uppercase letter then
					// ignore it
					if (Character.isLowerCase(words.get(j).charAt(0)) || !(words.get(j).length() > 1 && Character.isLowerCase(words.get(j).charAt(1))))
					{
						continue;
					}

					// Used to store a good place name
					String lastGoodPlaceName = null;

					// Add the first word to the list of words to be examined
					currentWords.append(words.get(j));

					// While the gazetteer does not reach a dead end, add
					// another word and examine them
					int count = 1;
					while (_gazetteer.checkPlaceName(currentWords.toString()) != -1)
					{
						// If the words are a place name then store it
						if (_gazetteer.checkPlaceName(currentWords.toString()) == 1)
						{
							lastGoodPlaceName = currentWords.toString();
							// System.out.println("Current place name part => "
							// + lastGoodPlaceName);
						}

						// If it is not the end of the words the add the next
						// word
						if (j + count < words.size())
						{
							currentWords.append(" " + words.get(j + count++));
						}
						else
						{
							break;
						}
					}

					// If there was a place name found then find its information
					// and score it
					if (lastGoodPlaceName != null)
					{
						placeNames.add(lastGoodPlaceName);
					}
					currentWords = new StringBuilder();
				}
			}
			file.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}

		return placeNames;
	}

	public ArrayList<Place> examineArrayOfStrings(ArrayList<String> placeNames)
	{
		_places.clear();
		for (int i = 0; i < placeNames.size(); i++)
		{
			String currentPlaceName = placeNames.get(i);

			ArrayList<Place> placeList = null;
			if (_placeCache.containsKey(currentPlaceName))
			{
				placeList = _placeCache.get(currentPlaceName);
				for (Place p : placeList)
				{
					p.unDirectReference();
				}
			}
			else
			{
				placeList = PlaceInformation.getPlaces(currentPlaceName);
				_placeCache.put(currentPlaceName, placeList);
			}

			if (placeList == null)
			{
				continue;
			}

			if (_placeNameMap.containsKey(currentPlaceName))
			{
				for (Place p : _placeNameMap.get(currentPlaceName))
				{
					p.directReference();
				}
			}

			for (Place p : placeList)
			{
				p.directReference();
				addScore(p, 256);
			}
		}

		// System.out.println("Done!");
		// System.out.print("Adjusting Scores... ");

		adjustScores();

		// System.out.println("Done!");
		// System.out.print("Sorting Scores... ");

		// System.out.println("Done!");

		// System.out.println("Places found = " + _places);

		return _places;
	}

	public ArrayList<Place> examineTextWithoutGate(String text)
	{
		_places.clear();
		try
		{
			// The file to read from
			BufferedReader file = new BufferedReader(new StringReader(text));

			// Stores the current words being examined
			StringBuilder currentWords = new StringBuilder();

			// Stores the current line being examined
			String currentLine = null;

			// The list of words in the line
			ArrayList<String> words = new ArrayList<String>();

			// Read each line from the file
			while ((currentLine = file.readLine()) != null)
			{
				words.clear();

				// Find the words in those lines and add them to the list
				words.addAll(MarkupService.findWords(currentLine));

				// Examine the words
				for (int j = 0; j < words.size(); j++)
				{
					// If the word does not begin with an uppercase letter then
					// ignore it
					if (Character.isLowerCase(words.get(j).charAt(0)) || !(words.get(j).length() > 1 && Character.isLowerCase(words.get(j).charAt(1))))
					{
						continue;
					}

					// Used to store a good place name
					String lastGoodPlaceName = null;

					// Add the first word to the list of words to be examined
					currentWords.append(words.get(j));

					// While the gazetteer does not reach a dead end, add
					// another word and examine them
					int count = 1;
					while (_gazetteer.checkPlaceName(currentWords.toString()) != -1)
					{
						// If the words are a place name then store it
						if (_gazetteer.checkPlaceName(currentWords.toString()) == 1)
						{
							lastGoodPlaceName = currentWords.toString();
							// System.out.println("Current place name part => "
							// + lastGoodPlaceName);
						}

						// If it is not the end of the words the add the next
						// word
						if (j + count < words.size())
						{
							currentWords.append(" " + words.get(j + count++));
						}
						else
						{
							break;
						}
					}

					// If there was a place name found then find its information
					// and score it
					if (lastGoodPlaceName != null)
					{
						ArrayList<Place> placeList = PlaceInformation.getPlaces(lastGoodPlaceName);

						if (placeList == null)
						{
							continue;
						}

						if (_placeNameMap.containsKey(lastGoodPlaceName))
						{
							for (Place p : _placeNameMap.get(lastGoodPlaceName))
							{
								p.directReference();
							}
						}

						for (Place p : placeList)
						{
							p.directReference();
							addScore(p, 256);
						}
					}
					currentWords = new StringBuilder();
				}
			}
			file.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}

		// System.out.println("Done!");
		// System.out.print("Adjusting Scores... ");

		adjustScores();

		// System.out.println("Done!");
		// System.out.print("Sorting Scores... ");
		// System.out.println("Done!");
		// System.out.println("Places found = " + _places);

		return _places;
	}

	public String getMarkedUpText()
	{
		return _markedUpText;
	}

	public Place getTopScorePlace()
	{
		Place p = _places.get(0);

		for (Place pp : _places)
		{
			if (pp.getScore() > p.getScore())
			{
				p = pp;
			}
		}
		return p;
	}

	/**
	 * Used to make the original place scores more accurate by using other place
	 * information
	 */
	public void adjustScores()
	{
		for (Place p : _places)
		{
			if (!p.isDirectlyReferenced())
			{
				p.setScore((int) (p.getScore() * _penaltyPercentage));
			}
		}

		Place topScore = getTopScorePlace();

		for (Place p : _places)
		{
			if (p.getParentPlaceName() == null)
			{
				continue;
			}

			for (Place pp : _places)
			{
				if (p.isIn(pp) && (topScore.getScore() - pp.getScore()) <= (int) (topScore.getScore() * 0.1 * _parentLimitPercentage))
				{
					p.setScore((int) (p.getScore() + (pp.getScore() * _parentBonusPercentage)));
				}
			}
		}
	}

	/**
	 * Adds one to the score of the given place
	 * 
	 * @param p
	 *            is the place to add one to the score of
	 */
	public void addScore(Place p, Integer scoreToAdd)
	{
		if (p == null)
		{
			return;
		}

		// If there is already a score for this key the increase it by one
		if (_places.contains(p))
		{
			Place place = _places.get(_places.indexOf(p));
			place.setScore(place.getScore() + scoreToAdd);
		}
		// If there is no score for this key then make one
		else
		{
			p.setScore(scoreToAdd);
			_places.add(p);

			if (_placeNameMap.containsKey(p.getName()))
			{
				_placeNameMap.get(p.getName()).add(p);
			}
			else
			{
				ArrayList<Place> placeList = new ArrayList<Place>();
				_placeNameMap.put(p.getName(), placeList);
			}
		}

		// Add to the parent's score too (if there is one)
		if (!(p.getParentPlaceName() == null))
		{
			// If there is an ancestor then add to it's score as well
			if (p.getParentPlaceName().contains(", "))
			{
				String[] places = p.getParentPlaceName().split(", ");

				ArrayList<Place> specificPlaces = null;

				if (places.length == 3)
				{
					specificPlaces = PlaceInformation.getSpecificPlace(places[0], places[1] + ", " + places[2]);
				}
				else
				{
					specificPlaces = PlaceInformation.getSpecificPlace(places[0], places[1]);
				}

				if (specificPlaces == null)
				{
					return;
				}

				for (Place pp : specificPlaces)
				{
					addScore(pp, (int) (scoreToAdd * _indirectReferencePenaltyPercentage));
				}
			}
			// Otherwise just add the parent
			else
			{
				ArrayList<Place> specificPlaces = PlaceInformation.getSpecificPlace(p.getParentPlaceName(), null);

				if (specificPlaces == null)
				{
					return;
				}

				for (Place pp : specificPlaces)
				{
					addScore(pp, (int) (scoreToAdd * _indirectReferencePenaltyPercentage));
				}
			}
		}
	}

	public void sortScores()
	{
		ArrayList<Place> sortedPlaces = new ArrayList<Place>();

		while (_places.size() > 0)
		{
			int index = -1;
			for (int j = 0; j < _places.size(); j++)
			{
				if (index == -1 || _places.get(j).getScore() > _places.get(index).getScore())
				{
					index = j;
				}
			}

			sortedPlaces.add(_places.remove(index));
		}

		_places = sortedPlaces;
	}

	public ArrayList<Place> getPlacesWithParams(double maxScorePercentage, double minScorePercentage, long minPopulation, boolean locality, boolean region, boolean country, int numberOfPlacesToGet)
	{
		// System.out.println("Getting places with params");

		int topScore = _places.get(0).getScore();
		int minScore = (int) (topScore * minScorePercentage);
		int maxScore = (int) (topScore * maxScorePercentage);

		// System.out.println("minScore = " + minScore);
		// System.out.println("maxScore = " + maxScore);

		ArrayList<Place> matchingPlaces = new ArrayList<Place>();

		// Go through all the markers
		for (Place p : _places)
		{
			// System.out.println("Testing place -> " + p.getName());
			if (p == null)
			{
				// System.out.println("P is null?");
				continue;
			}

			// If the place meets the criteria
			if (p.getScore() > minScore && p.getScore() <= maxScore && p.getPopulation() > minPopulation && ((locality && p.getPlaceType().equals("locality")) || (region && p.getPlaceType().equals("region")) || (country && p.getPlaceType().equals("country"))))
			{
				// System.out.println("MATCH!");
				// If there is not already the maximum amount of visible places
				// then add this
				if (matchingPlaces.size() < numberOfPlacesToGet)
				{
					matchingPlaces.add(p);
				}
				// If there is already MAXVISIBLE visible places then see if
				// this place should replace one
				else
				{
					Place minScorePlace = null;
					for (Place q : matchingPlaces)
					{
						if (minScorePlace == null)
						{
							minScorePlace = q;
						}
						if (q.getScore() < minScorePlace.getScore())
						{
							minScorePlace = q;
						}
					}

					if (p.getScore() > minScorePlace.getScore())
					{
						matchingPlaces.remove(minScorePlace);
						matchingPlaces.add(p);
					}
				}
			}
			else
			{
				// System.out.println("NOT A MATCH");
			}
		}
		return matchingPlaces;
	}

	public void setScoringParams(double penaltyPercentage, double parentBonusPercentage, double indirectReferencePenaltyPercentage)
	{
		_penaltyPercentage = penaltyPercentage;
		_parentBonusPercentage = parentBonusPercentage;
		_indirectReferencePenaltyPercentage = indirectReferencePenaltyPercentage;
	}

	public void setScoringParams(ScanConfiguration config)
	{
		_penaltyPercentage = config.getPenalty();
		_parentBonusPercentage = config.getParentBonus();
		_indirectReferencePenaltyPercentage = config.getIndirectReferencePenalty();
		_parentLimitPercentage = config.getParentLimit();
	}

	// public HashMap<String, Integer> wordCount(String fileName)
	// {
	// HashMap<String, Integer> wordCountMap = new HashMap<String, Integer>();
	//
	// try
	// {
	// BufferedReader file = new BufferedReader(new FileReader(fileName));
	//
	// StringBuilder currentWord = new StringBuilder();
	//
	// String line = "";
	//
	// ArrayList<String> words = new ArrayList<String>();
	//
	// //System.out.print("Finding words... ");
	// while((line = file.readLine()) != null)
	// {
	// words.addAll(MarkupService.findWords(line));
	// }
	// //System.out.println("Done!");
	//
	// //System.out.print("Adding up scores... ");
	// for(int j = 0; j < words.size(); j++)
	// {
	// if(Character.isLowerCase(words.get(j).charAt(0)))
	// {
	// //continue;
	// }
	//
	// currentWord.append(words.get(j));
	//
	// int count = 1;
	// while(_gazetteer.checkPlaceName(currentWord.toString()) != -1)
	// {
	// if(_gazetteer.checkPlaceName(currentWord.toString()) == 1)
	// {
	// if(wordCountMap.containsKey(currentWord.toString()))
	// {
	// Integer i = wordCountMap.get(currentWord.toString());
	// wordCountMap.put(currentWord.toString(), ++i);
	// }
	// else
	// {
	// wordCountMap.put(currentWord.toString(), 1);
	// }
	// }
	// currentWord.append(" " + words.get(j + count++));
	// }
	//
	// currentWord.delete(0, currentWord.length());
	// }
	// }
	// catch(Exception ex)
	// {
	// ex.printStackTrace();
	// }
	//
	// return wordCountMap;
	// }

	public boolean isGazetteerLoaded()
	{
		return _gazetteer != null;
	}

	public ArrayList<ArrayList<Place>> examineMultipleTexts(String[] texts)
	{
		ArrayList<ArrayList<Place>> multipleResults = new ArrayList<ArrayList<Place>>();
		for (String text : texts)
		{
			if (text != null)
			{
				this.examineTextWithGate(null, text);

				if (_places.size() > 0)
				{
					multipleResults.add(new ArrayList<Place>(_places));
				}
				else
				{
					multipleResults.add(null);
				}
			}
			else
			{
				multipleResults.add(null);
			}
		}

		return multipleResults;
	}

	public ArrayList<Place> getPlaces()
	{
		return _places;
	}

	public void clearPlaces()
	{
		_places.clear();
	}
}

/*
 * 
 * // Halve the scores for places that do not have their parents // mentioned //
 * ********************************************************************
 * 
 * String[] parentPlaceNames = p.getParentPlaceName().split(", "); if
 * (parentPlaceNames.length == 1) { ArrayList<Place> parentList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[0], null); Place parent =
 * null;
 * 
 * if (parentList != null && parentList.size() > 0) { parent =
 * parentList.get(0);
 * 
 * if (!_places.contains(parent) ||
 * !_places.get(_places.indexOf(parent)).isDirectlyReferenced()) {
 * p.setScore((int) (p.getScore() * (1 - _penaltyPercentage))); } } } else if
 * (parentPlaceNames.length == 2) { ArrayList<Place> parentList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[0], parentPlaceNames[1]);
 * ArrayList<Place> ancestorList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[1], null);
 * 
 * Place parent = null; Place ancestor = null;
 * 
 * if (parentList != null && parentList.size() > 0) { parent =
 * parentList.get(0);
 * 
 * if (!_places.contains(parent) ||
 * !_places.get(_places.indexOf(parent)).isDirectlyReferenced()) {
 * p.setScore((int) (p.getScore() * (1 - _penaltyPercentage))); } }
 * 
 * if (ancestorList != null && ancestorList.size() > 0) { ancestor =
 * ancestorList.get(0);
 * 
 * if (!_places.contains(ancestor) ||
 * !_places.get(_places.indexOf(ancestor)).isDirectlyReferenced()) {
 * p.setScore((int) (p.getScore() * (1 - _penaltyPercentage))); } } } else if
 * (parentPlaceNames.length == 3) { ArrayList<Place> parentList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[0], parentPlaceNames[1] +
 * ", " + parentPlaceNames[2]); ArrayList<Place> firstAncestorList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[1], parentPlaceNames[2]);
 * ArrayList<Place> secondAncestorList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[2], null);
 * 
 * Place parent = null; Place firstAncestor = null; Place secondAncestor = null;
 * 
 * if (parentList != null && parentList.size() > 0) { parent =
 * parentList.get(0);
 * 
 * if (!_places.contains(parent) ||
 * !_places.get(_places.indexOf(parent)).isDirectlyReferenced()) {
 * p.setScore((int) (p.getScore() * (1 - _penaltyPercentage))); } }
 * 
 * if (firstAncestorList != null && firstAncestorList.size() > 0) {
 * firstAncestor = firstAncestorList.get(0);
 * 
 * if (!_places.contains(firstAncestor) ||
 * !_places.get(_places.indexOf(firstAncestor)).isDirectlyReferenced()) {
 * p.setScore((int) (p.getScore() * (1 - _penaltyPercentage))); } }
 * 
 * if (secondAncestorList != null && secondAncestorList.size() > 0) {
 * secondAncestor = secondAncestorList.get(0);
 * 
 * if (!_places.contains(secondAncestor) ||
 * !_places.get(_places.indexOf(secondAncestor)).isDirectlyReferenced()) {
 * p.setScore((int) (p.getScore() * (1 - _penaltyPercentage))); } } }
 */
// Add part of the parent's score to the child
// *******************************************
/*
 * if (parentPlaceNames.length == 1) { ArrayList<Place> parentList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[0], null);
 * 
 * Place parent = null;
 * 
 * if (parentList != null && parentList.size() > 0) { parent =
 * parentList.get(0);
 * 
 * if (_places.contains(parent)) { p.setScore(p.getScore() + (int)
 * (_places.get(_places.indexOf(parent)).getScore() * _parentBonusPercentage));
 * } } } else if (parentPlaceNames.length == 2) { ArrayList<Place> parentList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[0], parentPlaceNames[1]);
 * ArrayList<Place> ancestorList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[1], null);
 * 
 * Place parent = null; Place ancestor = null;
 * 
 * if (parentList != null && parentList.size() > 0) { parent =
 * parentList.get(0);
 * 
 * if (_places.contains(parent)) { p.setScore(p.getScore() + (int)
 * (_places.get(_places.indexOf(parent)).getScore() * _parentBonusPercentage));
 * } }
 * 
 * if (ancestorList != null && ancestorList.size() > 0) { ancestor =
 * ancestorList.get(0);
 * 
 * if (_places.contains(ancestor)) { p.setScore(p.getScore() + (int)
 * (_places.get(_places.indexOf(ancestor)).getScore() *
 * _parentBonusPercentage)); } } } else if (parentPlaceNames.length == 3) {
 * ArrayList<Place> parentList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[0], parentPlaceNames[1] +
 * ", " + parentPlaceNames[2]); ArrayList<Place> firstAncestorList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[1], parentPlaceNames[2]);
 * ArrayList<Place> secondAncestorList =
 * PlaceInformation.getSpecificPlace(parentPlaceNames[2], null);
 * 
 * Place parent = null; Place firstAncestor = null; Place secondAncestor = null;
 * 
 * if (parentList != null && parentList.size() > 0) { parent =
 * parentList.get(0);
 * 
 * if (_places.contains(parent)) { p.setScore(p.getScore() + (int)
 * (_places.get(_places.indexOf(parent)).getScore() * _parentBonusPercentage));
 * } }
 * 
 * if (firstAncestorList != null && firstAncestorList.size() > 0) {
 * firstAncestor = firstAncestorList.get(0);
 * 
 * if (_places.contains(firstAncestor)) { p.setScore(p.getScore() + (int)
 * (_places.get(_places.indexOf(firstAncestor)).getScore() *
 * _parentBonusPercentage)); } }
 * 
 * if (secondAncestorList != null && secondAncestorList.size() > 0) {
 * secondAncestor = secondAncestorList.get(0);
 * 
 * if (_places.contains(secondAncestor)) { p.setScore(p.getScore() + (int)
 * (_places.get(_places.indexOf(secondAncestor)).getScore() *
 * _parentBonusPercentage)); } } }
 */