1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_linguistic.hxx" 30*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h> 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir #include <com/sun/star/linguistic2/SpellFailure.hpp> 33*cdf0e10cSrcweir #include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp> 34*cdf0e10cSrcweir #include <tools/debug.hxx> 35*cdf0e10cSrcweir #include <unotools/processfactory.hxx> 36*cdf0e10cSrcweir #include <osl/mutex.hxx> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include <vector> 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include "linguistic/spelldta.hxx" 41*cdf0e10cSrcweir #include "lngsvcmgr.hxx" 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir using namespace utl; 45*cdf0e10cSrcweir using namespace osl; 46*cdf0e10cSrcweir using namespace rtl; 47*cdf0e10cSrcweir using namespace com::sun::star; 48*cdf0e10cSrcweir using namespace com::sun::star::beans; 49*cdf0e10cSrcweir using namespace com::sun::star::lang; 50*cdf0e10cSrcweir using namespace com::sun::star::uno; 51*cdf0e10cSrcweir using namespace com::sun::star::linguistic2; 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir namespace linguistic 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////// 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir #define MAX_PROPOSALS 40 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir Reference< XSpellAlternatives > MergeProposals( 62*cdf0e10cSrcweir Reference< XSpellAlternatives > &rxAlt1, 63*cdf0e10cSrcweir Reference< XSpellAlternatives > &rxAlt2) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir Reference< XSpellAlternatives > xMerged; 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir if (!rxAlt1.is()) 68*cdf0e10cSrcweir xMerged = rxAlt2; 69*cdf0e10cSrcweir else if (!rxAlt2.is()) 70*cdf0e10cSrcweir xMerged = rxAlt1; 71*cdf0e10cSrcweir else 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir sal_Int32 nAltCount1 = rxAlt1->getAlternativesCount(); 74*cdf0e10cSrcweir Sequence< OUString > aAlt1( rxAlt1->getAlternatives() ); 75*cdf0e10cSrcweir const OUString *pAlt1 = aAlt1.getConstArray(); 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir sal_Int32 nAltCount2 = rxAlt2->getAlternativesCount(); 78*cdf0e10cSrcweir Sequence< OUString > aAlt2( rxAlt2->getAlternatives() ); 79*cdf0e10cSrcweir const OUString *pAlt2 = aAlt2.getConstArray(); 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir sal_Int32 nCountNew = Min( nAltCount1 + nAltCount2, (sal_Int32) MAX_PROPOSALS ); 82*cdf0e10cSrcweir Sequence< OUString > aAltNew( nCountNew ); 83*cdf0e10cSrcweir OUString *pAltNew = aAltNew.getArray(); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir sal_Int32 nIndex = 0; 86*cdf0e10cSrcweir sal_Int32 i = 0; 87*cdf0e10cSrcweir for (int j = 0; j < 2; j++) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir sal_Int32 nCount = j == 0 ? nAltCount1 : nAltCount2; 90*cdf0e10cSrcweir const OUString *pAlt = j == 0 ? pAlt1 : pAlt2; 91*cdf0e10cSrcweir for (i = 0; i < nCount && nIndex < MAX_PROPOSALS; i++) 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir if (pAlt[i].getLength()) 94*cdf0e10cSrcweir pAltNew[ nIndex++ ] = pAlt[ i ]; 95*cdf0e10cSrcweir } 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir DBG_ASSERT(nIndex == nCountNew, "lng : wrong number of proposals"); 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir SpellAlternatives *pSpellAlt = new SpellAlternatives; 100*cdf0e10cSrcweir pSpellAlt->SetWordLanguage( rxAlt1->getWord(), 101*cdf0e10cSrcweir LocaleToLanguage( rxAlt1->getLocale() ) ); 102*cdf0e10cSrcweir pSpellAlt->SetFailureType( rxAlt1->getFailureType() ); 103*cdf0e10cSrcweir pSpellAlt->SetAlternatives( aAltNew ); 104*cdf0e10cSrcweir xMerged = pSpellAlt; 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir return xMerged; 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir sal_Bool SeqHasEntry( 112*cdf0e10cSrcweir const Sequence< OUString > &rSeq, 113*cdf0e10cSrcweir const OUString &rTxt) 114*cdf0e10cSrcweir { 115*cdf0e10cSrcweir sal_Bool bRes = sal_False; 116*cdf0e10cSrcweir sal_Int32 nLen = rSeq.getLength(); 117*cdf0e10cSrcweir const OUString *pEntry = rSeq.getConstArray(); 118*cdf0e10cSrcweir for (sal_Int32 i = 0; i < nLen && !bRes; ++i) 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir if (rTxt == pEntry[i]) 121*cdf0e10cSrcweir bRes = sal_True; 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir return bRes; 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir void SearchSimilarText( const OUString &rText, sal_Int16 nLanguage, 128*cdf0e10cSrcweir Reference< XDictionaryList > &xDicList, 129*cdf0e10cSrcweir std::vector< OUString > & rDicListProps ) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir if (!xDicList.is()) 132*cdf0e10cSrcweir return; 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir const uno::Sequence< Reference< XDictionary > > 135*cdf0e10cSrcweir aDics( xDicList->getDictionaries() ); 136*cdf0e10cSrcweir const Reference< XDictionary > 137*cdf0e10cSrcweir *pDic = aDics.getConstArray(); 138*cdf0e10cSrcweir sal_Int32 nDics = xDicList->getCount(); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir for (sal_Int32 i = 0; i < nDics; i++) 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir Reference< XDictionary > xDic( pDic[i], UNO_QUERY ); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir sal_Int16 nLang = LocaleToLanguage( xDic->getLocale() ); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir if ( xDic.is() && xDic->isActive() 147*cdf0e10cSrcweir && (nLang == nLanguage || nLang == LANGUAGE_NONE) ) 148*cdf0e10cSrcweir { 149*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 150*cdf0e10cSrcweir DictionaryType eType = xDic->getDictionaryType(); 151*cdf0e10cSrcweir (void) eType; 152*cdf0e10cSrcweir DBG_ASSERT( eType != DictionaryType_MIXED, "unexpected dictionary type" ); 153*cdf0e10cSrcweir #endif 154*cdf0e10cSrcweir const Sequence< Reference< XDictionaryEntry > > aEntries = xDic->getEntries(); 155*cdf0e10cSrcweir const Reference< XDictionaryEntry > *pEntries = aEntries.getConstArray(); 156*cdf0e10cSrcweir sal_Int32 nLen = aEntries.getLength(); 157*cdf0e10cSrcweir for (sal_Int32 k = 0; k < nLen; ++k) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir String aEntryTxt; 160*cdf0e10cSrcweir if (pEntries[k].is()) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir aEntryTxt = pEntries[k]->getDictionaryWord(); 163*cdf0e10cSrcweir // remove characters used to determine hyphenation positions 164*cdf0e10cSrcweir aEntryTxt.EraseAllChars( '=' ); 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir if (aEntryTxt.Len() > 0 && LevDistance( rText, aEntryTxt ) <= 2) 167*cdf0e10cSrcweir rDicListProps.push_back( aEntryTxt ); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir } 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir void SeqRemoveNegEntries( Sequence< OUString > &rSeq, 175*cdf0e10cSrcweir Reference< XDictionaryList > &rxDicList, 176*cdf0e10cSrcweir sal_Int16 nLanguage ) 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir static const OUString aEmpty; 179*cdf0e10cSrcweir sal_Bool bSthRemoved = sal_False; 180*cdf0e10cSrcweir sal_Int32 nLen = rSeq.getLength(); 181*cdf0e10cSrcweir OUString *pEntries = rSeq.getArray(); 182*cdf0e10cSrcweir for (sal_Int32 i = 0; i < nLen; ++i) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir Reference< XDictionaryEntry > xNegEntry( SearchDicList( rxDicList, 185*cdf0e10cSrcweir pEntries[i], nLanguage, sal_False, sal_True ) ); 186*cdf0e10cSrcweir if (xNegEntry.is()) 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir pEntries[i] = aEmpty; 189*cdf0e10cSrcweir bSthRemoved = sal_True; 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir if (bSthRemoved) 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir Sequence< OUString > aNew; 195*cdf0e10cSrcweir // merge sequence without duplicates and empty strings in new empty sequence 196*cdf0e10cSrcweir aNew = MergeProposalSeqs( aNew, rSeq, sal_False ); 197*cdf0e10cSrcweir rSeq = aNew; 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir Sequence< OUString > MergeProposalSeqs( 203*cdf0e10cSrcweir Sequence< OUString > &rAlt1, 204*cdf0e10cSrcweir Sequence< OUString > &rAlt2, 205*cdf0e10cSrcweir sal_Bool bAllowDuplicates ) 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir Sequence< OUString > aMerged; 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir if (0 == rAlt1.getLength() && bAllowDuplicates) 210*cdf0e10cSrcweir aMerged = rAlt2; 211*cdf0e10cSrcweir else if (0 == rAlt2.getLength() && bAllowDuplicates) 212*cdf0e10cSrcweir aMerged = rAlt1; 213*cdf0e10cSrcweir else 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir sal_Int32 nAltCount1 = rAlt1.getLength(); 216*cdf0e10cSrcweir const OUString *pAlt1 = rAlt1.getConstArray(); 217*cdf0e10cSrcweir sal_Int32 nAltCount2 = rAlt2.getLength(); 218*cdf0e10cSrcweir const OUString *pAlt2 = rAlt2.getConstArray(); 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir sal_Int32 nCountNew = Min( nAltCount1 + nAltCount2, (sal_Int32) MAX_PROPOSALS ); 221*cdf0e10cSrcweir aMerged.realloc( nCountNew ); 222*cdf0e10cSrcweir OUString *pMerged = aMerged.getArray(); 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir sal_Int32 nIndex = 0; 225*cdf0e10cSrcweir sal_Int32 i = 0; 226*cdf0e10cSrcweir for (int j = 0; j < 2; j++) 227*cdf0e10cSrcweir { 228*cdf0e10cSrcweir sal_Int32 nCount = j == 0 ? nAltCount1 : nAltCount2; 229*cdf0e10cSrcweir const OUString *pAlt = j == 0 ? pAlt1 : pAlt2; 230*cdf0e10cSrcweir for (i = 0; i < nCount && nIndex < MAX_PROPOSALS; i++) 231*cdf0e10cSrcweir { 232*cdf0e10cSrcweir if (pAlt[i].getLength() && 233*cdf0e10cSrcweir (bAllowDuplicates || !SeqHasEntry(aMerged, pAlt[i] ))) 234*cdf0e10cSrcweir pMerged[ nIndex++ ] = pAlt[ i ]; 235*cdf0e10cSrcweir } 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir //DBG_ASSERT(nIndex == nCountNew, "wrong number of proposals"); 238*cdf0e10cSrcweir aMerged.realloc( nIndex ); 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir return aMerged; 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////// 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir SpellAlternatives::SpellAlternatives() 248*cdf0e10cSrcweir { 249*cdf0e10cSrcweir nLanguage = LANGUAGE_NONE; 250*cdf0e10cSrcweir nType = SpellFailure::IS_NEGATIVE_WORD; 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir SpellAlternatives::SpellAlternatives( 255*cdf0e10cSrcweir const OUString &rWord, sal_Int16 nLang, 256*cdf0e10cSrcweir sal_Int16 nFailureType, const OUString &rRplcWord ) : 257*cdf0e10cSrcweir aAlt ( Sequence< OUString >(1) ), 258*cdf0e10cSrcweir aWord (rWord), 259*cdf0e10cSrcweir nType (nFailureType), 260*cdf0e10cSrcweir nLanguage (nLang) 261*cdf0e10cSrcweir { 262*cdf0e10cSrcweir if (rRplcWord.getLength()) 263*cdf0e10cSrcweir aAlt.getArray()[ 0 ] = rRplcWord; 264*cdf0e10cSrcweir else 265*cdf0e10cSrcweir aAlt.realloc( 0 ); 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir SpellAlternatives::SpellAlternatives( 270*cdf0e10cSrcweir const OUString &rWord, sal_Int16 nLang, sal_Int16 nFailureType, 271*cdf0e10cSrcweir const Sequence< OUString > &rAlternatives ) : 272*cdf0e10cSrcweir aAlt (rAlternatives), 273*cdf0e10cSrcweir aWord (rWord), 274*cdf0e10cSrcweir nType (nFailureType), 275*cdf0e10cSrcweir nLanguage (nLang) 276*cdf0e10cSrcweir { 277*cdf0e10cSrcweir } 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir 280*cdf0e10cSrcweir SpellAlternatives::~SpellAlternatives() 281*cdf0e10cSrcweir { 282*cdf0e10cSrcweir } 283*cdf0e10cSrcweir 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir OUString SAL_CALL SpellAlternatives::getWord() 286*cdf0e10cSrcweir throw(RuntimeException) 287*cdf0e10cSrcweir { 288*cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() ); 289*cdf0e10cSrcweir return aWord; 290*cdf0e10cSrcweir } 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir Locale SAL_CALL SpellAlternatives::getLocale() 294*cdf0e10cSrcweir throw(RuntimeException) 295*cdf0e10cSrcweir { 296*cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() ); 297*cdf0e10cSrcweir return CreateLocale( nLanguage ); 298*cdf0e10cSrcweir } 299*cdf0e10cSrcweir 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir sal_Int16 SAL_CALL SpellAlternatives::getFailureType() 302*cdf0e10cSrcweir throw(RuntimeException) 303*cdf0e10cSrcweir { 304*cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() ); 305*cdf0e10cSrcweir return nType; 306*cdf0e10cSrcweir } 307*cdf0e10cSrcweir 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir sal_Int16 SAL_CALL SpellAlternatives::getAlternativesCount() 310*cdf0e10cSrcweir throw(RuntimeException) 311*cdf0e10cSrcweir { 312*cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() ); 313*cdf0e10cSrcweir return (sal_Int16) aAlt.getLength(); 314*cdf0e10cSrcweir } 315*cdf0e10cSrcweir 316*cdf0e10cSrcweir 317*cdf0e10cSrcweir Sequence< OUString > SAL_CALL SpellAlternatives::getAlternatives() 318*cdf0e10cSrcweir throw(RuntimeException) 319*cdf0e10cSrcweir { 320*cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() ); 321*cdf0e10cSrcweir return aAlt; 322*cdf0e10cSrcweir } 323*cdf0e10cSrcweir 324*cdf0e10cSrcweir 325*cdf0e10cSrcweir void SAL_CALL SpellAlternatives::setAlternatives( const uno::Sequence< OUString >& rAlternatives ) 326*cdf0e10cSrcweir throw (uno::RuntimeException) 327*cdf0e10cSrcweir { 328*cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() ); 329*cdf0e10cSrcweir aAlt = rAlternatives; 330*cdf0e10cSrcweir } 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir void SAL_CALL SpellAlternatives::setFailureType( sal_Int16 nFailureType ) 334*cdf0e10cSrcweir throw (uno::RuntimeException) 335*cdf0e10cSrcweir { 336*cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() ); 337*cdf0e10cSrcweir nType = nFailureType; 338*cdf0e10cSrcweir } 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir 341*cdf0e10cSrcweir void SpellAlternatives::SetWordLanguage(const OUString &rWord, sal_Int16 nLang) 342*cdf0e10cSrcweir { 343*cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() ); 344*cdf0e10cSrcweir aWord = rWord; 345*cdf0e10cSrcweir nLanguage = nLang; 346*cdf0e10cSrcweir } 347*cdf0e10cSrcweir 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir void SpellAlternatives::SetFailureType(sal_Int16 nTypeP) 350*cdf0e10cSrcweir { 351*cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() ); 352*cdf0e10cSrcweir nType = nTypeP; 353*cdf0e10cSrcweir } 354*cdf0e10cSrcweir 355*cdf0e10cSrcweir 356*cdf0e10cSrcweir void SpellAlternatives::SetAlternatives( const Sequence< OUString > &rAlt ) 357*cdf0e10cSrcweir { 358*cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() ); 359*cdf0e10cSrcweir aAlt = rAlt; 360*cdf0e10cSrcweir } 361*cdf0e10cSrcweir 362*cdf0e10cSrcweir 363*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////// 364*cdf0e10cSrcweir 365*cdf0e10cSrcweir } // namespace linguistic 366*cdf0e10cSrcweir 367