Coverage Report - org.galagosearch.core.retrieval.structured.StructuredRetrieval
 
Classes in this File Line Coverage Branch Coverage Complexity
StructuredRetrieval
73%
36/49
62%
10/16
0
 
 1  
 // BSD License (http://www.galagosearch.org/license)
 2  
 
 3  
 package org.galagosearch.core.retrieval.structured;
 4  
 
 5  
 import java.io.FileNotFoundException;
 6  
 import java.io.IOException;
 7  
 import java.util.ArrayList;
 8  
 import java.util.PriorityQueue;
 9  
 import org.galagosearch.core.index.StructuredIndex;
 10  
 import org.galagosearch.core.retrieval.query.Node;
 11  
 import org.galagosearch.core.retrieval.query.StructuredQuery;
 12  
 import org.galagosearch.core.retrieval.Retrieval;
 13  
 import org.galagosearch.core.retrieval.ScoredDocument;
 14  
 import org.galagosearch.core.retrieval.query.NodeType;
 15  
 import org.galagosearch.core.retrieval.traversal.AddCombineTraversal;
 16  
 import org.galagosearch.core.retrieval.traversal.ImplicitFeatureCastTraversal;
 17  
 import org.galagosearch.core.retrieval.traversal.IndriWindowCompatibilityTraversal;
 18  
 import org.galagosearch.core.retrieval.traversal.TextFieldRewriteTraversal;
 19  
 import org.galagosearch.core.retrieval.traversal.WeightConversionTraversal;
 20  
 import org.galagosearch.tupleflow.Parameters;
 21  
 
 22  
 /**
 23  
  *
 24  
  * @author trevor
 25  
  */
 26  
 public class StructuredRetrieval extends Retrieval {
 27  
     StructuredIndex index;
 28  
     FeatureFactory featureFactory;
 29  
 
 30  12
     public StructuredRetrieval(StructuredIndex index) {
 31  12
         this.index = index;
 32  12
         Parameters featureParameters = new Parameters();
 33  12
         featureParameters.add("collectionLength", Long.toString(index.getCollectionLength()));
 34  12
         featureParameters.add("documentCount", Long.toString(index.getDocumentCount()));
 35  12
         featureFactory = new FeatureFactory(featureParameters);
 36  12
     }
 37  
 
 38  
     public StructuredRetrieval(String filename) throws FileNotFoundException, IOException {
 39  8
         this(new StructuredIndex(filename));
 40  8
     }
 41  
 
 42  
     public ScoredDocument[] getArrayResults(PriorityQueue<ScoredDocument> scores) {
 43  4
         ScoredDocument[] results = new ScoredDocument[scores.size()];
 44  
 
 45  24
         for (int i = scores.size() - 1; i >= 0; i--) {
 46  20
             results[i] = scores.poll();
 47  
         }
 48  
 
 49  4
         return results;
 50  
     }
 51  
     
 52  
     public NodeType getNodeType(Node node) throws Exception {
 53  0
         NodeType nodeType = index.getNodeType(node);
 54  0
         if (nodeType == null) {
 55  0
             nodeType = featureFactory.getNodeType(node);
 56  
         }
 57  0
         return nodeType;
 58  
     }
 59  
     
 60  
     public StructuredIterator createIterator(Node node) throws Exception {
 61  20
         ArrayList<StructuredIterator> internalIterators = new ArrayList<StructuredIterator>();
 62  
 
 63  20
         for (Node internalNode : node.getInternalNodes()) {
 64  16
             StructuredIterator internalIterator = createIterator(internalNode);
 65  16
             internalIterators.add(internalIterator);
 66  16
         }
 67  
         
 68  20
         StructuredIterator iterator = index.getIterator(node);
 69  20
         if (iterator == null) {
 70  12
             iterator = featureFactory.getIterator(node, internalIterators);
 71  
         }
 72  
         
 73  20
         return iterator;
 74  
     }
 75  
 
 76  
     public Node transformQuery(Node queryTree) throws Exception {
 77  0
         queryTree = StructuredQuery.copy(new AddCombineTraversal(), queryTree);
 78  0
         queryTree = StructuredQuery.copy(new WeightConversionTraversal(), queryTree);
 79  0
         queryTree = StructuredQuery.copy(new IndriWindowCompatibilityTraversal(), queryTree);
 80  0
         queryTree = StructuredQuery.copy(new TextFieldRewriteTraversal(index), queryTree);
 81  0
         queryTree = StructuredQuery.copy(new ImplicitFeatureCastTraversal(this), queryTree);
 82  0
         return queryTree;
 83  
     }
 84  
 
 85  
     /**
 86  
      * Evaluates a query.
 87  
      *
 88  
      * @param queryTree A query tree that has been already transformed with StructuredRetrieval.transformQuery.
 89  
      * @param requested The number of documents to retrieve, at most.
 90  
      * @return
 91  
      * @throws java.lang.Exception
 92  
      */
 93  
     public ScoredDocument[] runQuery(Node queryTree, int requested) throws Exception {
 94  
         // construct the query iterators
 95  4
         ScoreIterator iterator = (ScoreIterator) createIterator(queryTree);
 96  
 
 97  
         // now there should be an iterator at the root of this tree
 98  4
         PriorityQueue<ScoredDocument> queue = new PriorityQueue<ScoredDocument>();
 99  
 
 100  24
         while (!iterator.isDone()) {
 101  20
             int document = iterator.nextCandidate();
 102  20
             int length = index.getLength(document);
 103  20
             double score = iterator.score(document, length);
 104  
 
 105  20
             if (queue.size() <= requested || queue.peek().score < score) {
 106  20
                 ScoredDocument scoredDocument = new ScoredDocument(document, score);
 107  20
                 queue.add(scoredDocument);
 108  
 
 109  20
                 if (queue.size() > requested) {
 110  0
                     queue.poll();
 111  
                 }
 112  
             }
 113  
 
 114  20
             iterator.movePast(document);
 115  20
         }
 116  
 
 117  4
         return getArrayResults(queue);
 118  
     }
 119  
 
 120  
     public String getDocumentName(int document) {
 121  20
         return index.getDocumentName(document);
 122  
     }
 123  
 
 124  
     public void close() throws IOException {
 125  0
         index.close();
 126  0
     }
 127  
 }