package hk.ust.comp4321.engine;

import java.io.IOException;

import hk.ust.comp4321.database.InvertedIndexTable;

/**
 * 
 * @author Paul Wong
 *
 */

public class TermWeight {
	
	// Constants and fields.
	// -------------------------------------------------------------------------	
	
	InvertedIndexTable invertedIndexTable;
	private int wordId;
	private int pageId;
	private final int totalPage = 300; //N
	
	// Constructors.
	// -------------------------------------------------------------------------

	/**
	 * This constructor encapsulates the WordId and PageId.
	 * 
	 * @param WordId        the word's id
	 * @param PageId        the page's id
	 */
	public TermWeight(int WordId, int PageId)
	{
		wordId = WordId;
		pageId = PageId;
	}
	
	/**
	 * This method converts base 10 log to base 2 log.
	 * 
	 * @param n        number needed to be log
	 */
	private static double log2(int n)
	{
	    return (Math.log(n) / Math.log(2));
	}
	
	/**
	 * This method finds the termWeight by (tf/max. tf) * log (N/df).
	 * 
	 * @return termWeight
	 */
	public double getTermWeight() throws IOException
	{
	  	invertedIndexTable = InvertedIndexTable.getTable();
		int tfmax=0;
		int tf = invertedIndexTable.getIndexInfo(wordId, pageId).getPositionList().size(); //tf
		int numOfPage = invertedIndexTable.getIndexInfoList(wordId).size(); //df
		for(int i=0; i<numOfPage; i++) // max. tf
		{
			if (invertedIndexTable.getIndexInfoList(wordId).get(i).getPositionList().size() > tfmax)
			{
				tfmax = invertedIndexTable.getIndexInfoList(wordId).get(i).getPositionList().size();
			}
		}
		return (tf/tfmax)*log2(totalPage/numOfPage);
	}
}

@@ -55,16 +55,67 @@
 ption;%0A%0A
+import hk.ust.comp4321.database.ForwardIndexTable;%0A
 import h
@@ -371,16 +371,54 @@
 xTable;%0A
+%09ForwardIndexTable forwardIndexTable;%0A
 %09private
@@ -824,16 +824,122 @@
 PageId;%0A
+%09%09forwardIndexTable = ForwardIndexTable.getTable();%0A%09%09invertedIndexTable = InvertedIndexTable.getTable();%0A
 %09%7D%0A%09%0A%09/*
@@ -1292,64 +1292,8 @@
 %0A%09%7B%0A
-%09  %09invertedIndexTable = InvertedIndexTable.getTable();%0A
 %09%09in
@@ -1485,25 +1485,63 @@
  i=0; i%3C
-numOfPage
+forwardIndexTable.getDocInfoList(pageId).size()
 ; i++) /
@@ -1561,23 +1561,22 @@
 %0A%09%09%09if (
-inverte
+forwar
 dIndexTa
@@ -1574,37 +1574,35 @@
 rdIndexTable.get
-Index
+Doc
 InfoList(wordId)
@@ -1586,36 +1586,36 @@
 .getDocInfoList(
-word
+page
 Id).get(i).getPo
@@ -1612,35 +1612,25 @@
 t(i).get
-PositionList().size
+Frequency
 () %3E tfm
@@ -1650,23 +1650,22 @@
 tfmax = 
-inverte
+forwar
 dIndexTa
@@ -1663,37 +1663,35 @@
 rdIndexTable.get
-Index
+Doc
 InfoList(wordId)
@@ -1679,28 +1679,28 @@
 DocInfoList(
-word
+page
 Id).get(i).g
@@ -1701,35 +1701,25 @@
 t(i).get
-PositionList().size
+Frequency
 ();%0A%09%09%09%7D

Update TermWeight.java