All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.it.unimi.di.mg4j.mock.search.WeightTest Maven / Gradle / Ivy

Go to download

MG4J (Managing Gigabytes for Java) is a free full-text search engine for large document collections written in Java.

There is a newer version: 5.2.2
Show newest version
package it.unimi.di.mg4j.mock.search;

import static org.junit.Assert.assertEquals;
import it.unimi.di.mg4j.document.StringArrayDocumentCollection;
import it.unimi.di.mg4j.index.Index;
import it.unimi.di.mg4j.query.nodes.And;
import it.unimi.di.mg4j.query.nodes.Query;
import it.unimi.di.mg4j.query.nodes.QueryBuilderVisitorException;
import it.unimi.di.mg4j.query.nodes.Term;
import it.unimi.di.mg4j.query.nodes.Weight;
import it.unimi.di.mg4j.search.DocumentIterator;
import it.unimi.di.mg4j.tool.IndexBuilder;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;


public class WeightTest {
	private static Index index;
	private static String basename;

	@BeforeClass
	public static void setUp() throws ConfigurationException, SecurityException, IOException, URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		basename = File.createTempFile( WeightTest.class.getSimpleName(), "test" ).getCanonicalPath();
		new IndexBuilder( basename, new StringArrayDocumentCollection( "a", "b", "c" ) ).run();
		index = Index.getInstance( basename + "-text", true, true );
	}

	@AfterClass
	public static void tearDown() {
		for( File f: new File( basename ).getParentFile().listFiles( (FileFilter)new PrefixFileFilter( new File( basename ).getName() ) ) )	f.delete();
	}

	@Test
	public void testWeights() throws QueryBuilderVisitorException, IOException {
		Query query = new Weight( 0.5, new And( new Term( "a" ), new Term( "b" ) ) );
		DocumentIteratorBuilderVisitor documentIteratorBuilderVisitor = new DocumentIteratorBuilderVisitor( null, index, Integer.MAX_VALUE );
		DocumentIterator documentIterator = query.accept( documentIteratorBuilderVisitor );
		assertEquals( .5, documentIterator.weight(), 0 );
		documentIterator.dispose();

		query = new Weight( .1, new Weight( 0.5, new And( new Weight( .2, new Term( "a" ) ), new Term( "b" ) ) ) );
		documentIteratorBuilderVisitor = new DocumentIteratorBuilderVisitor( null, index, Integer.MAX_VALUE );
		documentIterator = query.accept( documentIteratorBuilderVisitor );
		assertEquals( .5, documentIterator.weight(), 0 );
		documentIterator.dispose();
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy