1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 #include <linguistic/misc.hxx>
30 #include <linguistic/spelldta.hxx>
34 using namespace com::sun::star
;
35 using namespace com::sun::star::beans
;
36 using namespace com::sun::star::lang
;
37 using namespace com::sun::star::uno
;
38 using namespace com::sun::star::linguistic2
;
44 #define MAX_PROPOSALS 40
46 static bool SeqHasEntry(
47 const std::vector
< OUString
> &rSeq
,
51 sal_Int32 nLen
= rSeq
.size();
52 for (sal_Int32 i
= 0; i
< nLen
&& !bRes
; ++i
)
61 void SearchSimilarText( const OUString
&rText
, LanguageType nLanguage
,
62 Reference
< XSearchableDictionaryList
> const &xDicList
,
63 std::vector
< OUString
> & rDicListProps
)
68 const uno::Sequence
< Reference
< XDictionary
> >
69 aDics( xDicList
->getDictionaries() );
70 const Reference
< XDictionary
>
71 *pDic
= aDics
.getConstArray();
72 sal_Int32 nDics
= xDicList
->getCount();
74 for (sal_Int32 i
= 0; i
< nDics
; i
++)
76 Reference
< XDictionary
> xDic
= pDic
[i
];
78 LanguageType nLang
= LinguLocaleToLanguage( xDic
->getLocale() );
80 if ( xDic
.is() && xDic
->isActive()
81 && (nLang
== nLanguage
|| LinguIsUnspecified( nLang
)) )
83 #if OSL_DEBUG_LEVEL > 0
84 DictionaryType eType
= xDic
->getDictionaryType();
86 assert( eType
!= DictionaryType_MIXED
&& "unexpected dictionary type" );
88 const Sequence
< Reference
< XDictionaryEntry
> > aEntries
= xDic
->getEntries();
89 for (const Reference
<XDictionaryEntry
>& rEntry
: aEntries
)
94 // remove characters used to determine hyphenation positions
95 aEntryTxt
= rEntry
->getDictionaryWord().replaceAll("=", "");
97 if (!aEntryTxt
.isEmpty() && aEntryTxt
.getLength() > 1 && LevDistance( rText
, aEntryTxt
) <= 2)
98 rDicListProps
.push_back( aEntryTxt
);
105 void SeqRemoveNegEntries( std::vector
< OUString
> &rSeq
,
106 Reference
< XSearchableDictionaryList
> const &rxDicList
,
107 LanguageType nLanguage
)
109 bool bSthRemoved
= false;
110 sal_Int32 nLen
= rSeq
.size();
111 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
113 Reference
< XDictionaryEntry
> xNegEntry( SearchDicList( rxDicList
,
114 rSeq
[i
], nLanguage
, false, true ) );
123 std::vector
< OUString
> aNew
;
124 // merge sequence without duplicates and empty strings in new empty sequence
125 rSeq
= MergeProposalSeqs( aNew
, rSeq
);
130 std::vector
< OUString
> MergeProposalSeqs(
131 std::vector
< OUString
> &rAlt1
,
132 std::vector
< OUString
> &rAlt2
)
134 std::vector
< OUString
> aMerged
;
136 size_t nAltCount1
= rAlt1
.size();
137 size_t nAltCount2
= rAlt2
.size();
139 sal_Int32 nCountNew
= std::min
<sal_Int32
>( nAltCount1
+ nAltCount2
, sal_Int32(MAX_PROPOSALS
) );
140 aMerged
.resize( nCountNew
);
142 sal_Int32 nIndex
= 0;
144 for (int j
= 0; j
< 2; j
++)
146 sal_Int32 nCount
= j
== 0 ? nAltCount1
: nAltCount2
;
147 std::vector
< OUString
>& rAlt
= j
== 0 ? rAlt1
: rAlt2
;
148 for (i
= 0; i
< nCount
&& nIndex
< MAX_PROPOSALS
; i
++)
150 if (!rAlt
[i
].isEmpty() &&
151 !SeqHasEntry(aMerged
, rAlt
[i
] ))
152 aMerged
[ nIndex
++ ] = rAlt
[ i
];
155 aMerged
.resize( nIndex
);
161 SpellAlternatives::SpellAlternatives()
163 nLanguage
= LANGUAGE_NONE
;
164 nType
= SpellFailure::IS_NEGATIVE_WORD
;
168 SpellAlternatives::SpellAlternatives(
169 const OUString
&rWord
, LanguageType nLang
,
170 const Sequence
< OUString
> &rAlternatives
) :
171 aAlt (rAlternatives
),
173 nType (SpellFailure::IS_NEGATIVE_WORD
),
179 SpellAlternatives::~SpellAlternatives()
184 OUString SAL_CALL
SpellAlternatives::getWord()
186 MutexGuard
aGuard( GetLinguMutex() );
191 Locale SAL_CALL
SpellAlternatives::getLocale()
193 MutexGuard
aGuard( GetLinguMutex() );
194 return LanguageTag::convertToLocale( nLanguage
);
198 sal_Int16 SAL_CALL
SpellAlternatives::getFailureType()
200 MutexGuard
aGuard( GetLinguMutex() );
205 sal_Int16 SAL_CALL
SpellAlternatives::getAlternativesCount()
207 MutexGuard
aGuard( GetLinguMutex() );
208 return static_cast<sal_Int16
>(aAlt
.getLength());
212 Sequence
< OUString
> SAL_CALL
SpellAlternatives::getAlternatives()
214 MutexGuard
aGuard( GetLinguMutex() );
219 void SAL_CALL
SpellAlternatives::setAlternatives( const uno::Sequence
< OUString
>& rAlternatives
)
221 MutexGuard
aGuard( GetLinguMutex() );
222 aAlt
= rAlternatives
;
226 void SAL_CALL
SpellAlternatives::setFailureType( sal_Int16 nFailureType
)
228 MutexGuard
aGuard( GetLinguMutex() );
229 nType
= nFailureType
;
233 void SpellAlternatives::SetWordLanguage(const OUString
&rWord
, LanguageType nLang
)
235 MutexGuard
aGuard( GetLinguMutex() );
241 void SpellAlternatives::SetFailureType(sal_Int16 nTypeP
)
243 MutexGuard
aGuard( GetLinguMutex() );
248 void SpellAlternatives::SetAlternatives( const Sequence
< OUString
> &rAlt
)
250 MutexGuard
aGuard( GetLinguMutex() );
255 css::uno::Reference
< css::linguistic2::XSpellAlternatives
> SpellAlternatives::CreateSpellAlternatives(
256 const OUString
&rWord
, LanguageType nLang
, sal_Int16 nTypeP
, const css::uno::Sequence
< OUString
> &rAlt
)
258 SpellAlternatives
* pAlt
= new SpellAlternatives
;
259 pAlt
->SetWordLanguage( rWord
, nLang
);
260 pAlt
->SetFailureType( nTypeP
);
261 pAlt
->SetAlternatives( rAlt
);
262 return Reference
< XSpellAlternatives
>(pAlt
);
266 } // namespace linguistic
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */