Skip to content

Commit ff8366d

Browse files
Added support for spelling correction in our bindings.
* Created `spellingdb.cpp`, and `SpellingsDB` files for this. * Added test cases for spelling corrections.
1 parent ba6e721 commit ff8366d

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

lib/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ String getLibkiwixFiles() {
365365
"${projectDir}/src/main/java/org/kiwix/libkiwix/JNIKiwixException.java " +
366366
"${projectDir}/src/main/java/org/kiwix/libkiwix/Library.java " +
367367
"${projectDir}/src/main/java/org/kiwix/libkiwix/Manager.java " +
368-
"${projectDir}/src/main/java/org/kiwix/libkiwix/Server.java"
368+
"${projectDir}/src/main/java/org/kiwix/libkiwix/Server.java " +
369+
"${projectDir}/src/main/java/org/kiwix/libkiwix/SpellingsDB.java"
369370
}
370371

371372
String getLibzimFiles() {

lib/src/main/cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ add_library(kiwix_wrapper
4848
libkiwix/bookmark.cpp
4949
libkiwix/manager.cpp
5050
libkiwix/illustration.cpp
51+
libkiwix/spellingdb.cpp
5152
)
5253

5354
# Linux target
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2025 Matthieu Gautier <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301, USA.
18+
*/
19+
20+
#include <jni.h>
21+
#include "org_kiwix_libkiwix_SpellingsDB.h"
22+
#include "spelling_correction.h"
23+
#include <zim/archive.h>
24+
#include "utils.h"
25+
#define NATIVE_TYPE kiwix::SpellingsDB
26+
#define TYPENAME libkiwix_SpellingsDB
27+
#include <macros.h>
28+
29+
METHOD(void, setNativeSpellingsDB, jobject archive, jstring cacheDirPath)
30+
{
31+
auto archive1 = getPtr<zim::Archive>(env, archive);
32+
std::string cPath = TO_C(cacheDirPath);
33+
34+
auto spellingsDB = std::make_shared<kiwix::SpellingsDB>(*archive1, cPath);
35+
SET_PTR(spellingsDB);
36+
} CATCH_EXCEPTION()
37+
38+
DISPOSE
39+
40+
METHOD(jobjectArray, getSpellingCorrections, jstring jWord, jint maxCount)
41+
{
42+
auto results = THIS->getSpellingCorrections(TO_C(jWord), TO_C(maxCount));
43+
jclass stringClass = env->FindClass("java/lang/String");
44+
jobjectArray jArray = env->NewObjectArray(results.size(), stringClass, nullptr);
45+
46+
for (size_t i = 0; i < results.size(); ++i)
47+
{
48+
env->SetObjectArrayElement(jArray, i, TO_JNI(results[i]));
49+
}
50+
51+
return jArray;
52+
} CATCH_EXCEPTION(nullptr)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.kiwix.libkiwix;
2+
3+
import org.kiwix.libzim.Archive;
4+
5+
public class SpellingsDB {
6+
public SpellingsDB(Archive archive, String cacheDirPath) {
7+
setNativeSpellingsDB(archive, cacheDirPath);
8+
}
9+
10+
private native void setNativeSpellingsDB(Archive archive, String cacheDirPath);
11+
public native String[] getSpellingCorrections(String word, int maxCount);
12+
13+
@Override
14+
protected void finalize() { dispose(); }
15+
16+
///--------- The wrapper thing
17+
// To delete our native wrapper
18+
public native void dispose();
19+
// A pointer (as a long) to a native Handle
20+
private long nativeHandle;
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2025 Matthieu Gautier <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301, USA.
18+
*/
19+
20+
package org.kiwix.test.libkiwix;
21+
22+
import org.kiwix.libkiwix.SpellingsDB;
23+
import org.kiwix.libzim.Archive;
24+
import org.kiwix.test.libzim.TestArchive;
25+
26+
public class TestSpellingsDB {
27+
private SpellingsDB inner;
28+
29+
public TestSpellingsDB(TestArchive archive, String cacheDirPath) {
30+
inner = new SpellingsDB(archive.inner(), cacheDirPath);
31+
}
32+
33+
public String[] getSpellingCorrections(String word, int maxCount) {
34+
return inner.getSpellingCorrections(word, maxCount);
35+
}
36+
}

lib/src/test/test.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,17 @@ public void testSearcher() throws Exception, ZimFileFormatException, JNIKiwixExc
783783
assertEquals("main.html", suggestionItem.getPath());
784784
assertTrue(suggestionItem.hasSnippet());
785785
assertEquals("<b>Test</b> ZIM file", suggestionItem.getSnippet());
786+
787+
File cacheDir = new File("build/test-cache");
788+
if (cacheDir.exists()) {
789+
cacheDir.delete();
790+
}
791+
cacheDir.mkdirs();
792+
TestSpellingsDB testSpellingsDB = new TestSpellingsDB(archive, cacheDir.getAbsolutePath());
793+
String[] spellingCorrections = testSpellingsDB.getSpellingCorrections("Test ZIM fileeee", 1);
794+
assertEquals("Test ZIM file", spellingCorrections[0]);
795+
String[] spellingCorrections1 = testSpellingsDB.getSpellingCorrections("Test ZIM f", 1);
796+
assertEquals("Test ZIM file", spellingCorrections1[0]);
786797
}
787798
System.gc();
788799
System.runFinalization();

0 commit comments

Comments
 (0)