LibreOffice Module linguistic (master) 1
spelldta.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <com/sun/star/uno/Reference.h>
21#include <com/sun/star/linguistic2/SpellFailure.hpp>
22#include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp>
23#include <osl/mutex.hxx>
25
26#include <algorithm>
27#include <utility>
28#include <vector>
29
30#include <linguistic/misc.hxx>
32#include <rtl/ref.hxx>
33
34
35using namespace osl;
36using namespace com::sun::star;
37using namespace com::sun::star::beans;
38using namespace com::sun::star::lang;
39using namespace com::sun::star::uno;
40using namespace com::sun::star::linguistic2;
41
42
43namespace linguistic
44{
45
46#define MAX_PROPOSALS 40
47
48static bool SeqHasEntry(
49 const std::vector< OUString > &rSeq,
50 std::u16string_view rTxt)
51{
52 bool bRes = false;
53 sal_Int32 nLen = rSeq.size();
54 for (sal_Int32 i = 0; i < nLen && !bRes; ++i)
55 {
56 if (rTxt == rSeq[i])
57 bRes = true;
58 }
59 return bRes;
60}
61
62
63void SearchSimilarText( const OUString &rText, LanguageType nLanguage,
65 std::vector< OUString > & rDicListProps )
66{
67 if (!xDicList.is())
68 return;
69
70 const uno::Sequence< Reference< XDictionary > >
71 aDics( xDicList->getDictionaries() );
73 *pDic = aDics.getConstArray();
74 sal_Int32 nDics = xDicList->getCount();
75
76 for (sal_Int32 i = 0; i < nDics; i++)
77 {
78 Reference< XDictionary > xDic = pDic[i];
79
80 LanguageType nLang = LinguLocaleToLanguage( xDic->getLocale() );
81
82 if ( xDic.is() && xDic->isActive()
83 && (nLang == nLanguage || LinguIsUnspecified( nLang)) )
84 {
85#if OSL_DEBUG_LEVEL > 0
86 DictionaryType eType = xDic->getDictionaryType();
87 (void) eType;
88 assert( eType != DictionaryType_MIXED && "unexpected dictionary type" );
89#endif
90 const Sequence< Reference< XDictionaryEntry > > aEntries = xDic->getEntries();
91 for (const Reference<XDictionaryEntry>& rEntry : aEntries)
92 {
93 OUString aEntryTxt;
94 if (rEntry.is())
95 {
96 // remove characters used to determine hyphenation positions
97 aEntryTxt = rEntry->getDictionaryWord().replaceAll("=", "");
98 }
99 if (!aEntryTxt.isEmpty() && aEntryTxt.getLength() > 1 && LevDistance( rText, aEntryTxt ) <= 2)
100 rDicListProps.push_back( aEntryTxt );
101 }
102 }
103 }
104}
105
106
107void SeqRemoveNegEntries( std::vector< OUString > &rSeq,
109 LanguageType nLanguage )
110{
111 bool bSthRemoved = false;
112 sal_Int32 nLen = rSeq.size();
113 for (sal_Int32 i = 0; i < nLen; ++i)
114 {
115 Reference< XDictionaryEntry > xNegEntry( SearchDicList( rxDicList,
116 rSeq[i], nLanguage, false, true ) );
117 if (xNegEntry.is())
118 {
119 rSeq[i].clear();
120 bSthRemoved = true;
121 }
122 }
123 if (bSthRemoved)
124 {
125 std::vector< OUString > aNew;
126 // merge sequence without duplicates and empty strings in new empty sequence
127 rSeq = MergeProposalSeqs( aNew, rSeq );
128 }
129}
130
131
132std::vector< OUString > MergeProposalSeqs(
133 std::vector< OUString > &rAlt1,
134 std::vector< OUString > &rAlt2 )
135{
136 std::vector< OUString > aMerged;
137
138 size_t nAltCount1 = rAlt1.size();
139 size_t nAltCount2 = rAlt2.size();
140
141 sal_Int32 nCountNew = std::min<sal_Int32>( nAltCount1 + nAltCount2, sal_Int32(MAX_PROPOSALS) );
142 aMerged.resize( nCountNew );
143
144 sal_Int32 nIndex = 0;
145 sal_Int32 i = 0;
146 for (int j = 0; j < 2; j++)
147 {
148 sal_Int32 nCount = j == 0 ? nAltCount1 : nAltCount2;
149 std::vector< OUString >& rAlt = j == 0 ? rAlt1 : rAlt2;
150 for (i = 0; i < nCount && nIndex < MAX_PROPOSALS; i++)
151 {
152 if (!rAlt[i].isEmpty() &&
153 !SeqHasEntry(aMerged, rAlt[i] ))
154 aMerged[ nIndex++ ] = rAlt[ i ];
155 }
156 }
157 aMerged.resize( nIndex );
158
159 return aMerged;
160}
161
162
164{
166 nType = SpellFailure::IS_NEGATIVE_WORD;
167}
168
169
171 OUString aWord_, LanguageType nLang,
172 const Sequence< OUString > &rAlternatives ) :
173 aAlt (rAlternatives),
174 aWord (std::move(aWord_)),
175 nType (SpellFailure::IS_NEGATIVE_WORD),
176 nLanguage (nLang)
177{
178}
179
180
181SpellAlternatives::~SpellAlternatives()
182{
183}
184
185
186OUString SAL_CALL SpellAlternatives::getWord()
187{
188 MutexGuard aGuard( GetLinguMutex() );
189 return aWord;
190}
191
192
193Locale SAL_CALL SpellAlternatives::getLocale()
194{
195 MutexGuard aGuard( GetLinguMutex() );
196 return LanguageTag::convertToLocale( nLanguage );
197}
198
199
200sal_Int16 SAL_CALL SpellAlternatives::getFailureType()
201{
202 MutexGuard aGuard( GetLinguMutex() );
203 return nType;
204}
205
206
207sal_Int16 SAL_CALL SpellAlternatives::getAlternativesCount()
208{
209 MutexGuard aGuard( GetLinguMutex() );
210 return static_cast<sal_Int16>(aAlt.getLength());
211}
212
213
214Sequence< OUString > SAL_CALL SpellAlternatives::getAlternatives()
215{
216 MutexGuard aGuard( GetLinguMutex() );
217 return aAlt;
218}
219
220
221void SAL_CALL SpellAlternatives::setAlternatives( const uno::Sequence< OUString >& rAlternatives )
222{
223 MutexGuard aGuard( GetLinguMutex() );
224 aAlt = rAlternatives;
225}
226
227
228void SAL_CALL SpellAlternatives::setFailureType( sal_Int16 nFailureType )
229{
230 MutexGuard aGuard( GetLinguMutex() );
231 nType = nFailureType;
232}
233
234
235void SpellAlternatives::SetWordLanguage(const OUString &rWord, LanguageType nLang)
236{
237 MutexGuard aGuard( GetLinguMutex() );
238 aWord = rWord;
239 nLanguage = nLang;
240}
241
242
243void SpellAlternatives::SetFailureType(sal_Int16 nTypeP)
244{
245 MutexGuard aGuard( GetLinguMutex() );
246 nType = nTypeP;
247}
248
249
250void SpellAlternatives::SetAlternatives( const Sequence< OUString > &rAlt )
251{
252 MutexGuard aGuard( GetLinguMutex() );
253 aAlt = rAlt;
254}
255
256
257css::uno::Reference < css::linguistic2::XSpellAlternatives > SpellAlternatives::CreateSpellAlternatives(
258 const OUString &rWord, LanguageType nLang, sal_Int16 nTypeP, const css::uno::Sequence< OUString > &rAlt )
259{
260 rtl::Reference<SpellAlternatives> pAlt = new SpellAlternatives;
261 pAlt->SetWordLanguage( rWord, nLang );
262 pAlt->SetFailureType( nTypeP );
263 pAlt->SetAlternatives( rAlt );
264 return pAlt;
265}
266
267
268} // namespace linguistic
269
270/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)
SpellAlternatives(OUString aWord, LanguageType nLang, const css::uno::Sequence< OUString > &rAlternatives)
int nCount
ScXMLEditAttributeMap::Entry const aEntries[]
DocumentType eType
sal_Int32 nIndex
#define LANGUAGE_NONE
int i
void SearchSimilarText(const OUString &rText, LanguageType nLanguage, Reference< XSearchableDictionaryList > const &xDicList, std::vector< OUString > &rDicListProps)
Definition: spelldta.cxx:63
bool LinguIsUnspecified(LanguageType nLanguage)
Checks if a LanguageType is one of the values that denote absence of language or undetermined languag...
Definition: misc.cxx:88
uno::Reference< XDictionaryEntry > SearchDicList(const uno::Reference< XSearchableDictionaryList > &xDicList, const OUString &rWord, LanguageType nLanguage, bool bSearchPosDics, bool bSearchSpellEntry)
Definition: misc.cxx:248
static bool SeqHasEntry(const std::vector< OUString > &rSeq, std::u16string_view rTxt)
Definition: spelldta.cxx:48
osl::Mutex & GetLinguMutex()
! multi-thread safe mutex for all platforms !!
Definition: misc.cxx:60
void SeqRemoveNegEntries(std::vector< OUString > &rSeq, Reference< XSearchableDictionaryList > const &rxDicList, LanguageType nLanguage)
Definition: spelldta.cxx:107
sal_Int32 LevDistance(std::u16string_view rTxt1, std::u16string_view rTxt2)
Definition: misc.cxx:143
std::vector< OUString > MergeProposalSeqs(std::vector< OUString > &rAlt1, std::vector< OUString > &rAlt2)
Definition: spelldta.cxx:132
LanguageType LinguLocaleToLanguage(const css::lang::Locale &rLocale)
Convert Locale to LanguageType for legacy handling.
Definition: misc.cxx:74
QPRO_FUNC_TYPE nType
#define MAX_PROPOSALS
Definition: spelldta.cxx:46