lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx | 68 +++++++++- lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx | 2 2 files changed, 68 insertions(+), 2 deletions(-)
New commits: commit 3e47763926bea9f43d154fed6379f860101656e5 Author: Henry Castro <[email protected]> AuthorDate: Mon Nov 21 21:38:40 2022 -0400 Commit: Andras Timar <[email protected]> CommitDate: Sat Jan 28 09:23:32 2023 +0000 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 <[email protected]> Change-Id: I87228237f23306fb367edab1e21ce002aaf13f2c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143108 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145608 Reviewed-by: Andras Timar <[email protected]> Tested-by: Andras Timar <[email protected]> diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx index 12d39ef8af93..7e83c9abdc93 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,77 @@ 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 c3b79c95a43c..bec16c57ad80 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(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,
