Revision: 15955
http://gate.svn.sourceforge.net/gate/?rev=15955&view=rev
Author: valyt
Date: 2012-07-18 14:41:36 +0000 (Wed, 18 Jul 2012)
Log Message:
-----------
More simple terms operators:
- sort
- limit
Modified Paths:
--------------
mimir/trunk/mimir-core/src/gate/mimir/search/terms/AbstractTermsQuery.java
mimir/trunk/mimir-core/src/gate/mimir/search/terms/TermsQuery.java
Added Paths:
-----------
mimir/trunk/mimir-core/src/gate/mimir/search/terms/LimitTermsQuery.java
mimir/trunk/mimir-core/src/gate/mimir/search/terms/SortedTermsQuery.java
Modified:
mimir/trunk/mimir-core/src/gate/mimir/search/terms/AbstractTermsQuery.java
===================================================================
--- mimir/trunk/mimir-core/src/gate/mimir/search/terms/AbstractTermsQuery.java
2012-07-18 13:12:07 UTC (rev 15954)
+++ mimir/trunk/mimir-core/src/gate/mimir/search/terms/AbstractTermsQuery.java
2012-07-18 14:41:36 UTC (rev 15955)
@@ -28,7 +28,7 @@
/**
* The maximum number of results to be returned.
*/
- protected final int limit;
+ protected final int limit;
public AbstractTermsQuery(boolean stringsEnabled, boolean countsEnabled,
int limit) {
@@ -45,4 +45,19 @@
this(false, false, NO_LIMIT);
}
+ /**
+ * @return the stringsEnabled
+ */
+ public boolean isStringsEnabled() {
+ return stringsEnabled;
+ }
+
+ /**
+ * @return the countsEnabled
+ */
+ public boolean isCountsEnabled() {
+ return countsEnabled;
+ }
+
+
}
Added: mimir/trunk/mimir-core/src/gate/mimir/search/terms/LimitTermsQuery.java
===================================================================
--- mimir/trunk/mimir-core/src/gate/mimir/search/terms/LimitTermsQuery.java
(rev 0)
+++ mimir/trunk/mimir-core/src/gate/mimir/search/terms/LimitTermsQuery.java
2012-07-18 14:41:36 UTC (rev 15955)
@@ -0,0 +1,72 @@
+/*
+ * LimitTermsQuery.java
+ *
+ * Copyright (c) 2007-2011, The University of Sheffield.
+ *
+ * This file is part of GATE Mímir (see http://gate.ac.uk/family/mimir.html),
+ * and is free software, licenced under the GNU Lesser General Public License,
+ * Version 3, June 2007 (also included with this distribution as file
+ * LICENCE-LGPL3.html).
+ *
+ * Valentin Tablan, 18 Jul 2012
+ *
+ * $Id$
+ */
+package gate.mimir.search.terms;
+
+import gate.mimir.search.QueryEngine;
+
+import java.io.IOException;
+
+/**
+ * A wrapper for another terms query that limit the number of returned terms
+ * to a certain value.
+ * <br>
+ * This is not the same as setting the {@link AbstractTermsQuery#limit}
+ * parameter for an {@link AbstractTermsQuery} implementation because, in this
+ * case, the limit is applied <strong>after</strong> the execution of the
+ * wrapped query has completed. This allows for example to wrap a query into a
+ * {@link SortedTermsQuery} (to change the results order) and
+ * <strong>then</strong> limit the number of results.
+ */
+public class LimitTermsQuery extends AbstractTermsQuery {
+
+ protected TermsQuery query;
+
+
+ public LimitTermsQuery(TermsQuery query, int limit) {
+ super(query.isStringsEnabled(), query.isCountsEnabled(), limit);
+ this.query = query;
+ }
+
+
+ /* (non-Javadoc)
+ * @see
gate.mimir.search.terms.TermsQuery#execute(gate.mimir.search.QueryEngine)
+ */
+ @Override
+ public TermsResultSet execute(QueryEngine engine) throws IOException {
+ TermsResultSet trs = query.execute(engine);
+ if(trs.termIds.length > limit) {
+ long[] termIds = new long[limit];
+ System.arraycopy(trs.termIds, 0, termIds, 0, limit);
+ int[] termCounts = null;
+ if(trs.termCounts != null) {
+ termCounts = new int[limit];
+ System.arraycopy(trs.termCounts, 0, termCounts, 0, limit);
+ }
+ String[] termStrings = null;
+ if(trs.termStrings != null) {
+ termStrings = new String[limit];
+ System.arraycopy(trs.termStrings, 0, termStrings, 0, limit);
+ }
+ int[] termLengths = null;
+ if(trs.termLengths != null) {
+ termLengths = new int[limit];
+ System.arraycopy(trs.termLengths, 0, termLengths, 0, limit);
+ }
+ return new TermsResultSet(termIds, termStrings, termLengths, termCounts);
+ } else {
+ return trs;
+ }
+ }
+}
Property changes on:
mimir/trunk/mimir-core/src/gate/mimir/search/terms/LimitTermsQuery.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Added: mimir/trunk/mimir-core/src/gate/mimir/search/terms/SortedTermsQuery.java
===================================================================
--- mimir/trunk/mimir-core/src/gate/mimir/search/terms/SortedTermsQuery.java
(rev 0)
+++ mimir/trunk/mimir-core/src/gate/mimir/search/terms/SortedTermsQuery.java
2012-07-18 14:41:36 UTC (rev 15955)
@@ -0,0 +1,162 @@
+/*
+ * SortedTermsQuery.java
+ *
+ * Copyright (c) 2007-2011, The University of Sheffield.
+ *
+ * This file is part of GATE Mímir (see http://gate.ac.uk/family/mimir.html),
+ * and is free software, licenced under the GNU Lesser General Public License,
+ * Version 3, June 2007 (also included with this distribution as file
+ * LICENCE-LGPL3.html).
+ *
+ * Valentin Tablan, 18 Jul 2012
+ *
+ * $Id$
+ */
+package gate.mimir.search.terms;
+
+import gate.mimir.search.QueryEngine;
+
+import it.unimi.dsi.fastutil.Arrays;
+import it.unimi.dsi.fastutil.Swapper;
+import it.unimi.dsi.fastutil.ints.IntComparator;
+
+import java.io.IOException;
+
+/**
+ * A wrapper for another terms query that simply sorts the returned terms based
+ * on some criteria.
+ */
+public class SortedTermsQuery extends AbstractTermsQuery {
+
+ public static enum SortOrder {
+ /** Sort by ID, ascending. */
+ ID,
+
+ /** Sort by ID (descending) */
+ ID_DESC,
+
+ /** Sort by counts */
+ COUNT,
+
+ /** Sort by counts (descending). */
+ COUNT_DESC,
+ /** Sort by term string */
+ STRING,
+ /** Sort by term string (descending) */
+ STRING_DESC
+ }
+
+ protected TermsQuery query;
+
+ protected SortOrder[] criteria;
+
+ /**
+ * The default sort criteria: returned terms are sorted by:
+ * <ul>
+ * <li>terms count (descending), then by</li>
+ * <li>term string (ascending), then by</li>
+ * <li>term ID (ascending)</li>
+ * </ul>
+ */
+ public static final SortOrder[] DEFAULT_SORT_CRITERIA = new SortOrder[]
+ { SortOrder.COUNT_DESC, SortOrder.STRING, SortOrder.ID };
+
+ /**
+ * Creates a new sorted terms query, wrapping the provided query, and using
+ * the given sort criteria.
+ * @param query
+ * @param criteria
+ */
+ public SortedTermsQuery(TermsQuery query, SortOrder... criteria) {
+ super(query.isStringsEnabled(), query.isCountsEnabled(), NO_LIMIT);
+ this.query = query;
+ this.criteria = criteria;
+ }
+
+ /**
+ * Creates a new sorted terms query, wrapping the provided query, and using
+ * the {@link #DEFAULT_SORT_CRITERIA}.
+ * @param query
+ */
+ public SortedTermsQuery(TermsQuery query) {
+ this(query, DEFAULT_SORT_CRITERIA);
+ }
+
+ /* (non-Javadoc)
+ * @see
gate.mimir.search.terms.TermsQuery#execute(gate.mimir.search.QueryEngine)
+ */
+ @Override
+ public TermsResultSet execute(QueryEngine engine) throws IOException {
+ final TermsResultSet trs = query.execute(engine);
+ Arrays.quickSort(0, trs.termIds.length, new IntComparator() {
+ @Override
+ public int compare(Integer o1, Integer o2) {
+ return compare(o1.intValue(), o2.intValue());
+ }
+
+ @Override
+ public int compare(int k1, int k2) {
+ int retval = 0;
+ for(SortOrder crit: criteria) {
+ switch(crit) {
+ case ID:
+ retval = Long.signum(trs.termIds[k1] - trs.termIds[k2]);
+ break;
+ case ID_DESC:
+ retval = -Long.signum(trs.termIds[k1] - trs.termIds[k2]);
+ break;
+ case COUNT:
+ if(trs.termCounts != null){
+ retval = trs.termCounts[k1] - trs.termCounts[k2];
+ }
+ break;
+ case COUNT_DESC:
+ if(trs.termCounts != null){
+ retval = trs.termCounts[k2] - trs.termCounts[k1];
+ }
+ break;
+ case STRING:
+ if(trs.termStrings != null &&
+ trs.termStrings[k1] != null &&
+ trs.termStrings[k2] != null) {
+ retval = trs.termStrings[k1].compareTo(trs.termStrings[k2]);
+ }
+ break;
+ case STRING_DESC:
+ if(trs.termStrings != null &&
+ trs.termStrings[k1] != null &&
+ trs.termStrings[k2] != null) {
+ retval = trs.termStrings[k2].compareTo(trs.termStrings[k1]);
+ }
+ break;
+ }
+ if(retval != 0) return retval;
+ }
+ return retval;
+ }
+ }, new Swapper() {
+ @Override
+ public void swap(int a, int b) {
+ long termId = trs.termIds[a];
+ trs.termIds[a] = trs.termIds[b];
+ trs.termIds[b] = termId;
+ if(trs.termStrings != null) {
+ String termString = trs.termStrings[a];
+ trs.termStrings[a] = trs.termStrings[b];
+ trs.termStrings[b] = termString;
+ }
+ if(trs.termLengths != null) {
+ int termLen = trs.termLengths[a];
+ trs.termLengths[a] = trs.termLengths[b];
+ trs.termLengths[b] = termLen;
+ }
+ if(trs.termCounts != null) {
+ int termCount = trs.termCounts[a];
+ trs.termCounts[a] = trs.termCounts[b];
+ trs.termCounts[b] = termCount;
+ }
+ }
+ });
+ return trs;
+ }
+}
Property changes on:
mimir/trunk/mimir-core/src/gate/mimir/search/terms/SortedTermsQuery.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Modified: mimir/trunk/mimir-core/src/gate/mimir/search/terms/TermsQuery.java
===================================================================
--- mimir/trunk/mimir-core/src/gate/mimir/search/terms/TermsQuery.java
2012-07-18 13:12:07 UTC (rev 15954)
+++ mimir/trunk/mimir-core/src/gate/mimir/search/terms/TermsQuery.java
2012-07-18 14:41:36 UTC (rev 15955)
@@ -35,4 +35,16 @@
*/
public TermsResultSet execute(QueryEngine engine) throws IOException;
+ /**
+ * Does this query return term strings?
+ * @return
+ */
+ public boolean isStringsEnabled();
+
+ /**
+ * Does this query return term counts?
+ * @return
+ */
+ public boolean isCountsEnabled();
+
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs