diff options
author | Lianna07 <liannaxu07@gmail.com> | 2018-08-14 14:52:47 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2018-08-20 17:20:07 +0800 |
commit | 2d761159fa03de06d64ed11b646523e0593b41e7 (patch) | |
tree | 24dcf4f87a515756ddaf6763cd1776a450a7919d | |
parent | 36105b61ca7f31a00d603b83161dcd5aafbbe408 (diff) | |
download | ibus-libpinyin-2d761159fa03de06d64ed11b646523e0593b41e7.tar.gz ibus-libpinyin-2d761159fa03de06d64ed11b646523e0593b41e7.tar.xz ibus-libpinyin-2d761159fa03de06d64ed11b646523e0593b41e7.zip |
add cloudSource from googlelianna07
-rw-r--r-- | src/PYPCloudCandidates.cc | 225 | ||||
-rw-r--r-- | src/PYPCloudCandidates.h | 3 |
2 files changed, 69 insertions, 159 deletions
diff --git a/src/PYPCloudCandidates.cc b/src/PYPCloudCandidates.cc index 0ec8e56..3d1a574 100644 --- a/src/PYPCloudCandidates.cc +++ b/src/PYPCloudCandidates.cc @@ -2,7 +2,7 @@ * * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus * - * Copyright (c) 2018 + * Copyright (c) 2018 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,10 +33,10 @@ CloudCandidates::CloudCandidates (PhoneticEditor * editor) { m_session = soup_session_new (); m_editor = editor; - + m_cloud_state = m_editor->m_config.enableCloudInput (); m_cloud_source = m_editor->m_config.cloudInputSource (); - m_cloud_candidates_number = m_editor->m_config.cloudCandidatesNumber (); + m_cloud_candidates_number = m_editor->m_config.cloudCandidatesNumber (); m_first_cloud_candidate_position = m_editor->m_config.firstCloudCandidatePos (); m_min_cloud_trigger_length = m_editor->m_config.minCloudInputTriggerLen (); m_cloud_flag = FALSE; @@ -46,33 +46,26 @@ CloudCandidates::~CloudCandidates () { } - gboolean CloudCandidates::processCandidates (std::vector<EnhancedCandidate> & candidates) { - + EnhancedCandidate testCan = candidates[m_first_cloud_candidate_position-1]; /*have cloud candidates already*/ if (testCan.m_candidate_type == CANDIDATE_CLOUD_INPUT) return FALSE; - - m_candidates.clear (); - + + /* insert cloud candidates' placeholders */ - std::vector<EnhancedCandidate> tmp; - tmp.clear (); + m_candidates.clear (); for (guint i = 0; i != m_cloud_candidates_number; i++) { - EnhancedCandidate enhanced; + EnhancedCandidate enhanced; enhanced.m_display_string = "..."; enhanced.m_candidate_type = CANDIDATE_CLOUD_INPUT; - tmp.push_back (enhanced); + m_candidates.push_back (enhanced); } - candidates.insert (candidates.begin ()+m_first_cloud_candidate_position-1, tmp.begin(), tmp.end()); - - int size = candidates.size (); - for (int i = 0; i != size; ++i) - m_candidates.push_back (candidates[i]); - + candidates.insert (candidates.begin ()+m_first_cloud_candidate_position-1, m_candidates.begin(), m_candidates.end()); + if (! m_editor->m_config.doublePinyin ()) { const gchar *text = m_editor->m_text; @@ -88,33 +81,24 @@ CloudCandidates::processCandidates (std::vector<EnhancedCandidate> & candidates) gchar *text=g_strjoinv ("",tempArray); if (strlen (text) >= m_min_cloud_trigger_length) - cloudSyncRequest (text, candidates); - + cloudSyncRequest (text, candidates); + g_strfreev (tempArray); g_free (text); } - return TRUE; } int CloudCandidates::selectCandidate (EnhancedCandidate & enhanced) { - pinyin_instance_t * instance = m_editor->m_instance; - - guint id = enhanced.m_candidate_id; assert (CANDIDATE_CLOUD_INPUT == enhanced.m_candidate_type); - - guint len = 0; - pinyin_get_n_candidate (instance, &len); - if (G_UNLIKELY (id >= len)) - return SELECT_CANDIDATE_ALREADY_HANDLED; + if (enhanced.m_display_string == "...") return SELECT_CANDIDATE_ALREADY_HANDLED; return SELECT_CANDIDATE_COMMIT; - } void @@ -126,33 +110,25 @@ CloudCandidates::cloudAsyncRequest (const gchar* requestStr, std::vector<Enhance queryRequest= g_strdup_printf("http://olime.baidu.com/py?input=%s&inputtype=py&bg=0&ed=%d&result=hanzi&resultcoding=utf-8&ch_en=1&clientinfo=web&version=1",requestStr, m_cloud_candidates_number); else if (m_cloud_source == GOOGLE) queryRequest= g_strdup_printf("https://www.google.com/inputtools/request?ime=pinyin&text=%s",requestStr); -// queryRequest= g_strdup_printf("https://inputtools.google.com/request?text=%s&itc=zh-t-i0-pinyin&num=%d&ie=utf-8&oe=utf-8",requestStr, m_cloud_candidates_number); - - SoupRequest *request= soup_session_request (m_session, queryRequest, error); - - soup_request_send_async (request, NULL, cloudResponseCallBack, this); - + SoupMessage *msg = soup_message_new ("GET", queryRequest); + soup_session_send_async (m_session, msg, NULL, cloudResponseCallBack, static_cast<gpointer> (this)); } -void -CloudCandidates::cloudResponseCallBack (GObject *object, GAsyncResult *result, gpointer user_data) +void +CloudCandidates::cloudResponseCallBack (GObject *source_object, GAsyncResult *result, gpointer user_data) { GError **error = NULL; - GInputStream *stream = soup_request_send_finish (SOUP_REQUEST(object), result, error); - + GInputStream *stream = soup_session_send_finish (SOUP_SESSION(source_object), result, error); + gchar buffer[BUFFERLENGTH]; error = NULL; g_input_stream_read (stream, buffer, BUFFERLENGTH, NULL, error); - CloudCandidates *cloudCandidates = (CloudCandidates *)user_data; - - + CloudCandidates *cloudCandidates = static_cast<CloudCandidates *> (user_data); + String res; - res.clear (); + res.clear (); res.append (buffer); - - std::vector<std::string> string_results; - string_results.clear (); - + if (res) { if (cloudCandidates->m_cloud_source == BAIDU) @@ -160,13 +136,8 @@ CloudCandidates::cloudResponseCallBack (GObject *object, GAsyncResult *result, g /*BAIDU */ if (res[11]=='T') { - if (res[49] ==']') - { - /*respond true , but no results*/ - string_results.clear(); - } - else - { + if (res[49] !=']') + { /*respond true , with results*/ gchar **resultsArr = g_strsplit(res.data()+49, "],", 0); guint resultsArrLength = g_strv_length(resultsArr); @@ -174,62 +145,33 @@ CloudCandidates::cloudResponseCallBack (GObject *object, GAsyncResult *result, g { int end =strcspn(resultsArr[i], ","); std::string tmp = g_strndup(resultsArr[i]+2,end-3); - string_results.push_back(tmp); + cloudCandidates->m_candidates[i].m_display_string = tmp; } } - } else if (res[11] == 'F') - { - /*respond false*/ - string_results.clear (); - } else { - /*english words*/ - const gchar *tmp_res = res; - int start = strcspn (tmp_res, "\""); - int end = strcspn (tmp_res+4, "\""); - std::string tmp = g_strndup (tmp_res+start+1, end); - string_results.push_back (tmp); } } else if (cloudCandidates->m_cloud_source == GOOGLE) { /*GOOGLE */ - } - } - - if (!string_results.empty ()) - { - /* correct cloud candidates string */ - /* get enough results which meet up with m_cloud_candidates_number*/ - if (string_results.size () >= cloudCandidates->m_cloud_candidates_number) - { - for (guint i = 0; i != cloudCandidates->m_cloud_candidates_number; i++) { - EnhancedCandidate & enhanced = cloudCandidates->m_editor->m_candidates[i + cloudCandidates->m_first_cloud_candidate_position-1]; - enhanced.m_display_string = string_results[i]; - enhanced.m_candidate_type = CANDIDATE_CLOUD_INPUT; - cloudCandidates->m_candidates[i + cloudCandidates->m_first_cloud_candidate_position-1].m_display_string = string_results[i]; - } - } else { - /* don't get enough results which meet up with m_cloud_candidates_number*/ - for (guint i = 0; i != string_results.size(); ++i) + const gchar *tmp_res = res; + const gchar *prefix = "[\"SUCCESS\""; + if (g_str_has_prefix (tmp_res, prefix)) { - EnhancedCandidate & enhanced = cloudCandidates->m_editor->m_candidates [i+cloudCandidates->m_first_cloud_candidate_position-1]; - enhanced.m_display_string = string_results[i]; - enhanced.m_candidate_type = CANDIDATE_CLOUD_INPUT; - cloudCandidates->m_candidates[i + cloudCandidates->m_first_cloud_candidate_position-1].m_display_string = string_results[i]; + gchar **prefix_arr = g_strsplit (tmp_res, "\",[\"", -1); + gchar *prefix_str = prefix_arr[1]; + gchar **suffix_arr = g_strsplit (prefix_str, "\"],", -1); + std::string tmp = suffix_arr[0]; + cloudCandidates->m_candidates[0].m_display_string = tmp; + g_strfreev (prefix_arr); + g_strfreev (suffix_arr); } } - }else{ - for (guint i = 0; i != cloudCandidates->m_cloud_candidates_number; i++) { - EnhancedCandidate & enhanced = cloudCandidates->m_editor->m_candidates[i+cloudCandidates->m_first_cloud_candidate_position-1]; - enhanced.m_display_string = "..."; - enhanced.m_candidate_type = CANDIDATE_CLOUD_INPUT; - cloudCandidates->m_editor->m_candidates[i + cloudCandidates->m_first_cloud_candidate_position-1].m_display_string = "..."; - } } + cloudCandidates->m_editor->update (); } - + void CloudCandidates::cloudSyncRequest (const gchar* requestStr, std::vector<EnhancedCandidate> & candidates) { @@ -239,104 +181,71 @@ CloudCandidates::cloudSyncRequest (const gchar* requestStr, std::vector<Enhanced queryRequest= g_strdup_printf ("http://olime.baidu.com/py?input=%s&inputtype=py&bg=0&ed=%d&result=hanzi&resultcoding=utf-8&ch_en=1&clientinfo=web&version=1",requestStr, m_cloud_candidates_number); else if (m_cloud_source == GOOGLE) queryRequest= g_strdup_printf ("https://www.google.com/inputtools/request?ime=pinyin&text=%s",requestStr); -// queryRequest= g_strdup_printf ("https://inputtools.google.com/request?text=%s&itc=zh-t-i0-pinyin&num=%d&ie=utf-8&oe=utf-8",requestStr, m_cloud_candidates_number); SoupMessage *msg = soup_message_new ("GET", queryRequest); guint status = soup_session_send_message (m_session, msg); - + SoupMessageBody *msgBody =soup_message_body_new (); soup_message_body_truncate (msgBody); msgBody = msg->response_body; /* clear useless characters */ soup_message_body_flatten(msgBody); SoupBuffer *bufferBody= soup_message_body_get_chunk(msgBody, 0); - + const gchar *buffer= bufferBody->data; String res; - res.clear (); + res.clear (); res.append (buffer); - - std::vector<std::string> string_results; - string_results.clear (); - - + if (res) { if (m_cloud_source == BAIDU) { /*BAIDU */ - if (res[11]=='T') + if (res[11] == 'T') { - if (res[49] ==']') + if (res[49] != ']') { - /*respond true , but no results*/ - string_results.clear (); - } - else - { - /*respond true , with results*/ + /*respond true , with results*/ gchar **resultsArr = g_strsplit (res.data () + 49, "],", 0); guint resultsArrLength = g_strv_length (resultsArr); for (int i = 0; i != resultsArrLength - 1; ++i) { int end =strcspn (resultsArr[i], ","); std::string tmp = g_strndup (resultsArr[i]+2, end-3); - string_results.push_back (tmp); + m_candidates[i].m_display_string = tmp; } } - } else if (res[11] == 'F') - { - /*respond false*/ - string_results.clear (); - } else { - /*english words*/ - const gchar *tmp_res = res; - int start = strcspn (tmp_res, "\""); - int end = strcspn (tmp_res + 4, "\""); - std::string tmp = g_strndup (tmp_res + start + 1, end); - string_results.push_back (tmp); } } else if (m_cloud_source == GOOGLE) { /*GOOGLE */ - } - } - - if (!string_results.empty()) - { - /* correct cloud candidates string */ - /* get enough results which meet up with m_cloud_candidates_number*/ - if (string_results.size() >= m_cloud_candidates_number) - { - for (guint i = 0; i != m_cloud_candidates_number; i++) { - EnhancedCandidate & enhanced = candidates[i + m_first_cloud_candidate_position - 1]; - enhanced.m_display_string = string_results[i]; - enhanced.m_candidate_type = CANDIDATE_CLOUD_INPUT; - m_candidates[i + m_first_cloud_candidate_position - 1].m_display_string = string_results[i]; - } - } else { - /* don't get enough results which meet up with m_cloud_candidates_number*/ - for (guint i = 0; i != string_results.size(); ++i) + const gchar *tmp_res = res; + const gchar *prefix = "[\"SUCCESS\""; + if (g_str_has_prefix (tmp_res, prefix)) { - EnhancedCandidate & enhanced = candidates[i + m_first_cloud_candidate_position - 1]; - enhanced.m_display_string = string_results[i]; - enhanced.m_candidate_type = CANDIDATE_CLOUD_INPUT; - m_candidates[i + m_first_cloud_candidate_position - 1].m_display_string = string_results[i]; + gchar **prefix_arr = g_strsplit (tmp_res, "\",[\"", -1); + gchar *prefix_str = prefix_arr[1]; + gchar **suffix_arr = g_strsplit (prefix_str, "\"],", -1); + std::string tmp = suffix_arr[0]; + m_candidates[0].m_display_string = tmp; + g_strfreev (prefix_arr); + g_strfreev (suffix_arr); } } - } else - { - for (guint i = 0; i != m_cloud_candidates_number; i++) { - EnhancedCandidate & enhanced = candidates[i + m_first_cloud_candidate_position - 1]; - enhanced.m_display_string = "..."; - enhanced.m_candidate_type = CANDIDATE_CLOUD_INPUT; - m_candidates[i + m_first_cloud_candidate_position - 1].m_display_string = "..."; - } } + + for (guint i = 0; i != m_cloud_candidates_number; i++) { + EnhancedCandidate & enhanced = candidates[i + m_first_cloud_candidate_position - 1]; + enhanced.m_display_string = m_candidates[i].m_display_string; + enhanced.m_candidate_type = CANDIDATE_CLOUD_INPUT; + m_candidates.push_back(enhanced); + } + soup_message_body_free (msgBody); - + } - + diff --git a/src/PYPCloudCandidates.h b/src/PYPCloudCandidates.h index 147413d..6e98aa9 100644 --- a/src/PYPCloudCandidates.h +++ b/src/PYPCloudCandidates.h @@ -29,6 +29,8 @@ #include <libsoup/soup.h> #include "PYConfig.h" + + namespace PY { #define BUFFERLENGTH 2048 @@ -64,7 +66,6 @@ private: SoupSession *m_session; protected: - std::vector<EnhancedCandidate> m_candidates; }; |