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