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 <comphelper/string.hxx>
24 #include <tools/debug.hxx>
25 #include <osl/mutex.hxx>
29 #include "linguistic/spelldta.hxx"
30 #include "lngsvcmgr.hxx"
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
;
45 #define MAX_PROPOSALS 40
48 const Sequence
< OUString
> &rSeq
,
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
])
63 void SearchSimilarText( const OUString
&rText
, sal_Int16 nLanguage
,
64 Reference
< XSearchableDictionaryList
> &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
], 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();
88 DBG_ASSERT( eType
!= DictionaryType_MIXED
, "unexpected dictionary type" );
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
)
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 ) );
123 pEntries
[i
] = aEmpty
;
129 Sequence
< OUString
> aNew
;
130 // merge sequence without duplicates and empty strings in new empty sequence
131 aNew
= MergeProposalSeqs( aNew
, rSeq
, false );
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
)
146 else if (0 == rAlt2
.getLength() && bAllowDuplicates
)
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;
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
);
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
),
192 nType (nFailureType
),
198 SpellAlternatives::~SpellAlternatives()
203 OUString SAL_CALL
SpellAlternatives::getWord()
204 throw(RuntimeException
, std::exception
)
206 MutexGuard
aGuard( GetLinguMutex() );
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() );
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() );
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() );
267 void SpellAlternatives::SetFailureType(sal_Int16 nTypeP
)
269 MutexGuard
aGuard( GetLinguMutex() );
274 void SpellAlternatives::SetAlternatives( const Sequence
< OUString
> &rAlt
)
276 MutexGuard
aGuard( GetLinguMutex() );
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: */