summaryrefslogtreecommitdiff
path: root/lingucomponent
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2023-03-03 21:58:48 +0300
committerAndras Timar <andras.timar@collabora.com>2023-03-12 18:44:19 +0100
commit254e2e6f7049b4c47b2ccbfc1fca0c7cc33a218e (patch)
tree19fb9bae82a22a306b0b009e350cc7edc6bf5a41 /lingucomponent
parent514e1a4417133a43614badcf300e85a9f2636ef5 (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 2f06e054a5bd..579b088551b4 100644
--- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
+++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
@@ -220,17 +220,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;
@@ -241,16 +232,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;
@@ -270,8 +273,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 adfbe121c981..3413defe1fa9 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;