tdf#154499 sw spell checking: add 2-word phrase checking
Hunspell dictionaries can contain phrases, i.e. space separated word sequences, which were used only to reject compounds and to give better suggestions. Now recognize 2-word phrases in the text, no need to break the phrase into single words, e.g. "et" and "cetera", which resulted acceptance of typos (e.g. "et" without "cetera"), also bad suggestions (e.g. "et" and "cetera" independently from the context). More example: == old .dic file == ... et cetera von Neumann veni vidi vici ... List the 2-word phrases, and break the 3 or more word into 2-word phrases: == new .dic file == ... et cetera von Neumann veni vidi vidi vici ... Note: words of the phrase are separated by a space, but recognized also with punctuation, e.g. in the previous example, "Veni, vidi, vici." Note: during typing, the second word of the phrase will be accepted only at ending the paragraph, i.e. pressing Enter. Change-Id: I4a1487abc0e4ab31d09750ee8c817353e6325ca3 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151487 Tested-by: László Németh <nemeth@numbertext.org> Reviewed-by: László Németh <nemeth@numbertext.org>
This commit is contained in:
@@ -33,6 +33,7 @@ class SwScanner
|
|||||||
{
|
{
|
||||||
std::function<LanguageType (sal_Int32, sal_Int32, bool)> m_pGetLangOfChar;
|
std::function<LanguageType (sal_Int32, sal_Int32, bool)> m_pGetLangOfChar;
|
||||||
OUString m_aWord;
|
OUString m_aWord;
|
||||||
|
OUString m_aPrevWord;
|
||||||
const OUString m_aPreDashReplacementText;
|
const OUString m_aPreDashReplacementText;
|
||||||
OUString m_aText;
|
OUString m_aText;
|
||||||
const LanguageType* m_pLanguage;
|
const LanguageType* m_pLanguage;
|
||||||
@@ -62,6 +63,7 @@ public:
|
|||||||
bool NextWord();
|
bool NextWord();
|
||||||
|
|
||||||
const OUString& GetWord() const { return m_aWord; }
|
const OUString& GetWord() const { return m_aWord; }
|
||||||
|
const OUString& GetPrevWord() const { return m_aPrevWord; }
|
||||||
|
|
||||||
sal_Int32 GetBegin() const { return m_nBegin; }
|
sal_Int32 GetBegin() const { return m_nBegin; }
|
||||||
sal_Int32 GetEnd() const { return m_nBegin + m_nLength; }
|
sal_Int32 GetEnd() const { return m_nBegin + m_nLength; }
|
||||||
|
@@ -961,6 +961,7 @@ bool SwScanner::NextWord()
|
|||||||
if ( m_nWordType == i18n::WordType::WORD_COUNT )
|
if ( m_nWordType == i18n::WordType::WORD_COUNT )
|
||||||
m_nLength = forceEachAsianCodePointToWord(m_aText, m_nBegin, m_nLength);
|
m_nLength = forceEachAsianCodePointToWord(m_aText, m_nBegin, m_nLength);
|
||||||
|
|
||||||
|
m_aPrevWord = m_aWord;
|
||||||
m_aWord = m_aPreDashReplacementText.copy( m_nBegin, m_nLength );
|
m_aWord = m_aPreDashReplacementText.copy( m_nBegin, m_nLength );
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -1035,8 +1036,11 @@ bool SwTextNode::Spell(SwSpellArgs* pArgs)
|
|||||||
SwScanner aScanner( *this, m_Text, nullptr, ModelToViewHelper(),
|
SwScanner aScanner( *this, m_Text, nullptr, ModelToViewHelper(),
|
||||||
WordType::DICTIONARY_WORD,
|
WordType::DICTIONARY_WORD,
|
||||||
nBegin, nEnd );
|
nBegin, nEnd );
|
||||||
while( !pArgs->xSpellAlt.is() && aScanner.NextWord() )
|
bool bNextWord = aScanner.NextWord();
|
||||||
|
while( !pArgs->xSpellAlt.is() && bNextWord )
|
||||||
{
|
{
|
||||||
|
bool bCalledNextWord = false;
|
||||||
|
|
||||||
const OUString& rWord = aScanner.GetWord();
|
const OUString& rWord = aScanner.GetWord();
|
||||||
|
|
||||||
// get next language for next word, consider language attributes
|
// get next language for next word, consider language attributes
|
||||||
@@ -1066,25 +1070,45 @@ bool SwTextNode::Spell(SwSpellArgs* pArgs)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// make sure the selection build later from the data
|
OUString sPrevWord = aScanner.GetPrevWord();
|
||||||
// below does not include "in word" character to the
|
auto nWordBegin = aScanner.GetBegin();
|
||||||
// left and right in order to preserve those. Therefore
|
auto nWordEnd = aScanner.GetEnd();
|
||||||
// count those "in words" in order to modify the
|
bNextWord = aScanner.NextWord();
|
||||||
// selection accordingly.
|
const OUString& rActualWord = aScanner.GetPrevWord();
|
||||||
const sal_Unicode* pChar = rWord.getStr();
|
bCalledNextWord = true;
|
||||||
sal_Int32 nLeft = 0;
|
// check space separated word pairs in the dictionary, e.g. "vice versa"
|
||||||
while (*pChar++ == CH_TXTATR_INWORD)
|
if ( !((bNextWord && pArgs->xSpeller->isValid( rActualWord + " " + aScanner.GetWord(),
|
||||||
++nLeft;
|
static_cast<sal_uInt16>(eActLang), Sequence< PropertyValue >() )) ||
|
||||||
pChar = rWord.getLength() ? rWord.getStr() + rWord.getLength() - 1 : nullptr;
|
( !sPrevWord.isEmpty() && pArgs->xSpeller->isValid( sPrevWord + " " + rActualWord,
|
||||||
sal_Int32 nRight = 0;
|
static_cast<sal_uInt16>(eActLang), Sequence< PropertyValue >() ))) )
|
||||||
while (pChar && *pChar-- == CH_TXTATR_INWORD)
|
{
|
||||||
++nRight;
|
// make sure the selection build later from the data
|
||||||
|
// below does not include "in word" character to the
|
||||||
|
// left and right in order to preserve those. Therefore
|
||||||
|
// count those "in words" in order to modify the
|
||||||
|
// selection accordingly.
|
||||||
|
const sal_Unicode* pChar = aScanner.GetPrevWord().getStr();
|
||||||
|
sal_Int32 nLeft = 0;
|
||||||
|
while (*pChar++ == CH_TXTATR_INWORD)
|
||||||
|
++nLeft;
|
||||||
|
pChar = rActualWord.getLength() ? rActualWord.getStr() + rActualWord.getLength() - 1 : nullptr;
|
||||||
|
sal_Int32 nRight = 0;
|
||||||
|
while (pChar && *pChar-- == CH_TXTATR_INWORD)
|
||||||
|
++nRight;
|
||||||
|
|
||||||
pArgs->pStartPos->Assign(*this, aScanner.GetEnd() - nRight );
|
pArgs->pStartPos->Assign(*this, nWordEnd - nRight );
|
||||||
pArgs->pEndPos->Assign(*this, aScanner.GetBegin() + nLeft );
|
pArgs->pEndPos->Assign(*this, nWordBegin + nLeft );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pArgs->xSpellAlt = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !bCalledNextWord )
|
||||||
|
bNextWord = aScanner.NextWord();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1342,11 +1366,13 @@ SwRect SwTextFrame::AutoSpell_(SwTextNode & rNode, sal_Int32 nActPos)
|
|||||||
SwScanner aScanner( *pNode, pNode->GetText(), nullptr, ModelToViewHelper(),
|
SwScanner aScanner( *pNode, pNode->GetText(), nullptr, ModelToViewHelper(),
|
||||||
WordType::DICTIONARY_WORD, nBegin, nEnd);
|
WordType::DICTIONARY_WORD, nBegin, nEnd);
|
||||||
|
|
||||||
while( aScanner.NextWord() )
|
bool bNextWord = aScanner.NextWord();
|
||||||
|
while( bNextWord )
|
||||||
{
|
{
|
||||||
const OUString& rWord = aScanner.GetWord();
|
const OUString& rWord = aScanner.GetWord();
|
||||||
nBegin = aScanner.GetBegin();
|
nBegin = aScanner.GetBegin();
|
||||||
sal_Int32 nLen = aScanner.GetLen();
|
sal_Int32 nLen = aScanner.GetLen();
|
||||||
|
bool bCalledNextWord = false;
|
||||||
|
|
||||||
// get next language for next word, consider language attributes
|
// get next language for next word, consider language attributes
|
||||||
// within the word
|
// within the word
|
||||||
@@ -1365,31 +1391,49 @@ SwRect SwTextFrame::AutoSpell_(SwTextNode & rNode, sal_Int32 nActPos)
|
|||||||
((!bRestoreString && !bContainsComments) || !xSpell->isValid( rWord.replaceAll(OUStringChar(CH_TXTATR_INWORD), ""),
|
((!bRestoreString && !bContainsComments) || !xSpell->isValid( rWord.replaceAll(OUStringChar(CH_TXTATR_INWORD), ""),
|
||||||
static_cast<sal_uInt16>(eActLang), Sequence< PropertyValue >() ) ) )
|
static_cast<sal_uInt16>(eActLang), Sequence< PropertyValue >() ) ) )
|
||||||
{
|
{
|
||||||
sal_Int32 nSmartTagStt = nBegin;
|
OUString sPrevWord = aScanner.GetPrevWord();
|
||||||
sal_Int32 nDummy = 1;
|
bNextWord = aScanner.NextWord();
|
||||||
if ( !pNode->GetSmartTags() || !pNode->GetSmartTags()->InWrongWord( nSmartTagStt, nDummy ) )
|
bCalledNextWord = true;
|
||||||
|
// check space separated word pairs in the dictionary, e.g. "vice versa"
|
||||||
|
if ( !((bNextWord && xSpell->isValid( aScanner.GetPrevWord() + " " + aScanner.GetWord(),
|
||||||
|
static_cast<sal_uInt16>(eActLang), Sequence< PropertyValue >() )) ||
|
||||||
|
(!sPrevWord.isEmpty() && xSpell->isValid( sPrevWord + " " + aScanner.GetPrevWord(),
|
||||||
|
static_cast<sal_uInt16>(eActLang), Sequence< PropertyValue >() ))) )
|
||||||
{
|
{
|
||||||
if( !pNode->GetWrong() )
|
sal_Int32 nSmartTagStt = nBegin;
|
||||||
|
sal_Int32 nDummy = 1;
|
||||||
|
if ( !pNode->GetSmartTags() || !pNode->GetSmartTags()->InWrongWord( nSmartTagStt, nDummy ) )
|
||||||
{
|
{
|
||||||
pNode->SetWrong( std::make_unique<SwWrongList>( WRONGLIST_SPELL ) );
|
if( !pNode->GetWrong() )
|
||||||
pNode->GetWrong()->SetInvalid( 0, nEnd );
|
{
|
||||||
}
|
pNode->SetWrong( std::make_unique<SwWrongList>( WRONGLIST_SPELL ) );
|
||||||
SwWrongList::FreshState const eState(pNode->GetWrong()->Fresh(
|
pNode->GetWrong()->SetInvalid( 0, nEnd );
|
||||||
nChgStart, nChgEnd, nBegin, nLen, nInsertPos, nActPos));
|
}
|
||||||
switch (eState)
|
SwWrongList::FreshState const eState(pNode->GetWrong()->Fresh(
|
||||||
{
|
nChgStart, nChgEnd, nBegin, nLen, nInsertPos, nActPos));
|
||||||
case SwWrongList::FreshState::FRESH:
|
switch (eState)
|
||||||
pNode->GetWrong()->Insert(OUString(), nullptr, nBegin, nLen, nInsertPos++);
|
{
|
||||||
break;
|
case SwWrongList::FreshState::FRESH:
|
||||||
case SwWrongList::FreshState::CURSOR:
|
pNode->GetWrong()->Insert(OUString(), nullptr, nBegin, nLen, nInsertPos++);
|
||||||
bPending = true;
|
break;
|
||||||
[[fallthrough]]; // to mark as invalid
|
case SwWrongList::FreshState::CURSOR:
|
||||||
case SwWrongList::FreshState::NOTHING:
|
bPending = true;
|
||||||
nInvStart = nBegin;
|
[[fallthrough]]; // to mark as invalid
|
||||||
nInvEnd = nBegin + nLen;
|
case SwWrongList::FreshState::NOTHING:
|
||||||
break;
|
nInvStart = nBegin;
|
||||||
|
nInvEnd = nBegin + nLen;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if( bAddAutoCmpl && rACW.GetMinWordLen() <= aScanner.GetPrevWord().getLength() )
|
||||||
|
{
|
||||||
|
// tdf#119695 only add the word if the cursor position is outside the word
|
||||||
|
// so that the incomplete words are not added as autocomplete candidates
|
||||||
|
bool bCursorOutsideWord = nActPos > nBegin + nLen || nActPos < nBegin;
|
||||||
|
if (bCursorOutsideWord)
|
||||||
|
rACW.InsertWord(aScanner.GetPrevWord(), rDoc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if( bAddAutoCmpl && rACW.GetMinWordLen() <= rWord.getLength() )
|
else if( bAddAutoCmpl && rACW.GetMinWordLen() <= rWord.getLength() )
|
||||||
{
|
{
|
||||||
@@ -1400,6 +1444,9 @@ SwRect SwTextFrame::AutoSpell_(SwTextNode & rNode, sal_Int32 nActPos)
|
|||||||
rACW.InsertWord(rWord, rDoc);
|
rACW.InsertWord(rWord, rDoc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !bCalledNextWord )
|
||||||
|
bNextWord = aScanner.NextWord();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user