Files
libreoffice/linguistic/source/spelldta.cxx

281 lines
7.7 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
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>
2011-11-18 21:03:31 +00:00
#include <comphelper/string.hxx>
2000-11-17 11:37:46 +00:00
#include <osl/mutex.hxx>
#include <algorithm>
#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 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;
2011-02-26 15:54:59 +01:00
2000-11-17 11:37:46 +00:00
namespace linguistic
{
#define MAX_PROPOSALS 40
bool SeqHasEntry(
const std::vector< OUString > &rSeq,
const OUString &rTxt)
{
bool bRes = false;
sal_Int32 nLen = rSeq.size();
for (sal_Int32 i = 0; i < nLen && !bRes; ++i)
{
if (rTxt == rSeq[i])
bRes = true;
}
return bRes;
}
void SearchSimilarText( const OUString &rText, LanguageType nLanguage,
Reference< XSearchableDictionaryList > const &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 );
LanguageType nLang = LinguLocaleToLanguage( xDic->getLocale() );
if ( xDic.is() && xDic->isActive()
&& (nLang == nLanguage || LinguIsUnspecified( nLang)) )
{
#if OSL_DEBUG_LEVEL > 0
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;
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)
{
OUString aEntryTxt;
if (pEntries[k].is())
{
// remove characters used to determine hyphenation positions
aEntryTxt = pEntries[k]->getDictionaryWord().replaceAll("=", "");
}
if (!aEntryTxt.isEmpty() && aEntryTxt.getLength() > 1 && LevDistance( rText, aEntryTxt ) <= 2)
rDicListProps.push_back( aEntryTxt );
}
}
}
}
void SeqRemoveNegEntries( std::vector< OUString > &rSeq,
Reference< XSearchableDictionaryList > const &rxDicList,
LanguageType nLanguage )
{
bool bSthRemoved = false;
sal_Int32 nLen = rSeq.size();
for (sal_Int32 i = 0; i < nLen; ++i)
{
Reference< XDictionaryEntry > xNegEntry( SearchDicList( rxDicList,
rSeq[i], nLanguage, false, true ) );
if (xNegEntry.is())
{
rSeq[i].clear();
bSthRemoved = true;
}
}
if (bSthRemoved)
{
std::vector< OUString > aNew;
// merge sequence without duplicates and empty strings in new empty sequence
aNew = MergeProposalSeqs( aNew, rSeq, false );
rSeq = aNew;
}
}
std::vector< OUString > MergeProposalSeqs(
std::vector< OUString > &rAlt1,
std::vector< OUString > &rAlt2,
bool bAllowDuplicates )
{
std::vector< OUString > aMerged;
if (rAlt1.empty() && bAllowDuplicates)
aMerged = rAlt2;
else if (rAlt2.empty() && bAllowDuplicates)
aMerged = rAlt1;
else
{
size_t nAltCount1 = rAlt1.size();
size_t nAltCount2 = rAlt2.size();
sal_Int32 nCountNew = std::min<sal_Int32>( nAltCount1 + nAltCount2, (sal_Int32) MAX_PROPOSALS );
aMerged.resize( nCountNew );
sal_Int32 nIndex = 0;
sal_Int32 i = 0;
for (int j = 0; j < 2; j++)
{
sal_Int32 nCount = j == 0 ? nAltCount1 : nAltCount2;
std::vector< OUString >& rAlt = j == 0 ? rAlt1 : rAlt2;
for (i = 0; i < nCount && nIndex < MAX_PROPOSALS; i++)
{
if (!rAlt[i].isEmpty() &&
(bAllowDuplicates || !SeqHasEntry(aMerged, rAlt[i] )))
aMerged[ nIndex++ ] = rAlt[ i ];
}
}
aMerged.resize( 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, LanguageType nLang,
const Sequence< OUString > &rAlternatives ) :
aAlt (rAlternatives),
aWord (rWord),
nType (SpellFailure::IS_NEGATIVE_WORD),
nLanguage (nLang)
{
}
2000-11-17 11:37:46 +00:00
SpellAlternatives::~SpellAlternatives()
{
}
OUString SAL_CALL SpellAlternatives::getWord()
{
MutexGuard aGuard( GetLinguMutex() );
return aWord;
}
Locale SAL_CALL SpellAlternatives::getLocale()
{
MutexGuard aGuard( GetLinguMutex() );
return LanguageTag::convertToLocale( nLanguage );
2000-11-17 11:37:46 +00:00
}
sal_Int16 SAL_CALL SpellAlternatives::getFailureType()
{
MutexGuard aGuard( GetLinguMutex() );
return nType;
}
sal_Int16 SAL_CALL SpellAlternatives::getAlternativesCount()
{
MutexGuard aGuard( GetLinguMutex() );
return (sal_Int16) aAlt.getLength();
2000-11-17 11:37:46 +00:00
}
Sequence< OUString > SAL_CALL SpellAlternatives::getAlternatives()
{
MutexGuard aGuard( GetLinguMutex() );
return aAlt;
}
2008-12-15 12:01:46 +00:00
void SAL_CALL SpellAlternatives::setAlternatives( const uno::Sequence< OUString >& rAlternatives )
{
MutexGuard aGuard( GetLinguMutex() );
aAlt = rAlternatives;
}
void SAL_CALL SpellAlternatives::setFailureType( sal_Int16 nFailureType )
{
MutexGuard aGuard( GetLinguMutex() );
nType = nFailureType;
}
void SpellAlternatives::SetWordLanguage(const OUString &rWord, LanguageType 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;
}
css::uno::Reference < css::linguistic2::XSpellAlternatives > SpellAlternatives::CreateSpellAlternatives(
const OUString &rWord, LanguageType nLang, sal_Int16 nTypeP, const css::uno::Sequence< OUString > &rAlt )
{
SpellAlternatives* pAlt = new SpellAlternatives;
pAlt->SetWordLanguage( rWord, nLang );
pAlt->SetFailureType( nTypeP );
pAlt->SetAlternatives( rAlt );
return Reference < XSpellAlternatives >(pAlt);
}
2000-11-17 11:37:46 +00:00
} // namespace linguistic
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */