/*
 * Copyright 2008 Novamente LLC
 *
 * 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.
 */

package relex.chunk;

import java.util.ArrayList;

import relex.stats.TruthValue;
import relex.stats.SimpleTruthValue;

/**
 * Performs a simple, basic lexical chunk ranking, based on
 * the frequency of occurance.
 *
 * Copyright (C) 2008 Linas Vepstas <linas@linas.org>
 */

public abstract class ChunkRanker
{
	private int numParses;
	private ArrayList<LexChunk> chunks;
	
	public ChunkRanker()
	{
		numParses = 1;
		chunks = new ArrayList<LexChunk>();
	}

	public ArrayList<LexChunk> getChunks()
	{
		return chunks;
	}

	public void clear()
	{
		chunks.clear();
	}

	/**
	 * Add a chunk. If its already in the list, then increment 
	 * its liklihood of being appropriate.
	 */
	public void add(LexChunk ch)
	{
		boolean not_found = true;
		for (int i=0; i<chunks.size(); i++)
		{
			LexChunk c = chunks.get(i);

			// Increase the overall confidence.
			if (ch.equals(c))
			{
				not_found = false;
				ch = c;
				break;
			}
		}

		if (not_found)
		{
			chunks.add(ch);
		}

		TruthValue tv = ch.getTruthValue();
		SimpleTruthValue stv = (SimpleTruthValue) tv;
		double confidence = stv.getConfidence();
		confidence += 1.0/numParses;
		stv.setConfidence(confidence);
	}
}

@@ -879,17 +879,8 @@
 lic 
-abstract 
 clas
@@ -978,16 +978,22 @@
 kRanker(
+int np
 )%0A%09%7B%0A%09%09n
@@ -1003,17 +1003,18 @@
 arses = 
-1
+np
 ;%0A%09%09chun
@@ -1342,46 +1342,8 @@
 or (
-int i=0; i%3Cchunks.size(); i++)%0A%09%09%7B%0A%09%09%09
 LexC
@@ -1353,25 +1353,21 @@
 k c 
-=
+:
  chunks
-.get(i);%0A
+)%0A%09%09%7B
 %0A%09%09%09
@@ -1505,16 +1505,61 @@
 nd)%0A%09%09%7B%0A
+%09%09%09ch.addTruthValue(new SimpleTruthValue());%0A
 %09%09%09chunk
@@ -1769,11 +1769,586 @@
 nce);%0A%09%7D
+%0A%0A%09public void add(ArrayList%3CLexChunk%3E lst)%0A%09%7B%0A%09%09for (LexChunk ch : lst)%0A%09%09%09add(ch);%0A%09%7D%0A%0A%09/**%0A%09 * Ad-hoc printed representation of the ranked contents.%0A%09 * Meant to be human readable, and nothing more.%0A%09 * May change from one Relex version to another, not stable.%0A%09 */%0A%09public String toString()%0A%09%7B%0A%09%09String str = %22%22;%0A%09%09for (LexChunk ch: chunks)%0A%09%09%7B%0A%09%09%09str += ch.toString();%0A%0A%09%09%09TruthValue tv = ch.getTruthValue();%0A%09%09%09SimpleTruthValue stv = (SimpleTruthValue) tv;%0A%09%09%09double confidence = stv.getConfidence();%0A%0A%09%09%09str += %22 Confidence: %22 + confidence + %22%5Cn%22;%0A%09%09%7D%0A%09%09return str;%0A%09%7D
 %0A%7D%0A

fix up, add a toString() method