Java Code Examples for pitt.search.semanticvectors.VectorSearcher#getNearestNeighbors()

The following examples show how to use pitt.search.semanticvectors.VectorSearcher#getNearestNeighbors() . 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 File: SemVectorsPeer.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
private LinkedList<SearchResult> search(String query,VectorStore queryStore,VectorStore searchStore,int numResults)
{
	 VectorSearcher vecSearcher;
	 LinkedList<SearchResult> results = new LinkedList<>();
	 try 
	 {
		 String[] queryTerms = query.split("\\s+");
		 vecSearcher =
	            new VectorSearcher.VectorSearcherCosine(queryStore,
	                                                    searchStore,
	                                                    luceneUtils,
	                                                    flagConfig,
	                                                    queryTerms);
		 results = vecSearcher.getNearestNeighbors(numResults);
		 
		 
	 } catch (pitt.search.semanticvectors.vectors.ZeroVectorException e) {
		 results = new LinkedList<>();

	}
	 return results;
}
 
Example 2
Source File: SemVectorsPeer.java    From seldon-server with 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 3
Source File: SemanticVectorsStore.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
private LinkedList<SearchResult> search(String query,VectorStore queryStore,VectorStore searchStore,int numResults)
{
	 VectorSearcher vecSearcher;
	 LinkedList<SearchResult> results = new LinkedList<>();
	 try 
	 {
		 String[] queryTerms = query.split("\\s+");
		 vecSearcher =
	            new VectorSearcher.VectorSearcherCosine(queryStore,
	                                                    searchStore,
	                                                    luceneUtils,
	                                                    flagConfig,
	                                                    queryTerms);
		 results = vecSearcher.getNearestNeighbors(numResults);
		 
		 
	 } catch (pitt.search.semanticvectors.vectors.ZeroVectorException e) {
		 results = new LinkedList<>();

	}
	 return results;
}
 
Example 4
Source File: SemanticVectorsStore.java    From seldon-server with 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 5
Source File: BeagleTest.java    From semanticvectors with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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.");
	}
}
 
Example 6
Source File: SemanticVectorSearcher.java    From uncc2014watsonsim with GNU General Public License v2.0 4 votes vote down vote up
public List<Passage> query(Question question) {
	List<Passage> passages = new ArrayList<>();
	VectorSearcher[] sv_searchers;
	try {
		sv_searchers = new VectorSearcher[]{
				new VectorSearcher.VectorSearcherCosine( 
				        queryVecReader, resultsVecReader, luceneUtils, 
				        fconfig, question.getTokens().toArray(new String[]{})),
		        /*new VectorSearcher.VectorSearcherLucene(luceneUtils, 
				        fconfig, question.getTokens().toArray(new String[]{})),
		        new VectorSearcher.VectorSearcherMaxSim( 
				        queryVecReader, resultsVecReader, luceneUtils, 
				        fconfig, question.getTokens().toArray(new String[]{})),*/
		        new VectorSearcher.VectorSearcherMinSim(
				        queryVecReader, resultsVecReader, luceneUtils, 
				        fconfig, question.getTokens().toArray(new String[]{})),
		        /*new VectorSearcher.VectorSearcherSubspaceSim(
				        queryVecReader, resultsVecReader, luceneUtils, 
				        fconfig, question.getTokens().toArray(new String[]{})),*/
		};
	
		System.out.println("sv_searchers = " + sv_searchers);
		for (VectorSearcher sv_searcher : sv_searchers)
		if (sv_searcher != null) {
			List<SearchResult> results = sv_searcher.getNearestNeighbors(10);
			System.out.println("result = " + results);
			int rank = 0;
			for (SearchResult result: results) {
				passages.add(new Passage(
						"semvec", 											// Engine
						"",	// Title
						"",	// Text
						result.getObjectVector().getObject().toString())													// Reference
						.score("SEMVEC_RANK", (double) rank++)				// Rank
						.score("SEMVEC_SCORE", (double) result.getScore())	// Score
						.score("SEMVEC_PRESENT", 1.0)
						);
			}
		}
		/*sv_searcher = new VectorSearcher.VectorSearcherCosine( 
        queryVecReader, resultsVecReader, luceneUtils, 
        fconfig, question.tokens.toArray(new String[]{}));*/
	} catch (ZeroVectorException e) {
	// TODO: Under what circumstances does this happen?
	e.printStackTrace();
	}
	return fillFromSources(passages);
}