You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ctakes.apache.org by se...@apache.org on 2013/02/15 23:26:54 UTC
svn commit: r1446793 [3/3] - in
/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup:
./ ae/ algorithms/ filter/ jdbc/ lucene/ strtable/ vo/
Modified: incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/algorithms/PermutationUtil.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/algorithms/PermutationUtil.java?rev=1446793&r1=1446792&r2=1446793&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/algorithms/PermutationUtil.java (original)
+++ incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/algorithms/PermutationUtil.java Fri Feb 15 22:26:53 2013
@@ -18,218 +18,151 @@
*/
package org.apache.ctakes.dictionary.lookup.algorithms;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
/**
* @author Mayo Clinic
*/
-public class PermutationUtil
-{
- /**
- * Gets all permutations for the given level and all sub-levels.
- *
- * @param maxLevel
- */
- public static List getPermutationList(int maxLevel)
- {
- List permList = new ArrayList();
- for (int levelIdx = maxLevel; levelIdx >= 0; levelIdx--)
- {
- // contains ALL index values
- List baseNumList = new ArrayList();
- for (int j = 1; j <= levelIdx; j++)
- {
- baseNumList.add(new Integer(j));
- }
-
- Collection numListCol = new ArrayList();
- if (levelIdx != maxLevel)
- {
- numListCol.addAll(getNumLists(maxLevel, baseNumList));
- }
- else
- {
- numListCol.add(baseNumList);
- }
-
- Iterator numListItr = numListCol.iterator();
- while (numListItr.hasNext())
- {
- List numList = (List) numListItr.next();
- Collection pCol = PermutationUtil
- .getLinearPermutations(numList);
- Iterator pItr = pCol.iterator();
- while (pItr.hasNext())
- {
- List permutation = (List) pItr.next();
- permList.add(permutation);
- }
- }
-
- if (levelIdx == 0)
- {
- permList.add(new ArrayList());
- }
- }
-
- return permList;
- }
-
- private static Collection getNumLists(int maxLevel, List baseNumList)
- {
- Collection numListCol = new ArrayList();
- buildPermutations(maxLevel, baseNumList, numListCol, new ArrayList(), 0);
- filterNonIncreasingLists(numListCol);
- return numListCol;
- }
-
- /**
- * Filters the number lists such that only lists with increasing numbers are
- * kept.
- *
- * @param numListCol
- */
- private static void filterNonIncreasingLists(Collection numListCol)
- {
- Set removalSet = new HashSet();
-
- Iterator numListItr = numListCol.iterator();
- while (numListItr.hasNext())
- {
- List numList = (List) numListItr.next();
- Integer largestNum = null;
- Iterator numItr = numList.iterator();
- while (numItr.hasNext())
- {
- Integer num = (Integer) numItr.next();
- if (largestNum == null)
- {
- largestNum = num;
- }
- else
- {
- int comparison = largestNum.compareTo(num);
- if (comparison == 1)
- {
- removalSet.add(numList);
- }
- else
- {
- largestNum = num;
- }
- }
- }
- }
- numListCol.removeAll(removalSet);
- }
-
- /**
- * Recursively builds permutations of numbers specified by the base num
- * list. This includes permutations of these numbers with few items than the
- * original list.
- *
- * @param maxLevel
- * @param baseNumList
- * @param numListCol
- * @param residualList
- * @param residualCount
- */
- private static void buildPermutations(
- int maxLevel,
- List baseNumList,
- Collection numListCol,
- List residualList,
- int residualCount)
- {
- if (residualCount > baseNumList.size())
- {
- return;
- }
- else if (residualCount == baseNumList.size())
- {
- numListCol.add(new ArrayList(residualList));
- return;
- }
- else
- {
- int num = ((Integer) baseNumList.get(residualCount)).intValue();
- residualCount++;
- for (int i = num; i <= maxLevel; i++)
- {
- List tempList = new ArrayList(residualList);
- if (!tempList.contains(new Integer(i)))
- {
- tempList.add(new Integer(i));
- buildPermutations(
- maxLevel,
- baseNumList,
- numListCol,
- tempList,
- residualCount);
- }
- }
- }
- }
-
- /**
- * Gets a collection of lists, each list represents a single permutation.
- * This permutation is composed of Integer objects in defined order.
- *
- * @param level
- * @return
- */
- public static Collection getLinearPermutations(List numList)
- {
- Collection permutations = new ArrayList();
- getLinearPermutations(permutations, new ArrayList(), numList);
- return permutations;
- }
-
- /**
- * Recurisvely builds permutations from the number list. The size of the
- * permutations remains constant.
- *
- * @param permutations
- * @param plusList
- * @param numList
- */
- private static void getLinearPermutations(
- Collection permutations,
- List plusList,
- List numList)
- {
- Iterator numItr = numList.iterator();
- while (numItr.hasNext())
- {
- Integer num = (Integer) numItr.next();
-
- List subList = new ArrayList();
- subList.addAll(numList);
- subList.remove(num);
-
- plusList.add(num);
-
- if (subList.size() > 0)
- {
- getLinearPermutations(permutations, plusList, subList);
- }
- else
- {
- List permutation = new ArrayList();
- for (int i = 0; i < plusList.size(); i++)
- {
- Integer n = (Integer) plusList.get(i);
- permutation.add(n);
- }
- permutations.add(permutation);
- }
-
- plusList.remove(num);
- }
- }
-}
\ No newline at end of file
+public class PermutationUtil {
+ /**
+ * Gets all permutations for the given level and all sub-levels.
+ *
+ * @param maxLevel -
+ */
+ public static List<List<Integer>> getPermutationList( final int maxLevel ) {
+ final List<List<Integer>> permList = new ArrayList<List<Integer>>();
+ for ( int levelIdx = maxLevel; levelIdx >= 0; levelIdx-- ) {
+ // contains ALL index values
+ final List<Integer> baseNumList = new ArrayList<Integer>();
+ for ( int j = 1; j <= levelIdx; j++ ) {
+ baseNumList.add( j );
+ }
+
+ final Collection<List<Integer>> numListCol = new ArrayList<List<Integer>>();
+ if ( levelIdx != maxLevel ) {
+ numListCol.addAll( getNumLists( maxLevel, baseNumList ) );
+ } else {
+ numListCol.add( baseNumList );
+ }
+ for ( List<Integer> numList : numListCol ) {
+ final Collection<List<Integer>> pCol = PermutationUtil.getLinearPermutations( numList );
+ for ( List<Integer> permutation : pCol ) {
+ permList.add( permutation );
+ }
+ }
+
+ if ( levelIdx == 0 ) {
+ permList.add( new ArrayList<Integer>() );
+ }
+ }
+
+ return permList;
+ }
+
+ private static Collection<List<Integer>> getNumLists( final int maxLevel, final List<Integer> baseNumList ) {
+ final Collection<List<Integer>> numListCol = new ArrayList<List<Integer>>();
+ buildPermutations( maxLevel, baseNumList, numListCol, new ArrayList<Integer>(), 0 );
+ filterNonIncreasingLists( numListCol );
+ return numListCol;
+ }
+
+ /**
+ * Filters the number lists such that only lists with increasing numbers are
+ * kept.
+ *
+ * @param numListCol -
+ */
+ private static void filterNonIncreasingLists( final Collection<List<Integer>> numListCol ) {
+ final Set<List<Integer>> removalSet = new HashSet<List<Integer>>();
+ for ( List<Integer> numList : numListCol ) {
+ Integer largestNum = null;
+ for ( Integer num : numList ) {
+ if ( largestNum == null ) {
+ largestNum = num;
+ } else {
+ final int comparison = largestNum.compareTo( num );
+ if ( comparison == 1 ) {
+ removalSet.add( numList );
+ } else {
+ largestNum = num;
+ }
+ }
+ }
+ }
+ numListCol.removeAll( removalSet );
+ }
+
+ /**
+ * Recursively builds permutations of numbers specified by the base num
+ * list. This includes permutations of these numbers with few items than the
+ * original list.
+ *
+ * @param maxLevel -
+ * @param baseNumList -
+ * @param numListCol -
+ * @param residualList -
+ * @param residualCount -
+ */
+ private static void buildPermutations( final int maxLevel, final List<Integer> baseNumList,
+ final Collection<List<Integer>> numListCol,
+ final List<Integer> residualList, int residualCount ) {
+ if ( residualCount > baseNumList.size() ) {
+ return;
+ } else if ( residualCount == baseNumList.size() ) {
+ numListCol.add( new ArrayList<Integer>( residualList ) );
+ return;
+ } else {
+ final int num = baseNumList.get( residualCount );
+ residualCount++;
+ for ( int i = num; i <= maxLevel; i++ ) {
+ List<Integer> tempList = new ArrayList<Integer>( residualList );
+ if ( !tempList.contains( i ) ) {
+ tempList.add( i );
+ buildPermutations( maxLevel, baseNumList, numListCol, tempList, residualCount );
+ }
+ }
+ }
+ }
+
+ /**
+ * Gets a collection of lists, each list represents a single permutation.
+ * This permutation is composed of Integer objects in defined order.
+ *
+ * @param numList -
+ * @return -
+ */
+ public static Collection<List<Integer>> getLinearPermutations( final List<Integer> numList ) {
+ final Collection<List<Integer>> permutations = new ArrayList<List<Integer>>();
+ getLinearPermutations( permutations, new ArrayList<Integer>(), numList );
+ return permutations;
+ }
+
+ /**
+ * Recursively builds permutations from the number list. The size of the permutations remains constant.
+ *
+ * @param permutations -
+ * @param plusList -
+ * @param numList -
+ */
+ private static void getLinearPermutations( final Collection<List<Integer>> permutations,
+ final List<Integer> plusList, final List<Integer> numList ) {
+ for ( Integer num : numList ) {
+ final List<Integer> subList = new ArrayList<Integer>( numList );
+// subList.addAll( numList );
+ subList.remove( num );
+
+ plusList.add( num );
+
+ if ( !subList.isEmpty() ) {
+ getLinearPermutations( permutations, plusList, subList );
+ } else {
+ final List<Integer> permutation = new ArrayList<Integer>( plusList );
+// for ( Integer n : plusList ) {
+// permutation.add( n );
+// }
+ permutations.add( permutation );
+ }
+ plusList.remove( num );
+ }
+ }
+}
Modified: incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/filter/CollectionFilter.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/filter/CollectionFilter.java?rev=1446793&r1=1446792&r2=1446793&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/filter/CollectionFilter.java (original)
+++ incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/filter/CollectionFilter.java Fri Feb 15 22:26:53 2013
@@ -18,6 +18,8 @@
*/
package org.apache.ctakes.dictionary.lookup.filter;
+import org.apache.ctakes.dictionary.lookup.MetaDataHit;
+
import java.util.Collection;
/**
@@ -36,5 +38,5 @@ public interface CollectionFilter
* @param metaDataHitCol
* @throws FilterException
*/
- public Collection applyFilter(Collection metaDataHitCol) throws FilterException;
+ public Collection<MetaDataHit> applyFilter(Collection<MetaDataHit> metaDataHitCol) throws FilterException;
}
Modified: incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/filter/TruncateCollectionFilterImpl.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/filter/TruncateCollectionFilterImpl.java?rev=1446793&r1=1446792&r2=1446793&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/filter/TruncateCollectionFilterImpl.java (original)
+++ incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/filter/TruncateCollectionFilterImpl.java Fri Feb 15 22:26:53 2013
@@ -18,6 +18,8 @@
*/
package org.apache.ctakes.dictionary.lookup.filter;
+import org.apache.ctakes.dictionary.lookup.MetaDataHit;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -52,14 +54,14 @@ public class TruncateCollectionFilterImp
/**
* Implementation
*/
- public Collection applyFilter(Collection metaDataHitCol) throws FilterException
+ public Collection<MetaDataHit> applyFilter(final Collection<MetaDataHit> metaDataHitCol) throws FilterException
{
if (iv_mdhCount >= metaDataHitCol.size())
{
return metaDataHitCol;
}
- List mdhList = new ArrayList(metaDataHitCol);
+ final List<MetaDataHit> mdhList = new ArrayList<MetaDataHit>(metaDataHitCol);
// sort the hits
Collections.sort(mdhList, iv_mdhComparator);
@@ -69,4 +71,4 @@ public class TruncateCollectionFilterImp
return mdhList;
}
-}
\ No newline at end of file
+}
Modified: incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/jdbc/JdbcDictionaryImpl.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/jdbc/JdbcDictionaryImpl.java?rev=1446793&r1=1446792&r2=1446793&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/jdbc/JdbcDictionaryImpl.java (original)
+++ incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/jdbc/JdbcDictionaryImpl.java Fri Feb 15 22:26:53 2013
@@ -18,156 +18,125 @@
*/
package org.apache.ctakes.dictionary.lookup.jdbc;
+import org.apache.ctakes.dictionary.lookup.*;
+import org.apache.ctakes.dictionary.lookup.Dictionary;
+
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.ctakes.dictionary.lookup.BaseDictionaryImpl;
-import org.apache.ctakes.dictionary.lookup.Dictionary;
-import org.apache.ctakes.dictionary.lookup.DictionaryException;
-import org.apache.ctakes.dictionary.lookup.GenericMetaDataHitImpl;
-import org.apache.ctakes.dictionary.lookup.MetaDataHit;
+import java.util.*;
/**
- *
* @author Mayo Clinic
*/
-public class JdbcDictionaryImpl extends BaseDictionaryImpl implements Dictionary
-{
- private Connection iv_dbConn;
- private String iv_tableName;
- private String iv_lookupFieldName;
- private PreparedStatement iv_mdPrepStmt;
- private PreparedStatement iv_cntPrepStmt;
-
- public JdbcDictionaryImpl(
- Connection conn,
- String tableName,
- String lookupFieldName)
- {
- iv_dbConn = conn;
- iv_tableName = tableName;
- iv_lookupFieldName = lookupFieldName;
- }
-
- private PreparedStatement initCountPrepStmt(String text)
- throws SQLException
- {
- if (iv_cntPrepStmt == null)
- {
- StringBuffer sb = new StringBuffer();
- sb.append("SELECT COUNT(*) ");
-
- sb.append(" FROM ");
- sb.append(iv_tableName);
-
- sb.append(" WHERE ");
- sb.append(iv_lookupFieldName);
- sb.append(" = ?");
-
- iv_cntPrepStmt = iv_dbConn.prepareStatement(sb.toString());
- }
-
- iv_cntPrepStmt.clearParameters();
- iv_cntPrepStmt.setString(1, text);
-
- return iv_cntPrepStmt;
- }
-
- private PreparedStatement initMetaDataPrepStmt(String text)
- throws SQLException
- {
- if (iv_mdPrepStmt == null)
- {
- StringBuffer sb = new StringBuffer();
- sb.append("SELECT ");
-
- // translate meta data field names into columns
- // to be returned in the result set
- Iterator metaFieldNameItr = getMetaFieldNames();
- while (metaFieldNameItr.hasNext())
- {
- String mdFieldName = (String) metaFieldNameItr.next();
- sb.append(mdFieldName);
- sb.append(',');
- }
- // chomp off the last comma
- sb.deleteCharAt(sb.length() - 1);
-
- sb.append(" FROM ");
- sb.append(iv_tableName);
-
- sb.append(" WHERE ");
- sb.append(iv_lookupFieldName);
- sb.append(" = ?");
-
- iv_mdPrepStmt = iv_dbConn.prepareStatement(sb.toString());
- }
-
- iv_mdPrepStmt.clearParameters();
- iv_mdPrepStmt.setString(1, text);
-
- return iv_mdPrepStmt;
- }
-
- public boolean contains(String text) throws DictionaryException
- {
- try
- {
- PreparedStatement prepStmt = initCountPrepStmt(text);
- ResultSet rs = prepStmt.executeQuery();
-
- rs.next();
- int count = rs.getInt(1);
- if (count > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (SQLException e)
- {
- throw new DictionaryException(e);
- }
- }
-
- public Collection getEntries(String str) throws DictionaryException
- {
- Set metaDataHitSet = new HashSet();
- try
- {
- PreparedStatement prepStmt = initMetaDataPrepStmt(str);
- ResultSet rs = prepStmt.executeQuery();
-
- while (rs.next())
- {
- Map nameValMap = new HashMap();
- Iterator metaFieldNameItr = getMetaFieldNames();
- while (metaFieldNameItr.hasNext())
- {
- String metaFieldName = (String) metaFieldNameItr.next();
- String metaFieldValue = rs.getString(metaFieldName);
- nameValMap.put(metaFieldName, metaFieldValue);
- }
- MetaDataHit mdh = new GenericMetaDataHitImpl(nameValMap);
- metaDataHitSet.add(mdh);
+public class JdbcDictionaryImpl extends AbstractBaseDictionary implements Dictionary {
+ final private Connection iv_dbConn;
+ final private String iv_tableName;
+ final private String iv_lookupFieldName;
+ private PreparedStatement iv_mdPrepStmt;
+ private PreparedStatement iv_cntPrepStmt;
+
+ public JdbcDictionaryImpl( final Connection conn, final String tableName, final String lookupFieldName ) {
+ iv_dbConn = conn;
+ iv_tableName = tableName;
+ iv_lookupFieldName = lookupFieldName;
+ }
+
+ private PreparedStatement initCountPrepStmt( final String text ) throws SQLException {
+ if ( iv_cntPrepStmt == null ) {
+ final StringBuilder sb = new StringBuilder();
+ sb.append( "SELECT COUNT(*) " );
+
+ sb.append( " FROM " );
+ sb.append( iv_tableName );
+
+ sb.append( " WHERE " );
+ sb.append( iv_lookupFieldName );
+ sb.append( " = ?" );
+
+ iv_cntPrepStmt = iv_dbConn.prepareStatement( sb.toString() );
+ }
+
+ iv_cntPrepStmt.clearParameters();
+ iv_cntPrepStmt.setString( 1, text );
+
+ return iv_cntPrepStmt;
+ }
+
+ private PreparedStatement initMetaDataPrepStmt( final String text ) throws SQLException {
+ if ( iv_mdPrepStmt == null ) {
+ final StringBuilder sb = new StringBuilder();
+ sb.append( "SELECT " );
+
+ // translate meta data field names into columns
+ // to be returned in the result set
+ final Iterator metaFieldNameItr = getMetaFieldNames();
+ while ( metaFieldNameItr.hasNext() ) {
+ String mdFieldName = (String) metaFieldNameItr.next();
+ sb.append( mdFieldName );
+ sb.append( ',' );
+ }
+ // chomp off the last comma
+ sb.deleteCharAt( sb.length() - 1 );
+
+ sb.append( " FROM " );
+ sb.append( iv_tableName );
+
+ sb.append( " WHERE " );
+ sb.append( iv_lookupFieldName );
+ sb.append( " = ?" );
+
+ iv_mdPrepStmt = iv_dbConn.prepareStatement( sb.toString() );
+ }
+
+ iv_mdPrepStmt.clearParameters();
+ iv_mdPrepStmt.setString( 1, text );
+
+ return iv_mdPrepStmt;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean contains( final String text ) throws DictionaryException {
+ try {
+ final PreparedStatement prepStmt = initCountPrepStmt( text );
+ final ResultSet rs = prepStmt.executeQuery();
+ rs.next();
+ final int count = rs.getInt( 1 );
+ return count > 0;
+ } catch ( SQLException e ) {
+ throw new DictionaryException( e );
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Collection<MetaDataHit> getEntries( final String text ) throws DictionaryException {
+ final Set<MetaDataHit> metaDataHitSet = new HashSet<MetaDataHit>();
+ try {
+ final PreparedStatement prepStmt = initMetaDataPrepStmt( text );
+ final ResultSet rs = prepStmt.executeQuery();
+
+ while ( rs.next() ) {
+ final Map<String, String> nameValMap = new HashMap<String, String>();
+ final Iterator metaFieldNameItr = getMetaFieldNames();
+ while ( metaFieldNameItr.hasNext() ) {
+ final String metaFieldName = (String) metaFieldNameItr.next();
+ final String metaFieldValue = rs.getString( metaFieldName );
+ nameValMap.put( metaFieldName, metaFieldValue );
}
- return metaDataHitSet;
- }
- catch (SQLException e)
- {
- throw new DictionaryException(e);
- }
- }
+ final MetaDataHit mdh = new GenericMetaDataHitImpl( nameValMap );
+ metaDataHitSet.add( mdh );
+ }
+ return metaDataHitSet;
+ } catch ( SQLException e ) {
+ throw new DictionaryException( e );
+ }
+ }
}
Modified: incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/lucene/LuceneDictionaryImpl.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/lucene/LuceneDictionaryImpl.java?rev=1446793&r1=1446792&r2=1446793&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/lucene/LuceneDictionaryImpl.java (original)
+++ incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/lucene/LuceneDictionaryImpl.java Fri Feb 15 22:26:53 2013
@@ -18,12 +18,7 @@
*/
package org.apache.ctakes.dictionary.lookup.lucene;
-import java.io.IOException;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.ctakes.dictionary.lookup.BaseDictionaryImpl;
+import org.apache.ctakes.dictionary.lookup.AbstractBaseDictionary;
import org.apache.ctakes.dictionary.lookup.Dictionary;
import org.apache.ctakes.dictionary.lookup.DictionaryException;
import org.apache.ctakes.dictionary.lookup.MetaDataHit;
@@ -34,123 +29,116 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.queryparser.classic.QueryParserBase;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.Query;
-import org.apache.lucene.search.ScoreDoc;
-import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.search.*;
import org.apache.lucene.util.Version;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
/**
- *
* @author Mayo Clinic
*/
-public class LuceneDictionaryImpl extends BaseDictionaryImpl implements Dictionary
-{
- private IndexSearcher iv_searcher;
- private String iv_lookupFieldName;
- //ohnlp-Bugs-3296301 limits the search results to fixed 100 records.
- private int iv_maxHits;
- // LOG4J logger based on class name
- private Logger iv_logger = Logger.getLogger(getClass().getName());
-
- /**
- *
- * Constructor
- *
- */
- public LuceneDictionaryImpl(IndexSearcher searcher, String lookupFieldName)
- {
- this(searcher, lookupFieldName, Integer.MAX_VALUE);
-
- // TODO Only take perfect matches?
- }
-
- /**
- *
- * Constructor
- *
- */
- public LuceneDictionaryImpl(IndexSearcher searcher, String lookupFieldName, int maxListHits)
- {
- iv_searcher = searcher;
- iv_lookupFieldName = lookupFieldName;
- // Added 'maxListHits'
- iv_maxHits = maxListHits;
- // TODO Only take perfect matches?
- }
-
- public Collection getEntries(String str) throws DictionaryException
- {
- Set metaDataHitSet = new HashSet();
-
- try
- {
- Query q = null;
- TopDocs topDoc = null;
- if (str.indexOf('-') == -1) {
- q = new TermQuery(new Term(iv_lookupFieldName, str));
- topDoc = iv_searcher.search(q, iv_maxHits);
- }
- else { // needed the KeyworkAnalyzer for situations where the hypen was included in the f-word
- QueryParser query = new QueryParser(Version.LUCENE_40, iv_lookupFieldName, new KeywordAnalyzer());
- try {
- //CTAKES-63 - I believe all of the chars in the str token should be escaped to avoid issues such as a token ending with ']'
- //topDoc = iv_searcher.search(query.parse(str.replace('-', ' ')), iv_maxHits);
- String escaped = QueryParserBase.escape(str.replace('-', ' '));
- topDoc = iv_searcher.search(query.parse(escaped), iv_maxHits);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (iv_maxHits==0) {
- iv_maxHits=Integer.MAX_VALUE;
- iv_logger.warn("iv_maxHits was 0, using Integer.MAX_VALUE instead");
- }
-
- ScoreDoc[] hits = topDoc.scoreDocs;
- if (hits.length == iv_maxHits) {
- iv_logger.warn("'iv_maxHits' equals the list length returned by the lucene query (" + hits.length+").");
- iv_logger.warn("You may want to consider setting a higher value, since there may be more entries not being returned in the event greater than " +iv_maxHits +" exist.");
- }
- for (int i = 0; i < hits.length; i++) {
- int docId = hits[i].doc;
- Document luceneDoc = iv_searcher.doc(docId);
- MetaDataHit mdh = new LuceneDocumentMetaDataHitImpl(luceneDoc);
- metaDataHitSet.add(mdh);
- }
-
- return metaDataHitSet;
- }
- catch (IOException ioe)
- {
- throw new DictionaryException(ioe);
- }
- }
-
- public boolean contains(String str) throws DictionaryException
- {
- try
- {
- Query q = new TermQuery(new Term(iv_lookupFieldName, str));
-
- TopDocs topDoc = iv_searcher.search(q, iv_maxHits);
- ScoreDoc[] hits = topDoc.scoreDocs;
- if ((hits != null) && (hits.length > 0))
- {
- return true;
- }
- else
- {
- return false;
+public class LuceneDictionaryImpl extends AbstractBaseDictionary implements Dictionary {
+ final private IndexSearcher iv_searcher;
+ final private String iv_lookupFieldName;
+ //ohnlp-Bugs-3296301 limits the search results to fixed 100 records.
+ private int iv_maxHits;
+ // LOG4J logger based on class name
+ private Logger iv_logger = Logger.getLogger( getClass().getName() );
+
+ /**
+ * Constructor
+ */
+ public LuceneDictionaryImpl( final IndexSearcher searcher, final String lookupFieldName ) {
+ this( searcher, lookupFieldName, Integer.MAX_VALUE );
+
+ // TODO Only take perfect matches?
+ }
+
+ /**
+ * Constructor
+ */
+ public LuceneDictionaryImpl( final IndexSearcher searcher, final String lookupFieldName, final int maxListHits ) {
+ iv_searcher = searcher;
+ iv_lookupFieldName = lookupFieldName;
+ // Added 'maxListHits'
+ iv_maxHits = maxListHits;
+ // TODO Only take perfect matches?
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Collection<MetaDataHit> getEntries( final String text ) throws DictionaryException {
+ final Set<MetaDataHit> metaDataHitSet = new HashSet<MetaDataHit>();
+
+ try {
+ Query q = null;
+ TopDocs topDoc = null;
+ if ( text.indexOf( '-' ) == -1 ) {
+ q = new TermQuery( new Term( iv_lookupFieldName, text ) );
+ topDoc = iv_searcher.search( q, iv_maxHits );
+ } else { // needed the KeyworkAnalyzer for situations where the hypen was included in the f-word
+ final QueryParser query = new QueryParser( Version.LUCENE_40, iv_lookupFieldName, new KeywordAnalyzer() );
+ try {
+ //CTAKES-63 - I believe all of the chars in the str token should be escaped to avoid issues such as a token ending with ']'
+ //topDoc = iv_searcher.search(query.parse(text.replace('-', ' ')), iv_maxHits);
+ final String escaped = QueryParserBase.escape( text.replace( '-', ' ' ) );
+ topDoc = iv_searcher.search( query.parse( escaped ), iv_maxHits );
+ } catch ( ParseException e ) {
+ // thrown by QueryParser.parse()
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
- }
- catch (IOException ioe)
- {
- throw new DictionaryException(ioe);
- }
-
- }
+ }
+ if ( topDoc == null ) {
+ // avoids possible NPE on topDoc.scoreDocs 12-26-2012 SPF
+ iv_logger.warn( getClass().getName() + " getEntries(..) topDoc is null, returning empty collection" );
+ return Collections.emptySet();
+ }
+ if ( iv_maxHits == 0 ) {
+ iv_maxHits = Integer.MAX_VALUE;
+ iv_logger.warn( "iv_maxHits was 0, using Integer.MAX_VALUE instead" );
+ }
+ final ScoreDoc[] hits = topDoc.scoreDocs;
+ if ( hits.length == iv_maxHits ) {
+ iv_logger.warn( "'iv_maxHits' equals the list length returned by the lucene query (" + hits.length + ")." );
+ iv_logger.warn(
+ "You may want to consider setting a higher value, since there may be more entries not being returned in the event greater than "
+ + iv_maxHits + " exist." );
+ }
+ for ( ScoreDoc scoreDoc : hits ) {
+ final Document luceneDoc = iv_searcher.doc( scoreDoc.doc );
+ final MetaDataHit mdh = new LuceneDocumentMetaDataHitImpl( luceneDoc );
+ metaDataHitSet.add( mdh );
+ }
+ return metaDataHitSet;
+ } catch ( IOException ioe ) {
+ // thrown by IndexSearcher.search(), IndexSearcher.doc()
+ throw new DictionaryException( ioe );
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean contains( final String text ) throws DictionaryException {
+ try {
+ final Query q = new TermQuery( new Term( iv_lookupFieldName, text ) );
+
+ final TopDocs topDoc = iv_searcher.search( q, iv_maxHits );
+ final ScoreDoc[] hits = topDoc.scoreDocs;
+ return hits != null && hits.length > 0;
+ } catch ( IOException ioe ) {
+ // thrown by IndexSearcher.search()
+ throw new DictionaryException( ioe );
+ }
+
+ }
}
Modified: incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTable.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTable.java?rev=1446793&r1=1446792&r2=1446793&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTable.java (original)
+++ incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTable.java Fri Feb 15 22:26:53 2013
@@ -18,88 +18,63 @@
*/
package org.apache.ctakes.dictionary.lookup.strtable;
-import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
- *
* @author Mayo Clinic
*/
-public class StringTable
-{
- // key = indexed field name (String), value = VALUE MAP
- private Map iv_nameMap = new HashMap();
-
- // ROW MAP
- // key = indexed field value (String), value = set of StringTableRows
-
- public StringTable(String[] indexedFieldNames)
- {
- for (int i = 0; i < indexedFieldNames.length; i++)
- {
- iv_nameMap.put(indexedFieldNames[i], new HashMap());
- }
- }
-
- public void addRow(StringTableRow strTableRow)
- {
- Iterator indexedFieldNameItr = iv_nameMap.keySet().iterator();
- while (indexedFieldNameItr.hasNext())
- {
- String indexedFieldName = (String) indexedFieldNameItr.next();
- Map valueMap = (Map) iv_nameMap.get(indexedFieldName);
-
- String indexedFieldValue =
- strTableRow.getFieldValue(indexedFieldName);
- Set rowSet = (Set) valueMap.get(indexedFieldValue);
- if (rowSet == null)
- {
- rowSet = new HashSet();
+final public class StringTable {
+ // key = indexed field name (String), value = VALUE MAP
+ final private Map<String, Map<String, Set<StringTableRow>>> iv_nameMap;
+
+ // ROW MAP
+ // key = indexed field value (String), value = set of StringTableRows
+
+ public StringTable( final String[] indexedFieldNames ) {
+ iv_nameMap = new HashMap<String, Map<String, Set<StringTableRow>>>();
+ for ( String fieldName : indexedFieldNames ) {
+ iv_nameMap.put( fieldName, new HashMap<String, Set<StringTableRow>>() );
+ }
+ }
+
+ public void addRow( final StringTableRow strTableRow ) {
+ for ( Map.Entry<String,Map<String, Set<StringTableRow>>> stringMapEntry : iv_nameMap.entrySet() ) {
+ final Map<String, Set<StringTableRow>> valueMap = stringMapEntry.getValue();
+ final String indexedFieldValue = strTableRow.getFieldValue( stringMapEntry.getKey() );
+
+ Set<StringTableRow> rowSet = valueMap.get( indexedFieldValue );
+ if ( rowSet == null ) {
+ rowSet = new HashSet<StringTableRow>();
+ }
+ rowSet.add( strTableRow );
+ valueMap.put( indexedFieldValue, rowSet );
+
+ }
+ }
+
+ public StringTableRow[] getRows( final String indexedFieldName, final String fieldVal ) {
+ final Map<String, Set<StringTableRow>> valueMap = iv_nameMap.get( indexedFieldName );
+ Set<StringTableRow> rowSet = valueMap.get( fieldVal );
+ if ( rowSet != null ) {
+ return rowSet.toArray( new StringTableRow[rowSet.size()] );
+ } else {
+ return new StringTableRow[0];
+ }
+ }
+
+ public StringTableRow[] getAllRows() {
+ final Set<StringTableRow> allRows = new HashSet<StringTableRow>();
+ for ( Map<String, Set<StringTableRow>> valueMap : iv_nameMap.values() ) {
+ for ( Set<StringTableRow> rowSet : valueMap.values() ) {
+ if ( !rowSet.isEmpty() ) {
+ allRows.addAll( rowSet );
}
- rowSet.add(strTableRow);
+ }
+ }
+ return allRows.toArray( new StringTableRow[allRows.size()] );
+ }
- valueMap.put(indexedFieldValue, rowSet);
- }
- }
-
- public StringTableRow[] getRows(String indexedFieldName, String fieldVal)
- {
- Map valueMap = (Map) iv_nameMap.get(indexedFieldName);
- Set rowSet = (Set) valueMap.get(fieldVal);
- if (rowSet != null)
- {
- return (StringTableRow[]) rowSet.toArray(
- new StringTableRow[rowSet.size()]);
- }
- else
- {
- return new StringTableRow[0];
- }
- }
-
- public StringTableRow[] getAllRows()
- {
- Collection col = new HashSet();
- Iterator valueMapItr = iv_nameMap.values().iterator();
- while (valueMapItr.hasNext())
- {
- Map valueMap = (Map)valueMapItr.next();
- Iterator valueItr = valueMap.values().iterator();
- while (valueItr.hasNext())
- {
- Set rowSet = (Set)valueItr.next();
- if (rowSet.size() > 0)
- {
- col.addAll(rowSet);
- }
- }
- }
-
- return (StringTableRow[]) col.toArray(
- new StringTableRow[col.size()]);
- }
}
Modified: incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTableDictionaryImpl.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTableDictionaryImpl.java?rev=1446793&r1=1446792&r2=1446793&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTableDictionaryImpl.java (original)
+++ incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTableDictionaryImpl.java Fri Feb 15 22:26:53 2013
@@ -18,60 +18,52 @@
*/
package org.apache.ctakes.dictionary.lookup.strtable;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.ctakes.dictionary.lookup.BaseDictionaryImpl;
+import org.apache.ctakes.dictionary.lookup.AbstractBaseDictionary;
import org.apache.ctakes.dictionary.lookup.Dictionary;
import org.apache.ctakes.dictionary.lookup.DictionaryException;
import org.apache.ctakes.dictionary.lookup.MetaDataHit;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
/**
- *
* @author Mayo Clinic
*/
-public class StringTableDictionaryImpl
- extends BaseDictionaryImpl
- implements Dictionary
-{
- private StringTable iv_strTable;
- private String iv_lookupFieldName;
-
- public StringTableDictionaryImpl(
- StringTable strTable,
- String lookupFieldName)
- {
- iv_strTable = strTable;
- iv_lookupFieldName = lookupFieldName;
- }
-
- public boolean contains(String text) throws DictionaryException
- {
- if (iv_strTable.getRows(iv_lookupFieldName, text).length > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- public Collection getEntries(String str) throws DictionaryException
- {
- Set metaDataHitSet = new HashSet();
- StringTableRow[] strTableRows =
- iv_strTable.getRows(iv_lookupFieldName, str);
- for (int i = 0; i < strTableRows.length; i++)
- {
- MetaDataHit mdh =
- new StringTableRowMetaDataHitImpl(strTableRows[i]);
- metaDataHitSet.add(mdh);
- }
-
- return metaDataHitSet;
- }
+public class StringTableDictionaryImpl extends AbstractBaseDictionary implements Dictionary {
+ final private StringTable iv_strTable;
+ final private String iv_lookupFieldName;
+
+ public StringTableDictionaryImpl( final StringTable strTable, final String lookupFieldName ) {
+ iv_strTable = strTable;
+ iv_lookupFieldName = lookupFieldName;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean contains( final String text ) throws DictionaryException {
+ return iv_strTable.getRows( iv_lookupFieldName, text ).length > 0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Collection<MetaDataHit> getEntries( final String text ) throws DictionaryException {
+ final StringTableRow[] strTableRows = iv_strTable.getRows( iv_lookupFieldName, text );
+ if ( strTableRows.length == 0 ) {
+ return Collections.emptySet();
+ }
+ final Set<MetaDataHit> metaDataHitSet = new HashSet<MetaDataHit>();
+ for ( StringTableRow tableRow : strTableRows ) {
+ final MetaDataHit metaDataHit = new StringTableRowMetaDataHitImpl( tableRow );
+ metaDataHitSet.add( metaDataHit );
+ }
+ return metaDataHitSet;
+ }
}
Modified: incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTableRow.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTableRow.java?rev=1446793&r1=1446792&r2=1446793&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTableRow.java (original)
+++ incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/strtable/StringTableRow.java Fri Feb 15 22:26:53 2013
@@ -24,30 +24,25 @@ import java.util.Map;
import java.util.Set;
/**
- *
* @author Mayo Clinic
*/
-public class StringTableRow
-{
- private Map iv_fieldMap = new HashMap();
-
- public Set getNames()
- {
- return iv_fieldMap.keySet();
- }
-
- public Collection getValues()
- {
- return iv_fieldMap.values();
- }
-
- public void addField(String fieldName, String fieldValue)
- {
- iv_fieldMap.put(fieldName, fieldValue);
- }
-
- public String getFieldValue(String fieldName)
- {
- return (String) iv_fieldMap.get(fieldName);
- }
+final public class StringTableRow {
+
+ final private Map<String, String> iv_fieldMap = new HashMap<String, String>();
+
+ public Set<String> getNames() {
+ return iv_fieldMap.keySet();
+ }
+
+ public Collection<String> getValues() {
+ return iv_fieldMap.values();
+ }
+
+ public void addField( final String fieldName, final String fieldValue ) {
+ iv_fieldMap.put( fieldName, fieldValue );
+ }
+
+ public String getFieldValue( final String fieldName ) {
+ return iv_fieldMap.get( fieldName );
+ }
}
Modified: incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/vo/LookupHit.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/vo/LookupHit.java?rev=1446793&r1=1446792&r2=1446793&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/vo/LookupHit.java (original)
+++ incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/vo/LookupHit.java Fri Feb 15 22:26:53 2013
@@ -18,94 +18,100 @@
*/
package org.apache.ctakes.dictionary.lookup.vo;
+import org.apache.ctakes.dictionary.lookup.MetaDataHit;
+
+import javax.annotation.concurrent.Immutable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
-import org.apache.ctakes.dictionary.lookup.MetaDataHit;
-
/**
* Represents a single hit found by a lookup algorithm. Meta data returned by
* the Dictionary is also encapsulated here as well.
- *
+ *
* @author Mayo Clinic
*/
-public class LookupHit
-{
- private int iv_startOffset;
- private int iv_endOffset;
- private MetaDataHit iv_mdh;
-
- public LookupHit(MetaDataHit mdh, int startOffset, int endOffset)
- {
- iv_mdh = mdh;
- iv_startOffset = startOffset;
- iv_endOffset = endOffset;
- }
-
- public int getEndOffset()
- {
- return iv_endOffset;
- }
-
- public MetaDataHit getDictMetaDataHit()
- {
- return iv_mdh;
- }
-
- public int getStartOffset()
- {
- return iv_startOffset;
- }
-
- /**
- * Override default equals method. Two LookupHit objects are equal if their
- * offsets match and their MetaDataHit objects are equal.
- */
- public boolean equals(Object obj)
- {
- if (obj instanceof LookupHit)
- {
- LookupHit lh = (LookupHit) obj;
- if ((iv_startOffset == lh.getStartOffset())
- && (iv_endOffset == lh.getEndOffset()))
- {
- return iv_mdh.equals(lh.getDictMetaDataHit());
- }
- }
- return false;
- }
-
- /**
- * Generates a unique key based on this objects values.
- * @return
- */
- public String getUniqueKey()
- {
- StringBuffer key = new StringBuffer();
-
- key.append("s=");
- key.append(iv_startOffset);
- key.append("/");
- key.append("e=");
- key.append(iv_endOffset);
- key.append("/");
-
- List mfNameList = new ArrayList(iv_mdh.getMetaFieldNames());
- Collections.sort(mfNameList);
- Iterator mfNameItr = mfNameList.iterator();
- while (mfNameItr.hasNext())
- {
- String mfName = (String)mfNameItr.next();
- String mfValue = iv_mdh.getMetaFieldValue(mfName);
- key.append(mfName);
- key.append("=");
- key.append(mfValue);
- key.append(",");
- }
-
- return key.toString();
- }
-}
\ No newline at end of file
+@Immutable
+final public class LookupHit {
+ final private int iv_startOffset;
+ final private int iv_endOffset;
+ final private MetaDataHit iv_mdh;
+ final private int _hashCode;
+
+ public LookupHit( final MetaDataHit mdh, final int startOffset, final int endOffset ) {
+ iv_mdh = mdh;
+ iv_startOffset = startOffset;
+ iv_endOffset = endOffset;
+ _hashCode = mdh.hashCode() * 7 + startOffset * 3 + endOffset;
+ }
+
+ public int getEndOffset() {
+ return iv_endOffset;
+ }
+
+ public MetaDataHit getDictMetaDataHit() {
+ return iv_mdh;
+ }
+
+ public int getStartOffset() {
+ return iv_startOffset;
+ }
+
+ /**
+ * Override default equals method. Two LookupHit objects are equal if their
+ * offsets match and their MetaDataHit objects are equal.
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals( final Object obj ) {
+ if ( obj instanceof LookupHit ) {
+ LookupHit lh = (LookupHit) obj;
+ return iv_startOffset == lh.getStartOffset()
+ && iv_endOffset == lh.getEndOffset()
+ && iv_mdh.equals( lh.getDictMetaDataHit() );
+ }
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ return _hashCode;
+ }
+
+
+
+ /**
+ * Generates a unique key based on this objects values.
+ *
+ * @return
+ */
+ public String getUniqueKey() {
+ StringBuffer key = new StringBuffer();
+
+ key.append( "s=" );
+ key.append( iv_startOffset );
+ key.append( "/" );
+ key.append( "e=" );
+ key.append( iv_endOffset );
+ key.append( "/" );
+
+ List mfNameList = new ArrayList( iv_mdh.getMetaFieldNames() );
+ Collections.sort( mfNameList );
+ Iterator mfNameItr = mfNameList.iterator();
+ while ( mfNameItr.hasNext() ) {
+ String mfName = (String) mfNameItr.next();
+ String mfValue = iv_mdh.getMetaFieldValue( mfName );
+ key.append( mfName );
+ key.append( "=" );
+ key.append( mfValue );
+ key.append( "," );
+ }
+
+ return key.toString();
+ }
+}
Modified: incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/vo/LookupTokenComparator.java
URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/vo/LookupTokenComparator.java?rev=1446793&r1=1446792&r2=1446793&view=diff
==============================================================================
--- incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/vo/LookupTokenComparator.java (original)
+++ incubator/ctakes/trunk/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/vo/LookupTokenComparator.java Fri Feb 15 22:26:53 2013
@@ -18,46 +18,37 @@
*/
package org.apache.ctakes.dictionary.lookup.vo;
+
import java.util.Comparator;
/**
* Comparator that sorts LookupToken objects by their start and end offsets.
- *
+ *
* @author Mayo Clinic
*/
-public class LookupTokenComparator implements Comparator
-{
+public enum LookupTokenComparator implements Comparator<LookupToken> {
+ INSTANCE;
- public int compare(Object o1, Object o2)
- {
- LookupToken tok1 = (LookupToken) o1;
- LookupToken tok2 = (LookupToken) o2;
+ static public LookupTokenComparator getInstance() {
+ return INSTANCE;
+ }
- if (tok1.getStartOffset() < tok2.getStartOffset())
- {
+ public int compare( final LookupToken token1, final LookupToken token2 ) {
+ if ( token1.getStartOffset() < token2.getStartOffset() ) {
+ return -1;
+ } else if ( token1.getStartOffset() > token2.getStartOffset() ) {
+ return 1;
+ } else {
+ // equal start offsets, now look at end offsets
+ if ( token1.getEndOffset() < token2.getEndOffset() ) {
return -1;
- }
- else if (tok1.getStartOffset() > tok2.getStartOffset())
- {
+ } else if ( token1.getEndOffset() > token2.getEndOffset() ) {
return 1;
- }
- else
- {
- // equal start offsets, now look at end offsets
- if (tok1.getEndOffset() < tok2.getEndOffset())
- {
- return -1;
- }
- else if (tok1.getEndOffset() > tok2.getEndOffset())
- {
- return 1;
- }
- else
- {
- // all offsets are equal
- return 0;
- }
- }
- }
-}
\ No newline at end of file
+ } else {
+ // all offsets are equal
+ return 0;
+ }
+ }
+ }
+}