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>
30 #include <linguistic/misc.hxx>
31 #include <linguistic/spelldta.hxx>
32 #include <rtl/ref.hxx>
36 using namespace com::sun::star
;
37 using namespace com::sun::star::beans
;
38 using namespace com::sun::star::lang
;
39 using namespace com::sun::star::uno
;
40 using namespace com::sun::star::linguistic2
;
46 #define MAX_PROPOSALS 40
48 static bool SeqHasEntry(
49 const std::vector
< OUString
> &rSeq
,
50 std::u16string_view rTxt
)
53 sal_Int32 nLen
= rSeq
.size();
54 for (sal_Int32 i
= 0; i
< nLen
&& !bRes
; ++i
)
63 void SearchSimilarText( const OUString
&rText
, LanguageType nLanguage
,
64 Reference
< XSearchableDictionaryList
> const &xDicList
,
65 std::vector
< OUString
> & rDicListProps
)
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
];
80 LanguageType nLang
= LinguLocaleToLanguage( xDic
->getLocale() );
82 if ( xDic
.is() && xDic
->isActive()
83 && (nLang
== nLanguage
|| LinguIsUnspecified( nLang
)) )
85 #if OSL_DEBUG_LEVEL > 0
86 DictionaryType eType
= xDic
->getDictionaryType();
88 assert( eType
!= DictionaryType_MIXED
&& "unexpected dictionary type" );
90 const Sequence
< Reference
< XDictionaryEntry
> > aEntries
= xDic
->getEntries();
91 for (const Reference
<XDictionaryEntry
>& rEntry
: aEntries
)
96 // remove characters used to determine hyphenation positions
97 aEntryTxt
= rEntry
->getDictionaryWord().replaceAll("=", "");
99 if (!aEntryTxt
.isEmpty() && aEntryTxt
.getLength() > 1 && LevDistance( rText
, aEntryTxt
) <= 2)
100 rDicListProps
.push_back( aEntryTxt
);
107 void SeqRemoveNegEntries( std::vector
< OUString
> &rSeq
,
108 Reference
< XSearchableDictionaryList
> const &rxDicList
,
109 LanguageType nLanguage
)
111 bool bSthRemoved
= false;
112 sal_Int32 nLen
= rSeq
.size();
113 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
115 Reference
< XDictionaryEntry
> xNegEntry( SearchDicList( rxDicList
,
116 rSeq
[i
], nLanguage
, false, true ) );
125 std::vector
< OUString
> aNew
;
126 // merge sequence without duplicates and empty strings in new empty sequence
127 rSeq
= MergeProposalSeqs( aNew
, rSeq
);
132 std::vector
< OUString
> MergeProposalSeqs(
133 std::vector
< OUString
> &rAlt1
,
134 std::vector
< OUString
> &rAlt2
)
136 std::vector
< OUString
> aMerged
;
138 size_t nAltCount1
= rAlt1
.size();
139 size_t nAltCount2
= rAlt2
.size();
141 sal_Int32 nCountNew
= std::min
<sal_Int32
>( nAltCount1
+ nAltCount2
, sal_Int32(MAX_PROPOSALS
) );
142 aMerged
.resize( nCountNew
);
144 sal_Int32 nIndex
= 0;
146 for (int j
= 0; j
< 2; j
++)
148 sal_Int32 nCount
= j
== 0 ? nAltCount1
: nAltCount2
;
149 std::vector
< OUString
>& rAlt
= j
== 0 ? rAlt1
: rAlt2
;
150 for (i
= 0; i
< nCount
&& nIndex
< MAX_PROPOSALS
; i
++)
152 if (!rAlt
[i
].isEmpty() &&
153 !SeqHasEntry(aMerged
, rAlt
[i
] ))
154 aMerged
[ nIndex
++ ] = rAlt
[ i
];
157 aMerged
.resize( nIndex
);
163 SpellAlternatives::SpellAlternatives()
165 nLanguage
= LANGUAGE_NONE
;
166 nType
= SpellFailure::IS_NEGATIVE_WORD
;
170 SpellAlternatives::SpellAlternatives(
171 OUString aWord_
, LanguageType nLang
,
172 const Sequence
< OUString
> &rAlternatives
) :
173 aAlt (rAlternatives
),
174 aWord (std::move(aWord_
)),
175 nType (SpellFailure::IS_NEGATIVE_WORD
),
181 SpellAlternatives::~SpellAlternatives()
186 OUString SAL_CALL
SpellAlternatives::getWord()
188 MutexGuard
aGuard( GetLinguMutex() );
193 Locale SAL_CALL
SpellAlternatives::getLocale()
195 MutexGuard
aGuard( GetLinguMutex() );
196 return LanguageTag::convertToLocale( nLanguage
);
200 sal_Int16 SAL_CALL
SpellAlternatives::getFailureType()
202 MutexGuard
aGuard( GetLinguMutex() );
207 sal_Int16 SAL_CALL
SpellAlternatives::getAlternativesCount()
209 MutexGuard
aGuard( GetLinguMutex() );
210 return static_cast<sal_Int16
>(aAlt
.getLength());
214 Sequence
< OUString
> SAL_CALL
SpellAlternatives::getAlternatives()
216 MutexGuard
aGuard( GetLinguMutex() );
221 void SAL_CALL
SpellAlternatives::setAlternatives( const uno::Sequence
< OUString
>& rAlternatives
)
223 MutexGuard
aGuard( GetLinguMutex() );
224 aAlt
= rAlternatives
;
228 void SAL_CALL
SpellAlternatives::setFailureType( sal_Int16 nFailureType
)
230 MutexGuard
aGuard( GetLinguMutex() );
231 nType
= nFailureType
;
235 void SpellAlternatives::SetWordLanguage(const OUString
&rWord
, LanguageType nLang
)
237 MutexGuard
aGuard( GetLinguMutex() );
243 void SpellAlternatives::SetFailureType(sal_Int16 nTypeP
)
245 MutexGuard
aGuard( GetLinguMutex() );
250 void SpellAlternatives::SetAlternatives( const Sequence
< OUString
> &rAlt
)
252 MutexGuard
aGuard( GetLinguMutex() );
257 css::uno::Reference
< css::linguistic2::XSpellAlternatives
> SpellAlternatives::CreateSpellAlternatives(
258 const OUString
&rWord
, LanguageType nLang
, sal_Int16 nTypeP
, const css::uno::Sequence
< OUString
> &rAlt
)
260 rtl::Reference
<SpellAlternatives
> pAlt
= new SpellAlternatives
;
261 pAlt
->SetWordLanguage( rWord
, nLang
);
262 pAlt
->SetFailureType( nTypeP
);
263 pAlt
->SetAlternatives( rAlt
);
268 } // namespace linguistic
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */