Files
libreoffice/linguistic/source/spelldta.cxx

367 lines
10 KiB
C++
Raw Normal View History

2000-11-17 11:37:46 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2000-11-17 11:37:46 +00:00
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
2000-11-17 11:37:46 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2000-11-17 11:37:46 +00:00
*
* This file is part of OpenOffice.org.
2000-11-17 11:37:46 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2000-11-17 11:37:46 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2000-11-17 11:37:46 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2000-11-17 11:37:46 +00:00
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_linguistic.hxx"
2000-11-17 11:37:46 +00:00
#include <com/sun/star/uno/Reference.h>
#include <com/sun/star/linguistic2/SpellFailure.hpp>
#include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp>
2000-11-17 11:37:46 +00:00
#include <tools/debug.hxx>
#include <unotools/processfactory.hxx>
#include <osl/mutex.hxx>
#include <vector>
#include "linguistic/spelldta.hxx"
2000-11-17 11:37:46 +00:00
#include "lngsvcmgr.hxx"
using namespace utl;
using namespace osl;
using namespace rtl;
using namespace com::sun::star;
using namespace com::sun::star::beans;
using namespace com::sun::star::lang;
using namespace com::sun::star::uno;
using namespace com::sun::star::linguistic2;
namespace linguistic
{
///////////////////////////////////////////////////////////////////////////
#define MAX_PROPOSALS 40
Reference< XSpellAlternatives > MergeProposals(
Reference< XSpellAlternatives > &rxAlt1,
Reference< XSpellAlternatives > &rxAlt2)
{
Reference< XSpellAlternatives > xMerged;
if (!rxAlt1.is())
xMerged = rxAlt2;
else if (!rxAlt2.is())
xMerged = rxAlt1;
else
{
sal_Int32 nAltCount1 = rxAlt1->getAlternativesCount();
2000-11-17 11:37:46 +00:00
Sequence< OUString > aAlt1( rxAlt1->getAlternatives() );
const OUString *pAlt1 = aAlt1.getConstArray();
sal_Int32 nAltCount2 = rxAlt2->getAlternativesCount();
2000-11-17 11:37:46 +00:00
Sequence< OUString > aAlt2( rxAlt2->getAlternatives() );
const OUString *pAlt2 = aAlt2.getConstArray();
sal_Int32 nCountNew = Min( nAltCount1 + nAltCount2, (sal_Int32) MAX_PROPOSALS );
2000-11-17 11:37:46 +00:00
Sequence< OUString > aAltNew( nCountNew );
OUString *pAltNew = aAltNew.getArray();
sal_Int32 nIndex = 0;
sal_Int32 i = 0;
2000-11-17 11:37:46 +00:00
for (int j = 0; j < 2; j++)
{
sal_Int32 nCount = j == 0 ? nAltCount1 : nAltCount2;
2000-11-17 11:37:46 +00:00
const OUString *pAlt = j == 0 ? pAlt1 : pAlt2;
for (i = 0; i < nCount && nIndex < MAX_PROPOSALS; i++)
{
if (pAlt[i].getLength())
pAltNew[ nIndex++ ] = pAlt[ i ];
}
}
DBG_ASSERT(nIndex == nCountNew, "lng : wrong number of proposals");
SpellAlternatives *pSpellAlt = new SpellAlternatives;
pSpellAlt->SetWordLanguage( rxAlt1->getWord(),
LocaleToLanguage( rxAlt1->getLocale() ) );
pSpellAlt->SetFailureType( rxAlt1->getFailureType() );
pSpellAlt->SetAlternatives( aAltNew );
xMerged = pSpellAlt;
}
return xMerged;
}
sal_Bool SeqHasEntry(
const Sequence< OUString > &rSeq,
const OUString &rTxt)
{
sal_Bool bRes = sal_False;
sal_Int32 nLen = rSeq.getLength();
const OUString *pEntry = rSeq.getConstArray();
for (sal_Int32 i = 0; i < nLen && !bRes; ++i)
{
if (rTxt == pEntry[i])
bRes = sal_True;
}
return bRes;
}
void SearchSimilarText( const OUString &rText, sal_Int16 nLanguage,
Reference< XDictionaryList > &xDicList,
std::vector< OUString > & rDicListProps )
{
if (!xDicList.is())
return;
const uno::Sequence< Reference< XDictionary > >
aDics( xDicList->getDictionaries() );
const Reference< XDictionary >
*pDic = aDics.getConstArray();
sal_Int32 nDics = xDicList->getCount();
for (sal_Int32 i = 0; i < nDics; i++)
{
2008-12-15 12:01:46 +00:00
Reference< XDictionary > xDic( pDic[i], UNO_QUERY );
sal_Int16 nLang = LocaleToLanguage( xDic->getLocale() );
if ( xDic.is() && xDic->isActive()
&& (nLang == nLanguage || nLang == LANGUAGE_NONE) )
{
#if OSL_DEBUG_LEVEL > 1
DictionaryType eType = xDic->getDictionaryType();
CWS-TOOLING: integrate CWS tl66 2009-05-15 12:28:55 +0200 tl r271932 : #i91812# include fixed 2009-05-07 16:52:41 +0200 tl r271680 : #i97200# one more MathML export problem to fix 2009-05-05 08:33:29 +0200 tl r271494 : #i99401# positiv user-dicts vs negativ user-dicts 2009-05-04 14:14:03 +0200 tl r271452 : #i97200# warning free code; MathML 2.0 export 2009-05-04 13:26:30 +0200 tl r271448 : #i97200# write valid MathML 2.0 on export 2009-04-29 14:21:54 +0200 tl r271371 : #i97200# new MathML token 2009-04-29 11:12:07 +0200 tl r271360 : #i97200# inroducing separate files for import and export 2009-04-28 16:47:42 +0200 tl r271331 : #i97200# better MathML pretty printing 2009-04-28 11:21:57 +0200 tl r271315 : #i97200# MathML attributes and default namespace for MathML 2009-04-28 11:21:24 +0200 tl r271314 : #i97200# MathML attributes and default namespace for MathML 2009-04-23 12:44:18 +0200 tl r271154 : #i97200# math.dtd removed 2009-04-23 12:31:56 +0200 tl r271151 : #i97200# MathML: don't use namespace on attributes 2009-04-22 13:21:11 +0200 tl r271099 : warning-free code 2009-04-22 12:20:13 +0200 tl r271092 : #i100757# loop fixed 2009-04-22 11:29:51 +0200 tl r271086 : #97327# adding mongolian fingerprint for language guessing 2009-04-22 11:25:56 +0200 tl r271083 : #97327# adding mongolian fingerprint for language guessing 2009-04-21 10:39:21 +0200 tl r271025 : #99599# code fix for LRE/RLE embedding 2009-04-20 16:36:33 +0200 tl r270992 : #i99604# HasDigits fixed 2009-04-20 14:44:19 +0200 tl r270985 : #i99604# warning-free code for Windows 2009-04-20 13:48:13 +0200 tl r270980 : #i99604# HasDigits fix for non-ASCII characters 2009-04-20 13:47:50 +0200 tl r270979 : #i99604# HasDigits fix for non-ASCII characters 2009-04-20 12:28:15 +0200 tl r270973 : warning-free code after merging 2009-04-20 10:16:19 +0200 tl r270964 : warning-free code after merging 2009-04-17 14:43:36 +0200 tl r270948 : #i96846# 2009-04-16 13:09:15 +0200 tl r270883 : CWS-TOOLING: rebase CWS tl66 to trunk@270723 (milestone: DEV300:m46) 2009-04-14 14:34:08 +0200 tl r270770 : #101067# warning-free code 2009-04-02 09:07:44 +0200 tl r270368 : #i100757# performance patch for start-up (initialize language guessing on demand only) 2009-03-11 10:37:59 +0100 tl r269301 : #i100083# fixed system dictionary lookup 2009-03-06 13:10:23 +0100 tl r268998 : warning-free code for Windows non-pro 2009-02-23 14:01:23 +0100 tl r268355 : #i99401# winning rules for user-dictionaries changed 2009-02-19 14:05:57 +0100 tl r268281 : #i98644# suggestion improvement when first checker does not know any suggestions 2009-02-19 14:05:02 +0100 tl r268280 : #i98644# suggestion improvement when first checker does not know any suggestions 2009-02-19 13:58:51 +0100 tl r268279 : #i98644# suggestion improvement when first checker does not know any suggestions 2009-02-19 11:38:03 +0100 tl r268266 : #i98644# suggestion improvement when first checker does not know any suggestions 2009-02-12 11:58:34 +0100 tl r267642 : #i96846# some properties declared as maybevoid 2009-02-06 12:43:55 +0100 tl r267454 : #i98644# provide sugestions from secondary spell checkers if the primary does not provide ones 2009-02-05 13:02:26 +0100 tl r267418 : #i98880# a bit clean-up in the grammar checking framework 2009-02-04 12:15:37 +0100 tl r267363 : #i91812# remove unused/duplicate code 2009-02-04 12:09:34 +0100 tl r267362 : #i91812# remove unused/duplicate code 2009-02-04 11:07:57 +0100 tl r267355 : #i91812# remove unused code 2009-02-04 11:06:48 +0100 tl r267354 : #i91812# remove unused code 2009-02-03 14:52:43 +0100 tl r267331 : #i91812# remove unused code 2009-02-03 14:26:00 +0100 tl r267324 : #i91198# adding fingerprint for luxembourgish 2009-02-03 14:20:58 +0100 tl r267323 : #i91198# adding fingerprint for luxembourgish 2009-02-03 14:18:33 +0100 tl r267322 : #i91198# adding fingerprint for luxembourgish 2009-02-03 13:56:39 +0100 tl r267319 : #i91812# remove unused code 2009-02-03 12:41:50 +0100 tl r267314 : #i48400# auto-spellcheck improvement when deleting wrong chars 2009-02-03 11:48:51 +0100 tl r267310 : #i91812# remove unused code 2009-02-03 11:14:29 +0100 tl r267307 : warning free code 2009-02-03 10:45:21 +0100 tl r267306 : #i91812# remove unused code 2009-02-03 10:37:04 +0100 tl r267304 : #i33387# name change for 'View/Selection' 2009-02-03 10:36:17 +0100 tl r267303 : #i33387# name change for 'View/Selection' 2009-02-03 10:32:12 +0100 tl r267302 : #i30642# spelling error in context menu fixed 2009-02-03 10:27:34 +0100 tl r267301 : #i92210# remove unused code types.cxx cfgitem.*
2009-05-19 09:31:27 +00:00
(void) eType;
DBG_ASSERT( eType != DictionaryType_MIXED, "unexpected dictionary type" );
#endif
const Sequence< Reference< XDictionaryEntry > > aEntries = xDic->getEntries();
const Reference< XDictionaryEntry > *pEntries = aEntries.getConstArray();
sal_Int32 nLen = aEntries.getLength();
for (sal_Int32 k = 0; k < nLen; ++k)
{
String aEntryTxt;
if (pEntries[k].is())
{
aEntryTxt = pEntries[k]->getDictionaryWord();
// remove characters used to determine hyphenation positions
aEntryTxt.EraseAllChars( '=' );
}
if (aEntryTxt.Len() > 0 && LevDistance( rText, aEntryTxt ) <= 2)
rDicListProps.push_back( aEntryTxt );
}
}
}
}
void SeqRemoveNegEntries( Sequence< OUString > &rSeq,
Reference< XDictionaryList > &rxDicList,
sal_Int16 nLanguage )
{
static const OUString aEmpty;
sal_Bool bSthRemoved = sal_False;
sal_Int32 nLen = rSeq.getLength();
OUString *pEntries = rSeq.getArray();
for (sal_Int32 i = 0; i < nLen; ++i)
{
Reference< XDictionaryEntry > xNegEntry( SearchDicList( rxDicList,
pEntries[i], nLanguage, sal_False, sal_True ) );
if (xNegEntry.is())
{
pEntries[i] = aEmpty;
bSthRemoved = sal_True;
}
}
if (bSthRemoved)
{
Sequence< OUString > aNew;
// merge sequence without duplicates and empty strings in new empty sequence
aNew = MergeProposalSeqs( aNew, rSeq, sal_False );
rSeq = aNew;
}
}
Sequence< OUString > MergeProposalSeqs(
Sequence< OUString > &rAlt1,
Sequence< OUString > &rAlt2,
sal_Bool bAllowDuplicates )
{
Sequence< OUString > aMerged;
if (0 == rAlt1.getLength() && bAllowDuplicates)
aMerged = rAlt2;
else if (0 == rAlt2.getLength() && bAllowDuplicates)
aMerged = rAlt1;
else
{
sal_Int32 nAltCount1 = rAlt1.getLength();
const OUString *pAlt1 = rAlt1.getConstArray();
sal_Int32 nAltCount2 = rAlt2.getLength();
const OUString *pAlt2 = rAlt2.getConstArray();
sal_Int32 nCountNew = Min( nAltCount1 + nAltCount2, (sal_Int32) MAX_PROPOSALS );
aMerged.realloc( nCountNew );
OUString *pMerged = aMerged.getArray();
sal_Int32 nIndex = 0;
sal_Int32 i = 0;
for (int j = 0; j < 2; j++)
{
sal_Int32 nCount = j == 0 ? nAltCount1 : nAltCount2;
const OUString *pAlt = j == 0 ? pAlt1 : pAlt2;
for (i = 0; i < nCount && nIndex < MAX_PROPOSALS; i++)
{
if (pAlt[i].getLength() &&
(bAllowDuplicates || !SeqHasEntry(aMerged, pAlt[i] )))
pMerged[ nIndex++ ] = pAlt[ i ];
}
}
//DBG_ASSERT(nIndex == nCountNew, "wrong number of proposals");
aMerged.realloc( nIndex );
}
return aMerged;
}
2000-11-17 11:37:46 +00:00
///////////////////////////////////////////////////////////////////////////
SpellAlternatives::SpellAlternatives()
{
nLanguage = LANGUAGE_NONE;
nType = SpellFailure::IS_NEGATIVE_WORD;
}
SpellAlternatives::SpellAlternatives(
const OUString &rWord, sal_Int16 nLang,
sal_Int16 nFailureType, const OUString &rRplcWord ) :
aAlt ( Sequence< OUString >(1) ),
2000-11-17 11:37:46 +00:00
aWord (rWord),
nType (nFailureType),
nLanguage (nLang)
2000-11-17 11:37:46 +00:00
{
if (rRplcWord.getLength())
aAlt.getArray()[ 0 ] = rRplcWord;
else
aAlt.realloc( 0 );
}
SpellAlternatives::SpellAlternatives(
const OUString &rWord, sal_Int16 nLang, sal_Int16 nFailureType,
const Sequence< OUString > &rAlternatives ) :
aAlt (rAlternatives),
aWord (rWord),
nType (nFailureType),
nLanguage (nLang)
{
}
2000-11-17 11:37:46 +00:00
SpellAlternatives::~SpellAlternatives()
{
}
OUString SAL_CALL SpellAlternatives::getWord()
throw(RuntimeException)
{
MutexGuard aGuard( GetLinguMutex() );
return aWord;
}
Locale SAL_CALL SpellAlternatives::getLocale()
throw(RuntimeException)
{
MutexGuard aGuard( GetLinguMutex() );
return CreateLocale( nLanguage );
}
sal_Int16 SAL_CALL SpellAlternatives::getFailureType()
throw(RuntimeException)
{
MutexGuard aGuard( GetLinguMutex() );
return nType;
}
sal_Int16 SAL_CALL SpellAlternatives::getAlternativesCount()
throw(RuntimeException)
{
MutexGuard aGuard( GetLinguMutex() );
return (sal_Int16) aAlt.getLength();
2000-11-17 11:37:46 +00:00
}
Sequence< OUString > SAL_CALL SpellAlternatives::getAlternatives()
throw(RuntimeException)
{
MutexGuard aGuard( GetLinguMutex() );
return aAlt;
}
2008-12-15 12:01:46 +00:00
void SAL_CALL SpellAlternatives::setAlternatives( const uno::Sequence< OUString >& rAlternatives )
throw (uno::RuntimeException)
{
MutexGuard aGuard( GetLinguMutex() );
aAlt = rAlternatives;
}
void SAL_CALL SpellAlternatives::setFailureType( sal_Int16 nFailureType )
throw (uno::RuntimeException)
{
MutexGuard aGuard( GetLinguMutex() );
nType = nFailureType;
}
void SpellAlternatives::SetWordLanguage(const OUString &rWord, sal_Int16 nLang)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
aWord = rWord;
nLanguage = nLang;
}
void SpellAlternatives::SetFailureType(sal_Int16 nTypeP)
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
nType = nTypeP;
}
void SpellAlternatives::SetAlternatives( const Sequence< OUString > &rAlt )
{
MutexGuard aGuard( GetLinguMutex() );
aAlt = rAlt;
}
///////////////////////////////////////////////////////////////////////////
} // namespace linguistic