Java Code Examples for pitt.search.semanticvectors.LuceneUtils

The following examples show how to use pitt.search.semanticvectors.LuceneUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: seldon-server   Source File: SemVectorsPeer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Find similar users by querying the docstore using a query from the terms passed in
 * @param <T>
 * @param terms
 * @param lUtils : lucene utils
 * @param numResults : max number of results to return
 * @param docResult : the result list of return ids T
 * @param docTransform : the transform from document to return id type T
 */
public <T extends Comparable<T>> void findSimilarUsersFromTerms(String[] terms,LuceneUtils lUtils,int numResults,ArrayList<SemVectorResult<T>> docResult,QueryTransform<T> docTransform)
{
	List<SearchResult> results;
	try 
	{
		VectorSearcher vecSearcher =
	            new VectorSearcher.VectorSearcherCosine(termVecReader,
	                                                    docVecReader,
	                                                    luceneUtils,
	                                                    flagConfig,
	                                                    terms);
		results = vecSearcher.getNearestNeighbors(numResults);
	} 
	catch (pitt.search.semanticvectors.vectors.ZeroVectorException e) {
		results = new LinkedList<>();
	}
	for(SearchResult r : results)
	{
		String filename = r.getObjectVector().getObject().toString();
		
		docResult.add(new SemVectorResult<>(docTransform.fromSV(filename),r.getScore()));
	}
}
 
Example 2
Source Project: seldon-server   Source File: VectorStoreRecommender.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param queryVecStore Vector store to use for query generation.
 * @param searchVecStore The vector store to search.
 * @param luceneUtils LuceneUtils object to use for query weighting. (May be null.)
 * @param queryTerms Terms that will be parsed into a query
 * expression. If the string "NOT" appears, terms after this will be negated.
 */
public VectorStoreRecommenderCosine(VectorStore queryVecStore,
                            VectorStore searchVecStore,
                            LuceneUtils luceneUtils,
                            String[] queryTerms,
                            Set<String> exclusions,
                            Set<String> inclusions,
                            String minDoc)
    throws ZeroVectorException {
  super(queryVecStore, searchVecStore, luceneUtils, exclusions,inclusions,minDoc);
  this.queryVector = CompoundVectorBuilder.getQueryVector(queryVecStore,
                                                          luceneUtils,
                                                          FlagConfig.getFlagConfig(null),
                                                          queryTerms);
  if (this.queryVector.isZeroVector()) {
    throw new ZeroVectorException("Query vector is zero ... no results.");
  }
}
 
Example 3
Source Project: seldon-server   Source File: SemanticVectorsStore.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Find similar users by querying the docstore using a query from the terms passed in
 * @param <T>
 * @param terms
 * @param lUtils : lucene utils
 * @param numResults : max number of results to return
 * @param docResult : the result list of return ids T
 * @param docTransform : the transform from document to return id type T
 */
public <T extends Comparable<T>> void findSimilarUsersFromTerms(String[] terms,LuceneUtils lUtils,int numResults,ArrayList<SemVectorResult<T>> docResult,QueryTransform<T> docTransform)
{
	List<SearchResult> results;
	try 
	{
		VectorSearcher vecSearcher =
	            new VectorSearcher.VectorSearcherCosine(termVecReader,
	                                                    docVecReader,
	                                                    luceneUtils,
	                                                    flagConfig,
	                                                    terms);
		results = vecSearcher.getNearestNeighbors(numResults);
	} 
	catch (pitt.search.semanticvectors.vectors.ZeroVectorException e) {
		results = new LinkedList<>();
	}
	for(SearchResult r : results)
	{
		String filename = r.getObjectVector().getObject().toString();
		
		docResult.add(new SemVectorResult<>(docTransform.fromSV(filename),r.getScore()));
	}
}
 
Example 4
/**
 * @param queryVecStore Vector store to use for query generation.
 * @param searchVecStore The vector store to search.
 * @param luceneUtils LuceneUtils object to use for query weighting. (May be null.)
 * @param queryTerms Terms that will be parsed into a query expression. 
 */
public BeagleVectorSearcher(VectorStore queryVecStore, VectorStore searchVecStore,
														LuceneUtils luceneUtils,
														FlagConfig flagConfig,
														String[] queryTerms)
	throws ZeroVectorException 
{
	super(queryVecStore, searchVecStore, luceneUtils, flagConfig);
			
	BeagleCompoundVecBuilder bcvb = new BeagleCompoundVecBuilder(flagConfig);
	
	queryVector = new RealVector(bcvb.getNGramQueryVector(queryVecStore, queryTerms));
			
	if (this.queryVector.isZeroVector()) {
		throw new ZeroVectorException("Query vector is zero ... no results.");
	}
}
 
