Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #206 from Robyer/improveResultIterator
Browse files Browse the repository at this point in the history
Improve ResultIterator
  • Loading branch information
rmtheis authored May 14, 2017
2 parents 43c52fe + bfe2bcf commit ba06c7e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void testChoiceIterator() {
List<Pair<String, Double>> choicesAndConfidences;
iterator.begin();
do {
choicesAndConfidences = iterator.getChoicesAndConfidence(PageIteratorLevel.RIL_SYMBOL);
choicesAndConfidences = iterator.getSymbolChoicesAndConfidence();
assertNotNull("Invalid result.", choicesAndConfidences);

for (Pair<String, Double> choiceAndConfidence : choicesAndConfidences) {
Expand Down
31 changes: 23 additions & 8 deletions tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
jclass clazz, 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
Expand All @@ -73,7 +72,7 @@ jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetChoic
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));

Expand All @@ -85,13 +84,29 @@ jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetChoic
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;
if (resultIterator != 0) {
delete resultIterator;
}
return;
}

#ifdef __cplusplus
Expand Down
37 changes: 31 additions & 6 deletions tess-two/src/com/googlecode/tesseract/android/ResultIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,42 @@ 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.
* <p>
* 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<Pair<String, Double>> getChoicesAndConfidence(int level) {
public List<Pair<String, Double>> getSymbolChoicesAndConfidence() {
// Get the native choices
String[] nativeChoices = nativeGetChoices(mNativeResultIterator, level);
String[] nativeChoices = nativeGetSymbolChoices(mNativeResultIterator);

// Create the output list
ArrayList<Pair<String, Double>> pairedResults = new ArrayList<Pair<String, Double>>();
Expand Down Expand Up @@ -121,9 +144,11 @@ 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);
private static native void nativeDelete(long nativeIterator);
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 nativeResultIterator);
}

0 comments on commit ba06c7e

Please sign in to comment.