diff options
author | Henry Castro <hcastro@collabora.com> | 2022-11-21 21:38:40 -0400 |
---|---|---|
committer | Henry Castro <hcastro@collabora.com> | 2023-01-31 16:04:16 +0000 |
commit | 1d89f4835384e6379177e1607bb648b567e3fdab (patch) | |
tree | d28e9df4e56fdd550df4d61c0a814edb15dd5365 /lingucomponent | |
parent | 64b8057a6cd2fbaed6167e2be912914e9569796c (diff) |
lingucomponent: implement custom parse response rest protocol
Response:
HTTP/1.1 200 OK
Transfer encoding: chunked
Content-Type: application/json
{
"hyphenation-positions":[
{"offset":15,"quality":0},
{"offset":20,"quality":1}
],
}, "check-positions":[
{"offset":15,"length":6,"errorcode":4711,"type":"orth","severity":1,"propos
als":["Entwurf","Entw\u00fcrfe"]},
{"offset":22,"length":3,"errorcode":8221,"type":"orth","severity":1}
]
}
Signed-off-by: Henry Castro <hcastro@collabora.com>
Change-Id: I87228237f23306fb367edab1e21ce002aaf13f2c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143108
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145598
Tested-by: Jenkins
Diffstat (limited to 'lingucomponent')
-rw-r--r-- | lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx | 69 | ||||
-rw-r--r-- | lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx | 2 |
2 files changed, 69 insertions, 2 deletions
diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx index ae0b493a32e1..d2c715325f26 100644 --- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx +++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx @@ -62,7 +62,7 @@ namespace Sequence<PropertyValue> lcl_GetLineColorPropertyFromErrorId(const std::string& rErrorId) { Color aColor; - if (rErrorId == "TYPOS") + if (rErrorId == "TYPOS" || rErrorId == "orth") { aColor = COL_LIGHTRED; } @@ -261,13 +261,78 @@ ProofreadingResult SAL_CALL LanguageToolGrammarChecker::doProofreading( return xRes; } - parseProofreadingJSONResponse(xRes, response_body); + if (rLanguageOpts.getRestProtocol() == sDuden) + { + parseDudenResponse(xRes, response_body); + } + else + { + parseProofreadingJSONResponse(xRes, response_body); + } // cache the result mCachedResults.insert( std::pair<OUString, Sequence<SingleProofreadingError>>(aText, xRes.aErrors)); return xRes; } +void LanguageToolGrammarChecker::parseDudenResponse(ProofreadingResult& rResult, + std::string_view aJSONBody) +{ + size_t nSize; + int nProposalSize; + boost::property_tree::ptree aRoot; + std::stringstream aStream(aJSONBody.data()); + boost::property_tree::read_json(aStream, aRoot); + + const boost::optional<boost::property_tree::ptree&> aPositions + = aRoot.get_child_optional("check-positions"); + if (!aPositions || !(nSize = aPositions.get().size())) + { + return; + } + + Sequence<SingleProofreadingError> aChecks(nSize); + auto pChecks = aChecks.getArray(); + size_t nIndex1 = 0, nIndex2 = 0; + auto itPos = aPositions.get().begin(); + while (itPos != aPositions.get().end()) + { + const boost::property_tree::ptree& rTree = itPos->second; + const std::string sType = rTree.get<std::string>("type", ""); + const int nOffset = rTree.get<int>("offset", 0); + const int nLength = rTree.get<int>("length", 0); + + pChecks[nIndex1].nErrorStart = nOffset; + pChecks[nIndex1].nErrorLength = nLength; + pChecks[nIndex1].nErrorType = PROOFREADING_ERROR; + //pChecks[nIndex1].aShortComment = ?? + //pChecks[nIndex1].aFullComment = ?? + pChecks[nIndex1].aProperties = lcl_GetLineColorPropertyFromErrorId(sType); + + const boost::optional<const boost::property_tree::ptree&> aProposals + = rTree.get_child_optional("proposals"); + if (aProposals && (nProposalSize = aProposals.get().size())) + { + pChecks[nIndex1].aSuggestions.realloc(std::min(nProposalSize, MAX_SUGGESTIONS_SIZE)); + + nIndex2 = 0; + auto itProp = aProposals.get().begin(); + auto pSuggestions = pChecks[nIndex1].aSuggestions.getArray(); + while (itProp != aProposals.get().end() && nIndex2 < MAX_SUGGESTIONS_SIZE) + { + pSuggestions[nIndex2++] + = OStringToOUString(itProp->second.data(), RTL_TEXTENCODING_UTF8); + itProp++; + } + } + + nIndex1++; + itPos++; + } + + rResult.aErrors = aChecks; +} + /* rResult is both input and output aJSONBody is the response body from the HTTP Request to LanguageTool API diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx index 1a5da7995716..00513851888d 100644 --- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx +++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx @@ -51,6 +51,8 @@ class LanguageToolGrammarChecker mCachedResults; LanguageToolGrammarChecker(const LanguageToolGrammarChecker&) = delete; LanguageToolGrammarChecker& operator=(const LanguageToolGrammarChecker&) = delete; + static void parseDudenResponse(css::linguistic2::ProofreadingResult& rResult, + std::string_view aJSONBody); static void parseProofreadingJSONResponse(css::linguistic2::ProofreadingResult& rResult, std::string_view aJSONBody); static std::string makeDudenHttpRequest(std::string_view aURL, HTTP_METHOD method, |