Example 5
public SemanticVectorSearcher(Environment env) {
	super(env);

	try {
		// How to use SemanticVectors comes from their Wiki.
		// The search function takes many arguments, which are what we are
		// storing as fields here.
		fconfig = FlagConfig.getFlagConfig(
				new String[]{"-luceneindexpath", env.getConfOrDie("lucene_index"),
						"-docvectorsfile", "data/semanticvectors/docvectors.bin",
						"-termvectorsfile", "data/semanticvectors/termvectors.bin"});
		queryVecReader =
				VectorStoreReader.openVectorStore(
						fconfig.termvectorsfile(), fconfig);
		resultsVecReader =
				VectorStoreReader.openVectorStore(
						fconfig.docvectorsfile(), fconfig);
		luceneUtils = new LuceneUtils(fconfig); 
	} catch (IOException e) {
		e.printStackTrace();
	}
	
	Score.register("SEMVEC_RANK", -1, Merge.Mean);
	Score.register("SEMVEC_SCORE", -1, Merge.Mean);
	Score.register("SEMVEC_PRESENT", 0.0, Merge.Sum);
}
 
Example 6
Source Project: seldon-server   Source File: VectorStorePredictor.java    License: Apache License 2.0 5 votes vote down vote up
public VectorStorePredictor(String queryTerm,
		VectorStore queryVecStore,
           VectorStore searchVecStore,
           LuceneUtils luceneUtils) 
{
	this.queryVecStore = queryVecStore;
	this.searchVecStore = searchVecStore;
	this.luceneUtils = luceneUtils;
	
	queryVector = CompoundVectorBuilder.getQueryVectorFromString(queryVecStore,
	        luceneUtils,
	        flagConfig,
	        queryTerm);
}
 
Example 7
Source Project: seldon-server   Source File: VectorStoreRecommender.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Performs basic initialization; subclasses should normally call super() to use this.
 * @param queryVecStore Vector store to use for query generation.
 * @param searchVecStore The vector store to search.
 * @param luceneUtils LuceneUtils object to use for query weighting. (May be null.)
 */
public VectorStoreRecommender(VectorStore queryVecStore,
                      VectorStore searchVecStore,
                      LuceneUtils luceneUtils,
                      Set<String> exclusions,
                      Set<String> inclusions,
                      String minDoc) {
  this.queryVecStore = queryVecStore;
  this.searchVecStore = searchVecStore;
  this.luceneUtils = luceneUtils;
  this.exclusions = exclusions;
  this.inclusions = inclusions;
  this.minDoc = minDoc;
}
 
Example 8
public void testQuery(FlagConfig flagConfig, String searchfile, String indexfile, String query )
{
	VectorSearcher vs;
	LuceneUtils lUtils = null;
	CloseableVectorStore queryVecReader, searchVecReader;
	LinkedList<SearchResult> results;
	int numResults = 20;

	BeagleUtils utils = BeagleUtils.getInstance();
	utils.setFFTCacheSize(100);

	try
	{
		queryVecReader = VectorStoreReader.openVectorStore(indexfile, flagConfig);
		searchVecReader = VectorStoreReader.openVectorStore(searchfile, flagConfig);

		//BeagleCompoundVecBuilder bcb = new BeagleCompoundVecBuilder ();

		String[] queryTerms = query.split(" ");

		// Create VectorSearcher and search for nearest neighbors.
		vs = new BeagleVectorSearcher( queryVecReader, searchVecReader, lUtils, flagConfig, queryTerms);
		System.err.print("Searching term vectors, searchtype BEAGLE ... ");
		queryVecReader.close();
		searchVecReader.close();

		results = vs.getNearestNeighbors(numResults);

	}
	catch (Exception e)
	{
		System.err.println(e.getMessage());
		results = new LinkedList<SearchResult>();
	}

	// Print out results.
	if (results.size() > 0) {
		System.err.println("Search output follows ...\n");
		for (SearchResult result: results) {
			System.out.println(result.getScore() + ":" +
                                                  ((ObjectVector)result.getObjectVector()).getObject().toString());
		}
	} else {
		System.err.println("No search output.");
	}
}