Skip to content

Commit

Permalink
Get sorted list of available languages
Browse files Browse the repository at this point in the history
TessBaseAPI::GetAvailableLanguagesAsVector returned the list of languages
without sorting, so the result was random and not user friendly.

Now `tesseract --list-langs` shows the available languages and scripts
in alphabetic order.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Oct 22, 2018
1 parent 2ef7cc1 commit d75ef80
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/api/baseapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* File: baseapi.cpp
* Description: Simple API for calling tesseract.
* Author: Ray Smith
* Created: Fri Oct 06 15:35:01 PDT 2006
*
* (C) Copyright 2006, Google Inc.
** Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -173,6 +172,13 @@ static void addAvailableLanguages(const STRING &datadir, const STRING &base,
#endif
}

// Compare two STRING values (used for sorting).
static int CompareSTRING(const void* p1, const void* p2) {
const STRING* s1 = static_cast<const STRING*>(p1);
const STRING* s2 = static_cast<const STRING*>(p2);
return strcmp(s1->c_str(), s2->c_str());
}

TessBaseAPI::TessBaseAPI()
: tesseract_(nullptr),
osd_tesseract_(nullptr),
Expand Down Expand Up @@ -459,13 +465,14 @@ void TessBaseAPI::GetLoadedLanguagesAsVector(
}

/**
* Returns the available languages in the vector of STRINGs.
* Returns the available languages in the sorted vector of STRINGs.
*/
void TessBaseAPI::GetAvailableLanguagesAsVector(
GenericVector<STRING>* langs) const {
langs->clear();
if (tesseract_ != nullptr) {
addAvailableLanguages(tesseract_->datadir, "", langs);
langs->sort(CompareSTRING);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/api/baseapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// File: baseapi.h
// Description: Simple API for calling tesseract.
// Author: Ray Smith
// Created: Fri Oct 06 15:35:01 PDT 2006
//
// (C) Copyright 2006, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -261,7 +260,7 @@ class TESS_API TessBaseAPI {
void GetLoadedLanguagesAsVector(GenericVector<STRING>* langs) const;

/**
* Returns the available languages in the vector of STRINGs.
* Returns the available languages in the sorted vector of STRINGs.
*/
void GetAvailableLanguagesAsVector(GenericVector<STRING>* langs) const;

Expand Down

0 comments on commit d75ef80

Please sign in to comment.