const char *tag;
} pos_tag_s;
+static char *get_request_id()
+{
+ _request_id++;
+ char buffer [sizeof(unsigned int)*8+1];
+ snprintf(buffer, sizeof(buffer), "%u", _request_id);
+
+ return strdup(buffer);
+}
+
+static void process_pos_tag_result(nlp_h nh, bundle *msg)
+{
+ char *request_id = NULL;
+
+ bundle_get_str(msg, "request_id", &request_id);
+
+ const char **tag_array = NULL;
+ const char **token_array = NULL;
+ int len_tag_array = 0;
+ int len_token_array = 0;
+ pos_tag_s* pos_tag_pair = NULL;
+ GList *glist = NULL;
+ nlp_pos_tag_result_h pos_tag_result_h = NULL;
+
+ tag_array = bundle_get_str_array(msg, "return_tag", &len_tag_array);
+ token_array = bundle_get_str_array(msg, "return_token", &len_token_array);
+
+ for (int i=0; i < len_tag_array; i++) {
+ pos_tag_pair = (pos_tag_s* )calloc(1, sizeof(pos_tag_s));
+ if (pos_tag_pair) {
+ pos_tag_pair->token = token_array[i];
+ pos_tag_pair->tag = tag_array[i];
+ }
+
+ LOGD("tag: %s, token : %s", tag_array[i], token_array[i]);
+
+ glist = g_list_append(glist, pos_tag_pair);
+ }
+
+ pos_tag_result_h = glist;
+
+ if (nh->pos_tag_result_cb)
+ nh->pos_tag_result_cb((unsigned int)atoi(request_id), pos_tag_result_h, nh->pos_tag_result_data);
+}
+
+static void process_word_tokenize_result(nlp_h nh, bundle *msg)
+{
+ char *request_id = NULL;
+
+ bundle_get_str(msg, "request_id", &request_id);
+ LOGD("id : %s", request_id);
+
+ const char **word_array = NULL;
+ int len_word_array = 0;
+ GList *glist = NULL;
+ nlp_word_tokenize_result_h word_tokenize_result_h = NULL;
+
+ word_array = bundle_get_str_array(msg, "return_token", &len_word_array);
+
+ for (int i=0; i < len_word_array; i++) {
+ LOGD("word : %s", word_array[i]);
+ glist = g_list_append(glist, (void *)word_array[i]);
+ }
+
+ word_tokenize_result_h = glist;
+
+ if (nh->word_tokenize_result_cb)
+ nh->word_tokenize_result_cb((unsigned int)atoi(request_id), word_tokenize_result_h, nh->word_tokenize_result_data);
+}
+
+static void process_language_detect_result(nlp_h nh, bundle *msg)
+{
+ char *request_id = NULL;
+
+ bundle_get_str(msg, "request_id", &request_id);
+ LOGD("id : %s", request_id);
+
+ const char **word_array = NULL;
+ int len_word_array = 0;
+ //GList *glist = NULL;
+ //nlp_word_tokenize_result_h word_tokenize_result_h = NULL;
+
+ word_array = bundle_get_str_array(msg, "return_token", &len_word_array);
+
+ for (int i=0; i < len_word_array; i++) {
+ LOGD("language : %s", word_array[i]);
+ //glist = g_list_append(glist, (void *)word_array[i]);
+ }
+
+ //word_tokenize_result_h = glist;
+
+ if (nh->language_detect_result_cb)
+ nh->language_detect_result_cb((unsigned int)atoi(request_id), word_array[0], nh->language_detect_result_data);
+}
+
//LCOV_EXCL_START
static void _notify_cb(void *user_data, const char *sender, bundle *msg)
{
if (!command) return;
if (strcmp(command, "pos_tag") == 0) {
- const char **tag_array = NULL;
- const char **token_array = NULL;
- int len_tag_array = 0;
- int len_token_array = 0;
- pos_tag_s* pos_tag_pair = NULL;
- GList *glist = NULL;
- nlp_pos_tag_result_h pos_tag_result_h = NULL;
-
- tag_array = bundle_get_str_array(msg, "return_tag", &len_tag_array);
- token_array = bundle_get_str_array(msg, "return_token", &len_token_array);
-
- for (int i=0; i < len_tag_array; i++) {
- pos_tag_pair = (pos_tag_s* )calloc(1, sizeof(pos_tag_s));
- if (pos_tag_pair) {
- pos_tag_pair->token = token_array[i];
- pos_tag_pair->tag = tag_array[i];
- }
-
- LOGD("tag: %s, token : %s", tag_array[i], token_array[i]);
-
- glist = g_list_append(glist, pos_tag_pair);
- }
-
- pos_tag_result_h = glist;
-
- if (nh->pos_tag_result_cb)
- nh->pos_tag_result_cb((unsigned int)atoi(request_id), pos_tag_result_h, nh->pos_tag_result_data);
+ process_pos_tag_result(nh, msg);
+ }
+ else if (strcmp(command, "word_tokenize") == 0) {
+ process_word_tokenize_result(nh, msg);
+ }
+ else if (strcmp(command, "langdetect") == 0) {
+ process_language_detect_result(nh, msg);
}
}
}
if (!nh->rpc_h) {
+ LOGW("[ERROR] RPC handle is NULL");
return NLP_ERROR_OPERATION_FAILED;
}
}
}
-EXPORT_API int nlp_request_pos_tag(nlp_h nh, const char *str, unsigned int *request_id)
+int send_request(nlp_h nh, const char *command, const char *str, unsigned int *request_id)
{
if (!nh || !str) {
LOGW("[ERROR] Invalid parameter");
}
if (!nh->rpc_h) {
+ LOGW("[ERROR] RPC handle is NULL");
return NLP_ERROR_OPERATION_FAILED;
}
- _request_id++;
- char id[16];
- snprintf(id, sizeof(id), "%u", _request_id);
+ char *req_id = get_request_id();
bundle *msg = bundle_create();
- bundle_add_str(msg, "command", "pos_tag");
+ bundle_add_str(msg, "command", command);
bundle_add_str(msg, "info", str);
- bundle_add_str(msg, "request_id", id);
+ bundle_add_str(msg, "request_id", req_id);
+
+ free(req_id);
rpc_port_proxy_message_invoke_send(nh->rpc_h, msg);
bundle_free(msg);
return NLP_ERROR_NONE;
}
+EXPORT_API int nlp_request_pos_tag(nlp_h nh, const char *str, unsigned int *request_id)
+{
+ return send_request(nh, "pos_tag", str, request_id);
+}
+
EXPORT_API int nlp_set_pos_tag_result_cb(nlp_h nh, nlp_pos_tag_result_cb callback, void *user_data)
{
if (!nh || !callback) {
return NLP_ERROR_NONE;
}
+
+EXPORT_API int nlp_request_word_tokenize(nlp_h nh, const char *str, unsigned int *request_id)
+{
+ return send_request(nh, "word_tokenize", str, request_id);
+}
+
+EXPORT_API int nlp_set_word_tokenize_result_cb(nlp_h nh, nlp_word_tokenize_result_cb callback, void *user_data)
+{
+ if (!nh || !callback) {
+ LOGW("[ERROR] Invalid parameter");
+ return NLP_ERROR_INVALID_PARAMETER;
+ }
+
+ nh->word_tokenize_result_cb = callback;
+ nh->word_tokenize_result_data = user_data;
+
+ return NLP_ERROR_NONE;
+}
+
+EXPORT_API int nlp_unset_word_tokenize_result_cb(nlp_h nh)
+{
+ if (!nh) {
+ LOGW("[ERROR] Invalid parameter");
+ return NLP_ERROR_INVALID_PARAMETER;
+ }
+
+ nh->word_tokenize_result_cb = NULL;
+ nh->word_tokenize_result_data = NULL;
+
+ return NLP_ERROR_NONE;
+}
+
+EXPORT_API int nlp_foreach_word_tokenize(nlp_word_tokenize_result_h word_tokenize_result_h, nlp_word_tokenize_foreach_cb callback, void* user_data)
+{
+ if (!word_tokenize_result_h || !callback) {
+ LOGW("[ERROR] Invalid parameter");
+ return NLP_ERROR_INVALID_PARAMETER;
+ }
+
+ GList *glist = word_tokenize_result_h;
+
+ GList* iter = NULL;
+ char *word = NULL;
+
+ if (g_list_length(glist) > 0) {
+ iter = g_list_first(glist);
+
+ while (NULL != iter) {
+ word = iter->data;
+ if (NULL != word) {
+ LOGD("word(%s)", word);
+ callback(word, user_data);
+ }
+
+ /* next item */
+ iter = g_list_next(iter);
+ }
+
+ g_list_free(glist);
+ glist = NULL;
+ }
+
+ return NLP_ERROR_NONE;
+}
+
+EXPORT_API int nlp_request_language_detect(nlp_h nh, const char *str, unsigned int *request_id)
+{
+ return send_request(nh, "langdetect", str, request_id);
+}
+
+EXPORT_API int nlp_set_language_detect_result_cb(nlp_h nh, nlp_language_detect_result_cb callback, void *user_data)
+{
+ if (!nh || !callback) {
+ LOGW("[ERROR] Invalid parameter");
+ return NLP_ERROR_INVALID_PARAMETER;
+ }
+
+ nh->language_detect_result_cb = callback;
+ nh->language_detect_result_data = user_data;
+
+ return NLP_ERROR_NONE;
+}
+
+EXPORT_API int nlp_unset_language_detect_result_cb(nlp_h nh)
+{
+ if (!nh) {
+ LOGW("[ERROR] Invalid parameter");
+ return NLP_ERROR_INVALID_PARAMETER;
+ }
+
+ nh->language_detect_result_cb = NULL;
+ nh->language_detect_result_data = NULL;
+
+ return NLP_ERROR_NONE;
+}
nlp_pos_tag_result_cb pos_tag_result_cb;
void *pos_tag_result_data;
+
+ nlp_word_tokenize_result_cb word_tokenize_result_cb;
+ void *word_tokenize_result_data;
+
+ nlp_language_detect_result_cb language_detect_result_cb;
+ void *language_detect_result_data;
};
#endif /* __TIZEN_UIX_NLP_PRIVATE_H__ */
typedef GList *nlp_pos_tag_result_h;
+typedef GList *nlp_word_tokenize_result_h;
+
/**
* @brief Called when the connection status is changed.
* @since_tizen 7.0
*/
typedef void (*nlp_connection_status_changed_cb)(nlp_h nh, nlp_connection_status_e status, void* user_data);
+/**
+ * @brief Called when the result of part-of-speech is received.
+ * @since_tizen 7.0
+ * @param[in] request_id The request ID to be distingushed
+ * @param[in] pos_tag_result_h The part-of-speech tag result handle
+ * @param[in] user_data The user data passed from the callback function
+ * @see nlp_set_pos_tag_result_cb()
+ */
typedef void (*nlp_pos_tag_result_cb)(unsigned int request_id, nlp_pos_tag_result_h pos_tag_result_h, void *user_data);
+/**
+ * @brief Called to retrieve the tokenized word.
+ * @since_tizen 7.0
+ * @param[in] token The tokenized word
+ * @param[in] tag The result of POS tag
+ * @see nlp_foreach_pos_tag()
+ */
typedef bool (*nlp_pos_tag_foreach_cb)(const char* token, const char *tag, void* user_data);
+/**
+ * @brief Called when the result of word tokenize is received.
+ * @since_tizen 7.0
+ * @param[in] request_id The request ID to be distingushed
+ * @param[in] word_tokenize_result_h The word tokenize result handle
+ * @param[in] user_data The user data passed from the callback function
+ * @see nlp_set_word_tokenize_result_cb()
+ */
+typedef void (*nlp_word_tokenize_result_cb)(unsigned int request_id, nlp_word_tokenize_result_h word_tokenize_result_h, void *user_data);
+
+/**
+ * @brief Called to retrieve the tokenized word.
+ * @since_tizen 7.0
+ * @param[in] word The tokenized word
+ * @param[in] user_data The user data passed from the callback function
+ * @see nlp_foreach_word_tokenize()
+ */
+typedef bool (*nlp_word_tokenize_foreach_cb)(const char* word, void* user_data);
+
+/**
+ * @brief Called when the result of language detect is received.
+ * @since_tizen 7.0
+ * @param[in] request_id The request ID to be distingushed
+ * @param[in] lang The detected language (ISO 639-1 format such as "en")
+ * @param[in] user_data The user data passed from the callback function
+ * @see nlp_set_language_detect_result_cb()
+ */
+typedef void (*nlp_language_detect_result_cb)(unsigned int request_id, const char *lang, void *user_data);
+
/**
* @brief Creates a handle for nlp.
* @since_tizen 7.0
*/
int nlp_foreach_pos_tag(nlp_pos_tag_result_h pos_tag_result_h, nlp_pos_tag_foreach_cb callback, void* user_data);
+/**
+ * @brief Requests to get the word tokenize given the string @str.
+ * @since_tizen 7.0
+ * @remarks The result can be received by the callback function registered by nlp_set_word_tokenize_result_cb().
+ * @param[in] nh The nlp handle
+ * @param[in] str The string to be requested to get the word tokenize
+ * @param[out] request_id The request ID to be distingushed
+ * @return 0 on success, otherwise a negative error value
+ * @retval #NLP_ERROR_NONE No error
+ * @retval #NLP_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #NLP_ERROR_OPERATION_FAILED Operation failure
+ */
+int nlp_request_word_tokenize(nlp_h ah, const char *str, unsigned int *request_id);
+
+/**
+ * @brief Sets the callback to receive the word tokenize.
+ * @since_tizen 7.0
+ * @param[in] nh The NLP handle
+ * @param[in] callback The callback function to register
+ * @param[in] user_data The user data to be passed to the callback function
+ * @return 0 on success, otherwise a negative error value
+ * @retval #NLP_ERROR_NONE No error
+ * @retval #NLP_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see nlp_unset_word_tokenize_result_cb()
+ */
+int nlp_set_word_tokenize_result_cb(nlp_h nh, nlp_word_tokenize_result_cb callback, void *user_data);
+
+/**
+ * @brief Unsets the callback to receive the word tokenize.
+ * @since_tizen 7.0
+ * @param[in] nh The NLP handle
+ * @return 0 on success, otherwise a negative error value
+ * @retval #NLP_ERROR_NONE No error
+ * @retval #NLP_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see nlp_set_word_tokenize_result_cb()
+ */
+int nlp_unset_word_tokenize_result_cb(nlp_h nh);
+
+/**
+ * @brief Retrieves the tokenized words.
+ * @since_tizen 7.0
+ * @param[in] nlp_word_tokenize_result_h The word tokenize result handle
+ * @param[in] callback The callback function to register
+ * @param[in] user_data The user data to be passed to the callback function
+ * @return 0 on success, otherwise a negative error value
+ * @retval #NLP_ERROR_NONE No error
+ * @retval #NLP_ERROR_INVALID_PARAMETER Invalid parameter
+ */
+int nlp_foreach_word_tokenize(nlp_word_tokenize_result_h word_tokenize_result_h, nlp_word_tokenize_foreach_cb callback, void* user_data);
+
+/**
+ * @brief Requests to detect language given the string @str.
+ * @since_tizen 7.0
+ * @remarks The result can be received by the callback function registered by nlp_set_language_detect_result_cb().
+ * @param[in] nh The nlp handle
+ * @param[in] str The string to be requested to detect language
+ * @param[out] request_id The request ID to be distingushed
+ * @return 0 on success, otherwise a negative error value
+ * @retval #NLP_ERROR_NONE No error
+ * @retval #NLP_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #NLP_ERROR_OPERATION_FAILED Operation failure
+ */
+int nlp_request_language_detect(nlp_h nh, const char *str, unsigned int *request_id);
+
+/**
+ * @brief Sets the callback to receive the detected language.
+ * @since_tizen 7.0
+ * @param[in] nlp_language_detect_result_cb The language detect result handle
+ * @param[in] callback The callback function to register
+ * @param[in] user_data The user data to be passed to the callback function
+ * @return 0 on success, otherwise a negative error value
+ * @retval #NLP_ERROR_NONE No error
+ * @retval #NLP_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see nlp_unset_language_detect_result_cb()
+ */
+int nlp_set_language_detect_result_cb(nlp_h nh, nlp_language_detect_result_cb callback, void *user_data);
+
+/**
+ * @brief Unsets the callback to receive the detected language.
+ * @since_tizen 7.0
+ * @param[in] nh The NLP handle
+ * @return 0 on success, otherwise a negative error value
+ * @retval #NLP_ERROR_NONE No error
+ * @retval #NLP_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see nlp_set_language_detect_result_cb()
+ */
+int nlp_unset_language_detect_result_cb(nlp_h nh);
+
/**
* @}
*/
char *message = NULL;
bundle_get_str(msg, "command", &message);
rpc_port_stub_message_context_get_tag(context, (void *)&sender_client);
- if (sender_client)
+ if (sender_client)
PLOG("[__RPC_PORT__] name(%s), msg(%s)", sender_client->id, message);
else
PERR("[__RPC_PORT__] no sender info. msg(%s)", message);
void pos_tag_result_cb(unsigned int request_id, nlp_pos_tag_result_h pos_tag_result_h, void *user_data)
{
- printf("request id: %d\n", request_id);
+ printf("[%s] request id: %d\n", __func__, request_id);
nlp_foreach_pos_tag(pos_tag_result_h, pos_tag_foreach_cb, NULL);
STOP_LOOP();
}
+bool word_tokenize_foreach_cb(const char* word, void* user_data)
+{
+ printf("[%s] word(%s)\n", __func__, word);
+
+ return true;
+}
+
+void word_tokenize_result_cb(unsigned int request_id, nlp_word_tokenize_result_h word_tokenize_result_h, void *user_data)
+{
+ printf("[%s] request id: %d\n", __func__, request_id);
+
+ nlp_foreach_word_tokenize(word_tokenize_result_h, word_tokenize_foreach_cb, NULL);
+
+ STOP_LOOP();
+}
+
+void language_detect_result_cb(unsigned int request_id, const char *lang, void *user_data)
+{
+ printf("[%s] request id: %d. language: %s\n", __func__, request_id, lang);
+
+ STOP_LOOP();
+}
+
static void _connection_cb(nlp_h nh, nlp_connection_status_e status, void* user_data)
{
printf("status : %d\n", status);
unsigned int request_id = 0;
+ /* English */
+ /* language detect*/
+ const char *str = "Hello World";
+ printf("Request language detect. str(%s)\n", str);
+ nlp_set_language_detect_result_cb(nh, language_detect_result_cb, NULL);
+ nlp_request_language_detect(nh, str, &request_id);
+ printf("request id : %d\n", request_id);
+
+ WAIT_FOR_CALLBACK();
+
+ str = "안녕하세요";
+ printf("Request language detect. str(%s)\n", str);
+ nlp_request_language_detect(nh, str, &request_id);
+ printf("request id : %d\n", request_id);
+
+ WAIT_FOR_CALLBACK();
+
+ /* POS tag */
+ str = "Hello World";
+ printf("Request Pos tag. str(%s)\n", str);
nlp_set_pos_tag_result_cb(nh, pos_tag_result_cb, NULL);
- nlp_request_pos_tag(nh, "Hello World", &request_id);
+ nlp_request_pos_tag(nh, str, &request_id);
+ printf("request id : %d\n", request_id);
+
+ WAIT_FOR_CALLBACK();
+
+ /* Word tokenize */
+ printf("Request word tokenize. str(%s)\n", str);
+ nlp_set_word_tokenize_result_cb(nh, word_tokenize_result_cb, NULL);
+ nlp_request_word_tokenize(nh, str, &request_id);
printf("request id : %d\n", request_id);
WAIT_FOR_CALLBACK();
EXPECT_EQ(ret, NLP_ERROR_INVALID_PARAMETER);
}
+TEST_F(NlpCAPITest, utc_nlp_request_word_tokenize_invalid_parameter)
+{
+ int ret = nlp_request_word_tokenize(NULL, NULL, NULL);
+ EXPECT_EQ(ret, NLP_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(NlpCAPITest, utc_nlp_foreach_word_tokenize_invalid_parameter)
+{
+ int ret = nlp_foreach_word_tokenize(NULL, NULL, NULL);
+ EXPECT_EQ(ret, NLP_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(NlpCAPITest, utc_nlp_request_language_detect_invalid_parameter)
+{
+ int ret = nlp_request_language_detect(NULL, NULL, NULL);
+ EXPECT_EQ(ret, NLP_ERROR_INVALID_PARAMETER);
+}
+
} // namespace