summaryrefslogtreecommitdiff
path: root/lingucomponent
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2023-03-03 21:58:48 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2023-03-04 07:08:18 +0000
commit5b9089a23757805ffdc852944d6483cf0db15121 (patch)
treebb001491e5230d0cb9a3b684a95000b69e816484 /lingucomponent
parent92e93f25e5db2f6a2a6368c131ae83244b0c5a7f (diff)
tdf#153948: LanguageTool: cache the prepared request
So that when the same text is re-checked using different settings (e.g., language), the cached result is not used. Change-Id: Ic4ed63c6835f9d3935f1b9541c80822bb52313c2 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148200 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'lingucomponent')
-rw-r--r--lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx36
-rw-r--r--lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx2
2 files changed, 20 insertions, 18 deletions
diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
index fb8228890bdc..0856be3f741d 100644
--- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
+++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
@@ -224,17 +224,8 @@ ProofreadingResult SAL_CALL LanguageToolGrammarChecker::doProofreading(
xRes.nBehindEndOfSentencePosition
= std::min(xRes.nStartOfNextSentencePosition, aText.getLength());
- auto cachedResult = mCachedResults.find(aText);
- if (cachedResult != mCachedResults.end())
- {
- xRes.aErrors = cachedResult->second;
- return xRes;
- }
-
- tools::Long http_code = 0;
- std::string response_body;
OUString langTag(aLocale.Language + "-" + aLocale.Country);
-
+ OString postData;
if (rLanguageOpts.getRestProtocol() == sDuden)
{
std::stringstream aStream;
@@ -245,16 +236,28 @@ ProofreadingResult SAL_CALL LanguageToolGrammarChecker::doProofreading(
aTree.put("spellchecking-level", 3);
aTree.put("correction-proposals", true);
boost::property_tree::write_json(aStream, aTree);
- response_body = makeDudenHttpRequest(checkerURL, HTTP_METHOD::HTTP_POST,
- aStream.str().c_str(), http_code);
+ postData = OString(aStream.str());
}
else
{
- OString postData(OUStringToOString(Concat2View("text=" + aText + "&language=" + langTag),
- RTL_TEXTENCODING_UTF8));
- response_body = makeHttpRequest(checkerURL, HTTP_METHOD::HTTP_POST, postData, http_code);
+ postData = OUStringToOString(Concat2View("text=" + aText + "&language=" + langTag),
+ RTL_TEXTENCODING_UTF8);
+ }
+
+ if (auto cachedResult = mCachedResults.find(postData); cachedResult != mCachedResults.end())
+ {
+ xRes.aErrors = cachedResult->second;
+ return xRes;
}
+ tools::Long http_code = 0;
+ std::string response_body;
+ if (rLanguageOpts.getRestProtocol() == sDuden)
+ response_body
+ = makeDudenHttpRequest(checkerURL, HTTP_METHOD::HTTP_POST, postData, http_code);
+ else
+ response_body = makeHttpRequest(checkerURL, HTTP_METHOD::HTTP_POST, postData, http_code);
+
if (http_code != 200)
{
return xRes;
@@ -274,8 +277,7 @@ ProofreadingResult SAL_CALL LanguageToolGrammarChecker::doProofreading(
parseProofreadingJSONResponse(xRes, response_body);
}
// cache the result
- mCachedResults.insert(
- std::pair<OUString, Sequence<SingleProofreadingError>>(aText, xRes.aErrors));
+ mCachedResults.insert(std::make_pair(postData, xRes.aErrors));
return xRes;
}
diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx
index 00513851888d..0fc5edf877f0 100644
--- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx
+++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx
@@ -47,7 +47,7 @@ class LanguageToolGrammarChecker
css::lang::XServiceInfo, css::lang::XServiceDisplayName>
{
css::uno::Sequence<css::lang::Locale> m_aSuppLocales;
- o3tl::lru_map<OUString, css::uno::Sequence<css::linguistic2::SingleProofreadingError>>
+ o3tl::lru_map<OString, css::uno::Sequence<css::linguistic2::SingleProofreadingError>>
mCachedResults;
LanguageToolGrammarChecker(const LanguageToolGrammarChecker&) = delete;
LanguageToolGrammarChecker& operator=(const LanguageToolGrammarChecker&) = delete;