From af9fc834748fbe514e121afe3a15fac952a87d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20P=C3=B6sel?= Date: Thu, 11 May 2017 03:01:17 +0200 Subject: [PATCH 1/5] ResultIterator can't return choices with custom level (closes #119) ChoicesIterator always return only list of alternatives of symbol, it doesn't let us specify different PageIteratorLevel here. So this commit removes the method parameter and renames the whole method to make more sense. --- .../tesseract/android/test/TessBaseAPITest.java | 2 +- .../resultiterator.cpp | 11 +++++------ .../googlecode/tesseract/android/ResultIterator.java | 9 ++++----- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java b/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java index 24b42526f..63f9ab8fe 100644 --- a/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java +++ b/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java @@ -112,7 +112,7 @@ public void testChoiceIterator() { List> choicesAndConfidences; iterator.begin(); do { - choicesAndConfidences = iterator.getChoicesAndConfidence(PageIteratorLevel.RIL_SYMBOL); + choicesAndConfidences = iterator.getSymbolChoicesAndConfidence(); assertNotNull("Invalid result.", choicesAndConfidences); for (Pair choiceAndConfidence : choicesAndConfidences) { diff --git a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp index 7f2424e97..4b71ceb08 100644 --- a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp +++ b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp @@ -48,21 +48,20 @@ jfloat Java_com_googlecode_tesseract_android_ResultIterator_nativeConfidence(JNI return (jfloat) resultIterator->Confidence(enumLevel); } -jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetChoices(JNIEnv *env, - jobject thiz, jlong nativeResultIterator, jint level) { +jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetSymbolChoices(JNIEnv *env, + jobject thiz, jlong nativeResultIterator) { - // Get the actual result iterator and level (as C objects) - PageIteratorLevel enumLevel = (PageIteratorLevel) level; + // Get the actual result iterator (as C object) ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator; - // Create a choice iterator to determine to the number of alternatives + // Create a choice iterator to determine the number of alternatives tesseract::ChoiceIterator ci(*resultIterator); int numberOfAlternatives = 0; do { numberOfAlternatives++; } while (ci.Next()); - // Create a string array to hold the results + // Create a string array to hold the choices jobjectArray ret = (jobjectArray) env->NewObjectArray(numberOfAlternatives, env->FindClass("java/lang/String"), env->NewStringUTF("")); // Save each result to the output array diff --git a/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java b/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java index d970f01fa..b44d2b002 100644 --- a/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java +++ b/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java @@ -76,12 +76,11 @@ public float confidence(int level) { * The default matching text is blank (""). * The default confidence level is zero (0.0) * - * @param level the page iterator level. See {@link PageIteratorLevel}. - * @return A list of pairs with the UTF string and the confidence + * @return A list of pairs with the UTF symbol and the confidence */ - public List> getChoicesAndConfidence(int level) { + public List> getSymbolChoicesAndConfidence() { // Get the native choices - String[] nativeChoices = nativeGetChoices(mNativeResultIterator, level); + String[] nativeChoices = nativeGetSymbolChoices(mNativeResultIterator); // Create the output list ArrayList> pairedResults = new ArrayList>(); @@ -121,7 +120,7 @@ public void delete() { nativeDelete(mNativeResultIterator); } - private static native String[] nativeGetChoices(long nativeResultIterator, int level); + private static native String[] nativeGetSymbolChoices(long nativeResultIterator); private static native String nativeGetUTF8Text(long nativeResultIterator, int level); private static native float nativeConfidence(long nativeResultIterator, int level); From bdd2fe77dd8bf63afe74f55fe85c59da613b0589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20P=C3=B6sel?= Date: Thu, 11 May 2017 03:06:03 +0200 Subject: [PATCH 2/5] Fix size of string holding symbol and confidence We're printing "%s|%.2f" which could end up in "%s|100.00" so it's "strlen(utfText) + 7". --- .../jni/com_googlecode_tesseract_android/resultiterator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp index 4b71ceb08..dc4d1bc18 100644 --- a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp +++ b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp @@ -72,7 +72,7 @@ jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetSymbo const char *utfText = cb.GetUTF8Text(); // Add each string to the object array elements - char newString[strlen(utfText) + 5]; + char newString[strlen(utfText) + 7]; sprintf(newString, "%s|%.2f", utfText, cb.Confidence()); env->SetObjectArrayElement(ret, i, env->NewStringUTF(newString)); From 506d004e690a8aff886332245eb17073ff70c03c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20P=C3=B6sel?= Date: Thu, 11 May 2017 03:35:19 +0200 Subject: [PATCH 3/5] Fix static native method parameter --- .../jni/com_googlecode_tesseract_android/resultiterator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp index dc4d1bc18..73358ca29 100644 --- a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp +++ b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp @@ -49,7 +49,7 @@ jfloat Java_com_googlecode_tesseract_android_ResultIterator_nativeConfidence(JNI } jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetSymbolChoices(JNIEnv *env, - jobject thiz, jlong nativeResultIterator) { + jclass clazz, jlong nativeResultIterator) { // Get the actual result iterator (as C object) ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator; From 56203adc393e850558d337700caa7578b2440b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20P=C3=B6sel?= Date: Thu, 11 May 2017 04:39:52 +0200 Subject: [PATCH 4/5] ResultIterator: Add IsAtBeginningOf and IsAtFinalElement methods --- .../resultiterator.cpp | 17 ++++++++++++ .../tesseract/android/ResultIterator.java | 26 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp index 73358ca29..a0b45c9bd 100644 --- a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp +++ b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp @@ -84,6 +84,23 @@ jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetSymbo return ret; } +jboolean Java_com_googlecode_tesseract_android_ResultIterator_nativeIsAtBeginningOf(JNIEnv *env, + jclass clazz, jlong nativeResultIterator, jint level) { + ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator; + PageIteratorLevel enumLevel = (PageIteratorLevel) level; + + return (jboolean) (resultIterator->IsAtBeginningOf(enumLevel) ? JNI_TRUE : JNI_FALSE); +} + +jboolean Java_com_googlecode_tesseract_android_ResultIterator_nativeIsAtFinalElement(JNIEnv *env, + jclass clazz, jlong nativeResultIterator, jint level, jint element) { + ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator; + PageIteratorLevel enumLevel = (PageIteratorLevel) level; + PageIteratorLevel enumElement = (PageIteratorLevel) element; + + return (jboolean) (resultIterator->IsAtFinalElement(enumLevel, enumElement) ? JNI_TRUE : JNI_FALSE); +} + void Java_com_googlecode_tesseract_android_ResultIterator_nativeDelete(JNIEnv *env, jclass clazz, jlong nativeResultIterator) { ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator; diff --git a/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java b/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java index b44d2b002..3ffcedac7 100644 --- a/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java +++ b/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java @@ -69,6 +69,30 @@ public float confidence(int level) { return nativeConfidence(mNativeResultIterator, level); } + /** + * Returns true if the iterator is at the start of an object at the given + * level. Possible uses include determining if a call to Next(RIL_WORD) + * moved to the start of a RIL_PARA. + * + * @param level the page iterator level. See {@link PageIteratorLevel}. + * @return {@code true} if iterator points to the start of an object at the given level. + */ + public boolean isAtBeginningOf(int level) { + return nativeIsAtBeginningOf(mNativeResultIterator, level); + } + + /** + * Returns whether the iterator is positioned at the last element in a + * given level. (e.g. the last word in a line, the last line in a block) + * + * @param level the page iterator level. See {@link PageIteratorLevel}. + * @param element the page iterator level. See {@link PageIteratorLevel}. + * @return {@code true} if iterator points to the last element in a given level. + */ + public boolean isAtFinalElement(int level, int element) { + return nativeIsAtFinalElement(mNativeResultIterator, level, element); + } + /** * Returns all possible matching text strings and their confidence level * for the current object at the given level. @@ -124,5 +148,7 @@ public void delete() { private static native String nativeGetUTF8Text(long nativeResultIterator, int level); private static native float nativeConfidence(long nativeResultIterator, int level); + private static native boolean nativeIsAtBeginningOf(long nativeResultIterator, int level); + private static native boolean nativeIsAtFinalElement(long nativeResultIterator, int level, int element); private static native void nativeDelete(long nativeIterator); } From bfe2bcfcf1dfdea07b0672f35d6b9218f2ca060a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20P=C3=B6sel?= Date: Thu, 11 May 2017 03:59:49 +0200 Subject: [PATCH 5/5] Fix typo in nativeDelete method --- .../jni/com_googlecode_tesseract_android/resultiterator.cpp | 1 - .../src/com/googlecode/tesseract/android/ResultIterator.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp index a0b45c9bd..cac17f0b0 100644 --- a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp +++ b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp @@ -107,7 +107,6 @@ void Java_com_googlecode_tesseract_android_ResultIterator_nativeDelete(JNIEnv *e if (resultIterator != 0) { delete resultIterator; } - return; } #ifdef __cplusplus diff --git a/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java b/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java index 3ffcedac7..f736c447b 100644 --- a/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java +++ b/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java @@ -150,5 +150,5 @@ public void delete() { private static native float nativeConfidence(long nativeResultIterator, int level); private static native boolean nativeIsAtBeginningOf(long nativeResultIterator, int level); private static native boolean nativeIsAtFinalElement(long nativeResultIterator, int level, int element); - private static native void nativeDelete(long nativeIterator); + private static native void nativeDelete(long nativeResultIterator); }