update emoji autocorrect entries from po-files
[LibreOffice.git] / linguistic / source / spelldta.cxx
blobf1d5d7b000238afbc434a80ccfb8d45b9b537f14
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
27 #include <vector>
29 #include "linguistic/spelldta.hxx"
30 #include "lngsvcmgr.hxx"
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;
42 namespace linguistic
45 #define MAX_PROPOSALS 40
47 bool SeqHasEntry(
48 const Sequence< OUString > &rSeq,
49 const OUString &rTxt)
51 bool bRes = false;
52 sal_Int32 nLen = rSeq.getLength();
53 const OUString *pEntry = rSeq.getConstArray();
54 for (sal_Int32 i = 0; i < nLen && !bRes; ++i)
56 if (rTxt == pEntry[i])
57 bRes = true;
59 return bRes;
63 void SearchSimilarText( const OUString &rText, sal_Int16 nLanguage,
64 Reference< XSearchableDictionaryList > &xDicList,
65 std::vector< OUString > & rDicListProps )
67 if (!xDicList.is())
68 return;
70 const uno::Sequence< Reference< XDictionary > >
71 aDics( xDicList->getDictionaries() );
72 const Reference< XDictionary >
73 *pDic = aDics.getConstArray();
74 sal_Int32 nDics = xDicList->getCount();
76 for (sal_Int32 i = 0; i < nDics; i++)
78 Reference< XDictionary > xDic( pDic[i], UNO_QUERY );
80 sal_Int16 nLang = LinguLocaleToLanguage( xDic->getLocale() );
82 if ( xDic.is() && xDic->isActive()
83 && (nLang == nLanguage || LinguIsUnspecified( nLang)) )
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 const Sequence< Reference< XDictionaryEntry > > aEntries = xDic->getEntries();
91 const Reference< XDictionaryEntry > *pEntries = aEntries.getConstArray();
92 sal_Int32 nLen = aEntries.getLength();
93 for (sal_Int32 k = 0; k < nLen; ++k)
95 OUString aEntryTxt;
96 if (pEntries[k].is())
98 // remove characters used to determine hyphenation positions
99 aEntryTxt = comphelper::string::remove(pEntries[k]->getDictionaryWord(), '=');
101 if (!aEntryTxt.isEmpty() && LevDistance( rText, aEntryTxt ) <= 2)
102 rDicListProps.push_back( aEntryTxt );
109 void SeqRemoveNegEntries( Sequence< OUString > &rSeq,
110 Reference< XSearchableDictionaryList > &rxDicList,
111 sal_Int16 nLanguage )
113 static const OUString aEmpty;
114 bool bSthRemoved = false;
115 sal_Int32 nLen = rSeq.getLength();
116 OUString *pEntries = rSeq.getArray();
117 for (sal_Int32 i = 0; i < nLen; ++i)
119 Reference< XDictionaryEntry > xNegEntry( SearchDicList( rxDicList,
120 pEntries[i], nLanguage, false, true ) );
121 if (xNegEntry.is())
123 pEntries[i] = aEmpty;
124 bSthRemoved = true;
127 if (bSthRemoved)
129 Sequence< OUString > aNew;
130 // merge sequence without duplicates and empty strings in new empty sequence
131 aNew = MergeProposalSeqs( aNew, rSeq, false );
132 rSeq = aNew;
137 Sequence< OUString > MergeProposalSeqs(
138 Sequence< OUString > &rAlt1,
139 Sequence< OUString > &rAlt2,
140 bool bAllowDuplicates )
142 Sequence< OUString > aMerged;
144 if (0 == rAlt1.getLength() && bAllowDuplicates)
145 aMerged = rAlt2;
146 else if (0 == rAlt2.getLength() && bAllowDuplicates)
147 aMerged = rAlt1;
148 else
150 sal_Int32 nAltCount1 = rAlt1.getLength();
151 const OUString *pAlt1 = rAlt1.getConstArray();
152 sal_Int32 nAltCount2 = rAlt2.getLength();
153 const OUString *pAlt2 = rAlt2.getConstArray();
155 sal_Int32 nCountNew = std::min( nAltCount1 + nAltCount2, (sal_Int32) MAX_PROPOSALS );
156 aMerged.realloc( nCountNew );
157 OUString *pMerged = aMerged.getArray();
159 sal_Int32 nIndex = 0;
160 sal_Int32 i = 0;
161 for (int j = 0; j < 2; j++)
163 sal_Int32 nCount = j == 0 ? nAltCount1 : nAltCount2;
164 const OUString *pAlt = j == 0 ? pAlt1 : pAlt2;
165 for (i = 0; i < nCount && nIndex < MAX_PROPOSALS; i++)
167 if (!pAlt[i].isEmpty() &&
168 (bAllowDuplicates || !SeqHasEntry(aMerged, pAlt[i] )))
169 pMerged[ nIndex++ ] = pAlt[ i ];
172 aMerged.realloc( nIndex );
175 return aMerged;
180 SpellAlternatives::SpellAlternatives()
182 nLanguage = LANGUAGE_NONE;
183 nType = SpellFailure::IS_NEGATIVE_WORD;
187 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 nLanguage (nLang)
198 SpellAlternatives::~SpellAlternatives()
203 OUString SAL_CALL SpellAlternatives::getWord()
204 throw(RuntimeException, std::exception)
206 MutexGuard aGuard( GetLinguMutex() );
207 return aWord;
211 Locale SAL_CALL SpellAlternatives::getLocale()
212 throw(RuntimeException, std::exception)
214 MutexGuard aGuard( GetLinguMutex() );
215 return LanguageTag::convertToLocale( nLanguage );
219 sal_Int16 SAL_CALL SpellAlternatives::getFailureType()
220 throw(RuntimeException, std::exception)
222 MutexGuard aGuard( GetLinguMutex() );
223 return nType;
227 sal_Int16 SAL_CALL SpellAlternatives::getAlternativesCount()
228 throw(RuntimeException, std::exception)
230 MutexGuard aGuard( GetLinguMutex() );
231 return (sal_Int16) aAlt.getLength();
235 Sequence< OUString > SAL_CALL SpellAlternatives::getAlternatives()
236 throw(RuntimeException, std::exception)
238 MutexGuard aGuard( GetLinguMutex() );
239 return aAlt;
243 void SAL_CALL SpellAlternatives::setAlternatives( const uno::Sequence< OUString >& rAlternatives )
244 throw (uno::RuntimeException, std::exception)
246 MutexGuard aGuard( GetLinguMutex() );
247 aAlt = rAlternatives;
251 void SAL_CALL SpellAlternatives::setFailureType( sal_Int16 nFailureType )
252 throw (uno::RuntimeException, std::exception)
254 MutexGuard aGuard( GetLinguMutex() );
255 nType = nFailureType;
259 void SpellAlternatives::SetWordLanguage(const OUString &rWord, sal_Int16 nLang)
261 MutexGuard aGuard( GetLinguMutex() );
262 aWord = rWord;
263 nLanguage = nLang;
267 void SpellAlternatives::SetFailureType(sal_Int16 nTypeP)
269 MutexGuard aGuard( GetLinguMutex() );
270 nType = nTypeP;
274 void SpellAlternatives::SetAlternatives( const Sequence< OUString > &rAlt )
276 MutexGuard aGuard( GetLinguMutex() );
277 aAlt = rAlt;
281 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 )
284 SpellAlternatives* pAlt = new SpellAlternatives;
285 pAlt->SetWordLanguage( rWord, nLang );
286 pAlt->SetFailureType( nTypeP );
287 pAlt->SetAlternatives( rAlt );
288 return Reference < XSpellAlternatives >(pAlt);
292 } // namespace linguistic
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */