Line data Source code
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 <comphelper/string.hxx>
24 : #include <tools/debug.hxx>
25 : #include <osl/mutex.hxx>
26 :
27 : #include <vector>
28 :
29 : #include "linguistic/spelldta.hxx"
30 : #include "lngsvcmgr.hxx"
31 :
32 :
33 : using namespace utl;
34 : using namespace osl;
35 : using namespace com::sun::star;
36 : using namespace com::sun::star::beans;
37 : using namespace com::sun::star::lang;
38 : using namespace com::sun::star::uno;
39 : using namespace com::sun::star::linguistic2;
40 :
41 :
42 : namespace linguistic
43 : {
44 :
45 : #define MAX_PROPOSALS 40
46 :
47 0 : bool SeqHasEntry(
48 : const Sequence< OUString > &rSeq,
49 : const OUString &rTxt)
50 : {
51 0 : bool bRes = false;
52 0 : sal_Int32 nLen = rSeq.getLength();
53 0 : const OUString *pEntry = rSeq.getConstArray();
54 0 : for (sal_Int32 i = 0; i < nLen && !bRes; ++i)
55 : {
56 0 : if (rTxt == pEntry[i])
57 0 : bRes = true;
58 : }
59 0 : return bRes;
60 : }
61 :
62 :
63 0 : void SearchSimilarText( const OUString &rText, sal_Int16 nLanguage,
64 : Reference< XSearchableDictionaryList > &xDicList,
65 : std::vector< OUString > & rDicListProps )
66 : {
67 0 : if (!xDicList.is())
68 0 : return;
69 :
70 : const uno::Sequence< Reference< XDictionary > >
71 0 : aDics( xDicList->getDictionaries() );
72 : const Reference< XDictionary >
73 0 : *pDic = aDics.getConstArray();
74 0 : sal_Int32 nDics = xDicList->getCount();
75 :
76 0 : for (sal_Int32 i = 0; i < nDics; i++)
77 : {
78 0 : Reference< XDictionary > xDic( pDic[i], UNO_QUERY );
79 :
80 0 : sal_Int16 nLang = LinguLocaleToLanguage( xDic->getLocale() );
81 :
82 0 : if ( xDic.is() && xDic->isActive()
83 0 : && (nLang == nLanguage || LinguIsUnspecified( nLang)) )
84 : {
85 : #if OSL_DEBUG_LEVEL > 1
86 : DictionaryType eType = xDic->getDictionaryType();
87 : (void) eType;
88 : DBG_ASSERT( eType != DictionaryType_MIXED, "unexpected dictionary type" );
89 : #endif
90 0 : const Sequence< Reference< XDictionaryEntry > > aEntries = xDic->getEntries();
91 0 : const Reference< XDictionaryEntry > *pEntries = aEntries.getConstArray();
92 0 : sal_Int32 nLen = aEntries.getLength();
93 0 : for (sal_Int32 k = 0; k < nLen; ++k)
94 : {
95 0 : OUString aEntryTxt;
96 0 : if (pEntries[k].is())
97 : {
98 : // remove characters used to determine hyphenation positions
99 0 : aEntryTxt = comphelper::string::remove(pEntries[k]->getDictionaryWord(), '=');
100 : }
101 0 : if (!aEntryTxt.isEmpty() && LevDistance( rText, aEntryTxt ) <= 2)
102 0 : rDicListProps.push_back( aEntryTxt );
103 0 : }
104 : }
105 0 : }
106 : }
107 :
108 :
109 0 : void SeqRemoveNegEntries( Sequence< OUString > &rSeq,
110 : Reference< XSearchableDictionaryList > &rxDicList,
111 : sal_Int16 nLanguage )
112 : {
113 0 : static const OUString aEmpty;
114 0 : bool bSthRemoved = false;
115 0 : sal_Int32 nLen = rSeq.getLength();
116 0 : OUString *pEntries = rSeq.getArray();
117 0 : for (sal_Int32 i = 0; i < nLen; ++i)
118 : {
119 : Reference< XDictionaryEntry > xNegEntry( SearchDicList( rxDicList,
120 0 : pEntries[i], nLanguage, false, true ) );
121 0 : if (xNegEntry.is())
122 : {
123 0 : pEntries[i] = aEmpty;
124 0 : bSthRemoved = true;
125 : }
126 0 : }
127 0 : if (bSthRemoved)
128 : {
129 0 : Sequence< OUString > aNew;
130 : // merge sequence without duplicates and empty strings in new empty sequence
131 0 : aNew = MergeProposalSeqs( aNew, rSeq, false );
132 0 : rSeq = aNew;
133 : }
134 0 : }
135 :
136 :
137 0 : Sequence< OUString > MergeProposalSeqs(
138 : Sequence< OUString > &rAlt1,
139 : Sequence< OUString > &rAlt2,
140 : bool bAllowDuplicates )
141 : {
142 0 : Sequence< OUString > aMerged;
143 :
144 0 : if (0 == rAlt1.getLength() && bAllowDuplicates)
145 0 : aMerged = rAlt2;
146 0 : else if (0 == rAlt2.getLength() && bAllowDuplicates)
147 0 : aMerged = rAlt1;
148 : else
149 : {
150 0 : sal_Int32 nAltCount1 = rAlt1.getLength();
151 0 : const OUString *pAlt1 = rAlt1.getConstArray();
152 0 : sal_Int32 nAltCount2 = rAlt2.getLength();
153 0 : const OUString *pAlt2 = rAlt2.getConstArray();
154 :
155 0 : sal_Int32 nCountNew = std::min( nAltCount1 + nAltCount2, (sal_Int32) MAX_PROPOSALS );
156 0 : aMerged.realloc( nCountNew );
157 0 : OUString *pMerged = aMerged.getArray();
158 :
159 0 : sal_Int32 nIndex = 0;
160 0 : sal_Int32 i = 0;
161 0 : for (int j = 0; j < 2; j++)
162 : {
163 0 : sal_Int32 nCount = j == 0 ? nAltCount1 : nAltCount2;
164 0 : const OUString *pAlt = j == 0 ? pAlt1 : pAlt2;
165 0 : for (i = 0; i < nCount && nIndex < MAX_PROPOSALS; i++)
166 : {
167 0 : if (!pAlt[i].isEmpty() &&
168 0 : (bAllowDuplicates || !SeqHasEntry(aMerged, pAlt[i] )))
169 0 : pMerged[ nIndex++ ] = pAlt[ i ];
170 : }
171 : }
172 0 : aMerged.realloc( nIndex );
173 : }
174 :
175 0 : return aMerged;
176 : }
177 :
178 :
179 :
180 0 : SpellAlternatives::SpellAlternatives()
181 : {
182 0 : nLanguage = LANGUAGE_NONE;
183 0 : nType = SpellFailure::IS_NEGATIVE_WORD;
184 0 : }
185 :
186 :
187 0 : SpellAlternatives::SpellAlternatives(
188 : const OUString &rWord, sal_Int16 nLang, sal_Int16 nFailureType,
189 : const Sequence< OUString > &rAlternatives ) :
190 : aAlt (rAlternatives),
191 : aWord (rWord),
192 : nType (nFailureType),
193 0 : nLanguage (nLang)
194 : {
195 0 : }
196 :
197 :
198 0 : SpellAlternatives::~SpellAlternatives()
199 : {
200 0 : }
201 :
202 :
203 0 : OUString SAL_CALL SpellAlternatives::getWord()
204 : throw(RuntimeException, std::exception)
205 : {
206 0 : MutexGuard aGuard( GetLinguMutex() );
207 0 : return aWord;
208 : }
209 :
210 :
211 0 : Locale SAL_CALL SpellAlternatives::getLocale()
212 : throw(RuntimeException, std::exception)
213 : {
214 0 : MutexGuard aGuard( GetLinguMutex() );
215 0 : return LanguageTag::convertToLocale( nLanguage );
216 : }
217 :
218 :
219 0 : sal_Int16 SAL_CALL SpellAlternatives::getFailureType()
220 : throw(RuntimeException, std::exception)
221 : {
222 0 : MutexGuard aGuard( GetLinguMutex() );
223 0 : return nType;
224 : }
225 :
226 :
227 0 : sal_Int16 SAL_CALL SpellAlternatives::getAlternativesCount()
228 : throw(RuntimeException, std::exception)
229 : {
230 0 : MutexGuard aGuard( GetLinguMutex() );
231 0 : return (sal_Int16) aAlt.getLength();
232 : }
233 :
234 :
235 0 : Sequence< OUString > SAL_CALL SpellAlternatives::getAlternatives()
236 : throw(RuntimeException, std::exception)
237 : {
238 0 : MutexGuard aGuard( GetLinguMutex() );
239 0 : return aAlt;
240 : }
241 :
242 :
243 0 : void SAL_CALL SpellAlternatives::setAlternatives( const uno::Sequence< OUString >& rAlternatives )
244 : throw (uno::RuntimeException, std::exception)
245 : {
246 0 : MutexGuard aGuard( GetLinguMutex() );
247 0 : aAlt = rAlternatives;
248 0 : }
249 :
250 :
251 0 : void SAL_CALL SpellAlternatives::setFailureType( sal_Int16 nFailureType )
252 : throw (uno::RuntimeException, std::exception)
253 : {
254 0 : MutexGuard aGuard( GetLinguMutex() );
255 0 : nType = nFailureType;
256 0 : }
257 :
258 :
259 0 : void SpellAlternatives::SetWordLanguage(const OUString &rWord, sal_Int16 nLang)
260 : {
261 0 : MutexGuard aGuard( GetLinguMutex() );
262 0 : aWord = rWord;
263 0 : nLanguage = nLang;
264 0 : }
265 :
266 :
267 0 : void SpellAlternatives::SetFailureType(sal_Int16 nTypeP)
268 : {
269 0 : MutexGuard aGuard( GetLinguMutex() );
270 0 : nType = nTypeP;
271 0 : }
272 :
273 :
274 0 : void SpellAlternatives::SetAlternatives( const Sequence< OUString > &rAlt )
275 : {
276 0 : MutexGuard aGuard( GetLinguMutex() );
277 0 : aAlt = rAlt;
278 0 : }
279 :
280 :
281 0 : com::sun::star::uno::Reference < com::sun::star::linguistic2::XSpellAlternatives > SpellAlternatives::CreateSpellAlternatives(
282 : const OUString &rWord, sal_Int16 nLang, sal_Int16 nTypeP, const ::com::sun::star::uno::Sequence< OUString > &rAlt )
283 : {
284 0 : SpellAlternatives* pAlt = new SpellAlternatives;
285 0 : pAlt->SetWordLanguage( rWord, nLang );
286 0 : pAlt->SetFailureType( nTypeP );
287 0 : pAlt->SetAlternatives( rAlt );
288 0 : return Reference < XSpellAlternatives >(pAlt);
289 : }
290 :
291 :
292 : } // namespace linguistic
293 :
294 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|