mirror of
https://github.com/kiwix/java-libkiwix.git
synced 2025-09-11 08:08:46 -04:00
Add wrapper for libzim search and suggestion.
This commit is contained in:
parent
08d59c4221
commit
f9131e901c
35
lib/src/test/org/kiwix/test/libzim/TestQuery.java
Normal file
35
lib/src/test/org/kiwix/test/libzim/TestQuery.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.test.libzim;
|
||||||
|
|
||||||
|
import org.kiwix.libzim.Query;
|
||||||
|
|
||||||
|
public class TestQuery
|
||||||
|
{
|
||||||
|
private Query inner;
|
||||||
|
public Query inner() { return inner; }
|
||||||
|
public TestQuery(String query) {
|
||||||
|
inner = new Query(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestQuery setQuery(String query) { inner.setQuery(query); return this; }
|
||||||
|
public TestQuery setGeorange(float latitude, float longitute, float distance) { inner.setGeorange(latitude, latitude, distance); return this; }
|
||||||
|
|
||||||
|
}
|
31
lib/src/test/org/kiwix/test/libzim/TestSearch.java
Normal file
31
lib/src/test/org/kiwix/test/libzim/TestSearch.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.test.libzim;
|
||||||
|
|
||||||
|
import org.kiwix.libzim.Search;
|
||||||
|
import org.kiwix.libzim.SearchIterator;
|
||||||
|
|
||||||
|
public class TestSearch
|
||||||
|
{
|
||||||
|
private Search inner;
|
||||||
|
public TestSearch(Search _inner) { inner = _inner; }
|
||||||
|
public TestSearchIterator getResults(int start, int maxResults) { return new TestSearchIterator(inner.getResults(start, maxResults)); }
|
||||||
|
public long getEstimatedMatches() { return inner.getEstimatedMatches(); }
|
||||||
|
}
|
40
lib/src/test/org/kiwix/test/libzim/TestSearchIterator.java
Normal file
40
lib/src/test/org/kiwix/test/libzim/TestSearchIterator.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.test.libzim;
|
||||||
|
|
||||||
|
import org.kiwix.libzim.SearchIterator;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class TestSearchIterator implements Iterator<TestEntry>
|
||||||
|
{
|
||||||
|
private SearchIterator inner;
|
||||||
|
public TestSearchIterator(SearchIterator _inner) { inner = _inner; }
|
||||||
|
public String getPath() { return inner.getPath(); }
|
||||||
|
public String getTitle() { return inner.getTitle(); }
|
||||||
|
public int getScore() { return inner.getScore(); }
|
||||||
|
public String getSnippet() { return inner.getSnippet(); }
|
||||||
|
public int getWordCount() { return inner.getWordCount(); }
|
||||||
|
public int getFileIndex() { return inner.getFileIndex(); }
|
||||||
|
public int getSize() { return inner.getSize(); }
|
||||||
|
public String getZimId() { return inner.getZimId(); }
|
||||||
|
|
||||||
|
public boolean hasNext() { return inner.hasNext(); }
|
||||||
|
public TestEntry next() { return new TestEntry(inner.next()); }
|
||||||
|
}
|
47
lib/src/test/org/kiwix/test/libzim/TestSearcher.java
Normal file
47
lib/src/test/org/kiwix/test/libzim/TestSearcher.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.test.libzim;
|
||||||
|
|
||||||
|
import org.kiwix.libzim.Archive;
|
||||||
|
import org.kiwix.libzim.Searcher;
|
||||||
|
import org.kiwix.libzim.Search;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class TestSearcher
|
||||||
|
{
|
||||||
|
private Searcher inner;
|
||||||
|
public TestSearcher(TestArchive archive) throws Exception
|
||||||
|
{
|
||||||
|
inner = new Searcher(archive.inner());
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestSearcher(TestArchive[] archives) throws Exception
|
||||||
|
{
|
||||||
|
inner = new Searcher(
|
||||||
|
Stream.of(archives).map(a -> a.inner()).toArray(Archive[]::new)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestSearcher addArchive(TestArchive archive) { inner.addArchive(archive.inner()); return this; }
|
||||||
|
public TestSearch search(TestQuery query) { return new TestSearch(inner.search(query.inner())); }
|
||||||
|
public void setVerbose(boolean verbose) { inner.setVerbose(verbose); }
|
||||||
|
|
||||||
|
public void dispose() { inner.dispose(); }
|
||||||
|
}
|
32
lib/src/test/org/kiwix/test/libzim/TestSuggestionItem.java
Normal file
32
lib/src/test/org/kiwix/test/libzim/TestSuggestionItem.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.test.libzim;
|
||||||
|
|
||||||
|
import org.kiwix.libzim.SuggestionItem;
|
||||||
|
|
||||||
|
public class TestSuggestionItem
|
||||||
|
{
|
||||||
|
private SuggestionItem inner;
|
||||||
|
public TestSuggestionItem(SuggestionItem _inner) { inner = _inner; }
|
||||||
|
public String getTitle() { return inner.getTitle(); }
|
||||||
|
public String getPath() { return inner.getPath(); }
|
||||||
|
public String getSnippet() { return inner.getSnippet(); }
|
||||||
|
public boolean hasSnippet() { return inner.hasSnippet(); }
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.test.libzim;
|
||||||
|
|
||||||
|
import org.kiwix.libzim.SuggestionIterator;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class TestSuggestionIterator implements Iterator<TestSuggestionItem>
|
||||||
|
{
|
||||||
|
private SuggestionIterator inner;
|
||||||
|
public TestSuggestionIterator(SuggestionIterator _inner) { inner = _inner; }
|
||||||
|
public boolean hasNext() { return inner.hasNext(); }
|
||||||
|
public TestSuggestionItem next() { return new TestSuggestionItem(inner.next()); }
|
||||||
|
}
|
31
lib/src/test/org/kiwix/test/libzim/TestSuggestionSearch.java
Normal file
31
lib/src/test/org/kiwix/test/libzim/TestSuggestionSearch.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.test.libzim;
|
||||||
|
|
||||||
|
import org.kiwix.libzim.SuggestionSearch;
|
||||||
|
|
||||||
|
public class TestSuggestionSearch
|
||||||
|
{
|
||||||
|
private SuggestionSearch inner;
|
||||||
|
public TestSuggestionSearch(SuggestionSearch _inner) { inner = _inner; }
|
||||||
|
|
||||||
|
public TestSuggestionIterator getResults(int start, int maxResults) { return new TestSuggestionIterator(inner.getResults(start, maxResults)); }
|
||||||
|
public long getEstimatedMatches() { return inner.getEstimatedMatches(); }
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.test.libzim;
|
||||||
|
|
||||||
|
import org.kiwix.libzim.SuggestionSearcher;
|
||||||
|
|
||||||
|
import java.io.FileDescriptor;
|
||||||
|
|
||||||
|
public class TestSuggestionSearcher
|
||||||
|
{
|
||||||
|
private SuggestionSearcher inner;
|
||||||
|
public TestSuggestionSearcher(TestArchive archive) throws Exception
|
||||||
|
{
|
||||||
|
inner = new SuggestionSearcher(archive.inner());
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestSuggestionSearch suggest(String query) { return new TestSuggestionSearch(inner.suggest(query)); }
|
||||||
|
public void setVerbose(boolean verbose) { inner.setVerbose(verbose); }
|
||||||
|
public void dispose() { inner.dispose(); }
|
||||||
|
}
|
@ -177,30 +177,30 @@ public class test {
|
|||||||
bookmarkArray = lib.getBookmarks(true);
|
bookmarkArray = lib.getBookmarks(true);
|
||||||
assertEquals(0, bookmarkArray.length);
|
assertEquals(0, bookmarkArray.length);
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
@Test
|
@Test
|
||||||
public void testSearcher() throws Exception, ZimFileFormatException, JNIKiwixException {
|
public void testSearcher() throws Exception, ZimFileFormatException, JNIKiwixException {
|
||||||
Archive archive = new Archive("small.zim");
|
TestArchive archive = new TestArchive("small.zim");
|
||||||
|
|
||||||
Searcher searcher = new Searcher(archive);
|
TestSearcher searcher = new TestSearcher(archive);
|
||||||
Query query = new Query("test");
|
TestQuery query = new TestQuery("test");
|
||||||
Search search = searcher.search(query);
|
TestSearch search = searcher.search(query);
|
||||||
int estimatedMatches = (int) search.getEstimatedMatches();
|
int estimatedMatches = (int) search.getEstimatedMatches();
|
||||||
assertEquals(1, estimatedMatches);
|
assertEquals(1, estimatedMatches);
|
||||||
SearchIterator iterator = search.getResults(0, estimatedMatches);
|
TestSearchIterator iterator = search.getResults(0, estimatedMatches);
|
||||||
assertEquals("Test ZIM file", iterator.getTitle());
|
assertEquals("Test ZIM file", iterator.getTitle());
|
||||||
searcher.dispose();
|
searcher.dispose();
|
||||||
|
|
||||||
SuggestionSearcher suggestionSearcher = new SuggestionSearcher(archive);
|
TestSuggestionSearcher suggestionSearcher = new TestSuggestionSearcher(archive);
|
||||||
SuggestionSearch suggestionSearch = suggestionSearcher.suggest("test");
|
TestSuggestionSearch suggestionSearch = suggestionSearcher.suggest("test");
|
||||||
int matches = (int) suggestionSearch.getEstimatedMatches();
|
int matches = (int) suggestionSearch.getEstimatedMatches();
|
||||||
assertEquals(1, matches);
|
assertEquals(1, matches);
|
||||||
SuggestionIterator results = suggestionSearch.getResults(1, matches);
|
TestSuggestionIterator results = suggestionSearch.getResults(1, matches);
|
||||||
SuggestionItem suggestionItem = results.next();
|
TestSuggestionItem suggestionItem = results.next();
|
||||||
assertEquals("Test ZIM file", suggestionItem.getTitle());
|
assertEquals("Test ZIM file", suggestionItem.getTitle());
|
||||||
suggestionSearcher.dispose();
|
suggestionSearcher.dispose();
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
static
|
static
|
||||||
public void main(String[] args) {
|
public void main(String[] args) {
|
||||||
Library lib = new Library();
|
Library lib = new Library();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user