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