bump product version to 7.2.5.1
[LibreOffice.git] / linguistic / source / spelldta.cxx
blobb3f57e9b870fe23ab4992afc8665f800873982c0
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 <osl/mutex.hxx>
24 #include <i18nlangtag/languagetag.hxx>
26 #include <algorithm>
27 #include <vector>
29 #include <linguistic/misc.hxx>
30 #include <linguistic/spelldta.hxx>
31 #include <rtl/ref.hxx>
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 static bool SeqHasEntry(
48 const std::vector< OUString > &rSeq,
49 std::u16string_view rTxt)
51 bool bRes = false;
52 sal_Int32 nLen = rSeq.size();
53 for (sal_Int32 i = 0; i < nLen && !bRes; ++i)
55 if (rTxt == rSeq[i])
56 bRes = true;
58 return bRes;
62 void SearchSimilarText( const OUString &rText, LanguageType nLanguage,
63 Reference< XSearchableDictionaryList > const &xDicList,
64 std::vector< OUString > & rDicListProps )
66 if (!xDicList.is())
67 return;
69 const uno::Sequence< Reference< XDictionary > >
70 aDics( xDicList->getDictionaries() );
71 const Reference< XDictionary >
72 *pDic = aDics.getConstArray();
73 sal_Int32 nDics = xDicList->getCount();
75 for (sal_Int32 i = 0; i < nDics; i++)
77 Reference< XDictionary > xDic = pDic[i];
79 LanguageType nLang = LinguLocaleToLanguage( xDic->getLocale() );
81 if ( xDic.is() && xDic->isActive()
82 && (nLang == nLanguage || LinguIsUnspecified( nLang)) )
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)
92 OUString aEntryTxt;
93 if (rEntry.is())
95 // remove characters used to determine hyphenation positions
96 aEntryTxt = rEntry->getDictionaryWord().replaceAll("=", "");
98 if (!aEntryTxt.isEmpty() && aEntryTxt.getLength() > 1 && LevDistance( rText, aEntryTxt ) <= 2)
99 rDicListProps.push_back( aEntryTxt );
106 void SeqRemoveNegEntries( std::vector< OUString > &rSeq,
107 Reference< XSearchableDictionaryList > const &rxDicList,
108 LanguageType nLanguage )
110 bool bSthRemoved = false;
111 sal_Int32 nLen = rSeq.size();
112 for (sal_Int32 i = 0; i < nLen; ++i)
114 Reference< XDictionaryEntry > xNegEntry( SearchDicList( rxDicList,
115 rSeq[i], nLanguage, false, true ) );
116 if (xNegEntry.is())
118 rSeq[i].clear();
119 bSthRemoved = true;
122 if (bSthRemoved)
124 std::vector< OUString > aNew;
125 // merge sequence without duplicates and empty strings in new empty sequence
126 rSeq = MergeProposalSeqs( aNew, rSeq );
131 std::vector< OUString > MergeProposalSeqs(
132 std::vector< OUString > &rAlt1,
133 std::vector< OUString > &rAlt2 )
135 std::vector< OUString > aMerged;
137 size_t nAltCount1 = rAlt1.size();
138 size_t nAltCount2 = rAlt2.size();
140 sal_Int32 nCountNew = std::min<sal_Int32>( nAltCount1 + nAltCount2, sal_Int32(MAX_PROPOSALS) );
141 aMerged.resize( nCountNew );
143 sal_Int32 nIndex = 0;
144 sal_Int32 i = 0;
145 for (int j = 0; j < 2; j++)
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++)
151 if (!rAlt[i].isEmpty() &&
152 !SeqHasEntry(aMerged, rAlt[i] ))
153 aMerged[ nIndex++ ] = rAlt[ i ];
156 aMerged.resize( nIndex );
158 return aMerged;
162 SpellAlternatives::SpellAlternatives()
164 nLanguage = LANGUAGE_NONE;
165 nType = SpellFailure::IS_NEGATIVE_WORD;
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)
180 SpellAlternatives::~SpellAlternatives()
185 OUString SAL_CALL SpellAlternatives::getWord()
187 MutexGuard aGuard( GetLinguMutex() );
188 return aWord;
192 Locale SAL_CALL SpellAlternatives::getLocale()
194 MutexGuard aGuard( GetLinguMutex() );
195 return LanguageTag::convertToLocale( nLanguage );
199 sal_Int16 SAL_CALL SpellAlternatives::getFailureType()
201 MutexGuard aGuard( GetLinguMutex() );
202 return nType;
206 sal_Int16 SAL_CALL SpellAlternatives::getAlternativesCount()
208 MutexGuard aGuard( GetLinguMutex() );
209 return static_cast<sal_Int16>(aAlt.getLength());
213 Sequence< OUString > SAL_CALL SpellAlternatives::getAlternatives()
215 MutexGuard aGuard( GetLinguMutex() );
216 return aAlt;
220 void SAL_CALL SpellAlternatives::setAlternatives( const uno::Sequence< OUString >& rAlternatives )
222 MutexGuard aGuard( GetLinguMutex() );
223 aAlt = rAlternatives;
227 void SAL_CALL SpellAlternatives::setFailureType( sal_Int16 nFailureType )
229 MutexGuard aGuard( GetLinguMutex() );
230 nType = nFailureType;
234 void SpellAlternatives::SetWordLanguage(const OUString &rWord, LanguageType nLang)
236 MutexGuard aGuard( GetLinguMutex() );
237 aWord = rWord;
238 nLanguage = nLang;
242 void SpellAlternatives::SetFailureType(sal_Int16 nTypeP)
244 MutexGuard aGuard( GetLinguMutex() );
245 nType = nTypeP;
249 void SpellAlternatives::SetAlternatives( const Sequence< OUString > &rAlt )
251 MutexGuard aGuard( GetLinguMutex() );
252 aAlt = 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 )
259 rtl::Reference<SpellAlternatives> pAlt = new SpellAlternatives;
260 pAlt->SetWordLanguage( rWord, nLang );
261 pAlt->SetFailureType( nTypeP );
262 pAlt->SetAlternatives( rAlt );
263 return pAlt;
267 } // namespace linguistic
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */