-
-
Notifications
You must be signed in to change notification settings - Fork 13
Segmenter using ICU4C #461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3b0bed2
Add segmenter as test type for nodejs with minimal data
sven-oly a1e35ea
Update test and verify data
sven-oly b1c3779
Add segmenter test cases
sven-oly 34f6e00
Updated generator to produce segmentation tests from NodeJS
sven-oly e6b07b4
Updating data gen and characterizing differences in lists
sven-oly 6d7ec26
Remove temporary code
sven-oly b980052
Add CPP segmenter to executor
sven-oly ec76ece
Fix so segmenter data is recomputed
sven-oly 628e3e3
Trying modified segmenter test generation for line
sven-oly 014ed9d
Hacking line break generator to handle some line segmentation
sven-oly 78b4e28
include new segmenter.cpp function
sven-oly 6e6b45e
Merge remote-tracking branch 'upstream/main' into segmenter_nodejs
sven-oly d01ebe4
Merge remote-tracking branch 'upstream/main' into segmenter_cpp
sven-oly 8b38eb5
Update as per comments on this PR
sven-oly 4acafa6
Removing unneeded .gitignore items
sven-oly b0020da
Add classification type
sven-oly f3c6dad
Merge branch 'segmenter_nodejs' into segmenter_cpp
sven-oly 0db4405
Merge remote-tracking branch 'upstream/main' into segmenter_cpp
sven-oly 3060ca5
Fix generator
sven-oly c4c3e8a
Removing empty debug lines
sven-oly 7410086
Update executors/cpp/segmenter.cpp
sven-oly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/****** | ||
* testing segmenter / break iterator for locales | ||
*/ | ||
|
||
#include <json-c/json.h> | ||
#include <json-c/arraylist.h> | ||
|
||
#include <unicode/brkiter.h> | ||
#include <unicode/bytestream.h> | ||
#include <unicode/locid.h> | ||
#include <unicode/uclean.h> | ||
#include <unicode/unistr.h> | ||
#include <unicode/utypes.h> | ||
|
||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
|
||
#include <cstring> | ||
#include <iostream> | ||
#include <string> | ||
|
||
using std::string; | ||
|
||
using icu::BreakIterator; | ||
using icu::Locale; | ||
using icu::StringByteSink; | ||
using icu::UnicodeString; | ||
|
||
void free_string(void* data) { | ||
if (data) { | ||
free(data); | ||
} | ||
} | ||
|
||
auto TestSegmenter(json_object *json_in) -> string { | ||
UErrorCode status = U_ZERO_ERROR; | ||
|
||
json_object *label_obj = json_object_object_get(json_in, "label"); | ||
string label_string = json_object_get_string(label_obj); | ||
|
||
// The locale in which the name is given. | ||
json_object *locale_label_obj = json_object_object_get(json_in, "locale"); | ||
string locale_string = json_object_get_string(locale_label_obj); | ||
Locale locale(locale_string.c_str()); | ||
|
||
|
||
// What we are segmenting... | ||
json_object *input_obj = json_object_object_get(json_in, "input"); | ||
string input = json_object_get_string(input_obj); | ||
|
||
UnicodeString u_input = UnicodeString::fromUTF8(input); | ||
|
||
// The type of conversion requested | ||
json_object *options_obj = json_object_object_get(json_in, "options"); | ||
|
||
json_object *granularity_obj = json_object_object_get(options_obj, "granularity"); | ||
string granularity_value = json_object_get_string(granularity_obj); | ||
|
||
// Create output array to store results | ||
struct json_object* test_result = json_object_new_array(); | ||
|
||
json_object *return_json = json_object_new_object(); | ||
json_object_object_add(return_json, "label", label_obj); | ||
|
||
// The default. | ||
BreakIterator* brk_iterator; | ||
|
||
if (granularity_value == "grapheme_cluster" || | ||
granularity_value == "grapheme") { | ||
brk_iterator = BreakIterator::createCharacterInstance(locale, status); | ||
} else if (granularity_value == "word") { | ||
brk_iterator = BreakIterator::createWordInstance(locale, status); | ||
} else if (granularity_value == "sentence") { | ||
brk_iterator = BreakIterator::createSentenceInstance(locale, status); | ||
} else if (granularity_value == "line") { | ||
brk_iterator = BreakIterator::createLineInstance(locale, status); | ||
} else { | ||
// No such granularity | ||
json_object_object_add( | ||
return_json, | ||
"error", | ||
json_object_new_string("Unknown granularity")); | ||
json_object_object_add( | ||
return_json, | ||
"error_detail", | ||
json_object_new_string(granularity_value.c_str())); | ||
return json_object_to_json_string(return_json); | ||
} | ||
|
||
// Check if there's an error in the creation of the iterator. | ||
if (U_FAILURE(status) != 0) { | ||
// An error in the call. | ||
json_object_object_add( | ||
return_json, | ||
"error", | ||
json_object_new_string("Failure to create break iterator ")); | ||
json_object_object_add( | ||
return_json, | ||
"error_detail", | ||
json_object_new_string(granularity_value.c_str())); | ||
return json_object_to_json_string(return_json); | ||
} | ||
|
||
// We must have an interator | ||
brk_iterator->setText(u_input); | ||
|
||
int32_t start_pos = brk_iterator->first(); | ||
int32_t end_pos = brk_iterator->next(); | ||
|
||
// Loop until we get DONE or an error. | ||
while (end_pos != BreakIterator::DONE) { | ||
// Extract the Unicode string, converting to a c string. | ||
UnicodeString u_target; | ||
u_input.extractBetween(start_pos, end_pos, u_target); | ||
string target; | ||
u_target.toUTF8String(target); | ||
json_object* j_target = json_object_new_string(target.c_str()); | ||
json_object_array_add(test_result, j_target); | ||
start_pos = end_pos; | ||
end_pos = brk_iterator->next(); | ||
} | ||
|
||
// For each, extract the current part of the input string, adding to the output | ||
json_object_object_add(return_json, "result", test_result); | ||
|
||
return json_object_to_json_string(return_json); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Observation:
granularity_value
is an enum, but we're leaving it as a C++string
. We do properly convert enum values into Java enums in the ICU4J executor. Not for this PR but for the future, it would be better to convert enums into C++ enums.