[Spellchecker] [IDEA-243510] Fix suggestions in some specific cases

GitOrigin-RevId: b97e0ccd78186b9d6e1d51214e7d826c4e7b3a60
This commit is contained in:
Vladislav.Tankov
2020-06-17 16:11:04 +03:00
committed by intellij-monorepo-bot
parent ab9c116212
commit d36c35b97d
7 changed files with 17 additions and 67 deletions

View File

@@ -256,7 +256,7 @@ class CommunityLibraryLicenses {
new LibraryLicense(name: "Grazie Language Tool (English)", libraryName: "tanvd.grazi.languagetool:grazi-language-tool-en:4.7.10",
license: "LGPL 2.1",
licenseUrl: "http://www.gnu.org/licenses/lgpl-2.1.txt"),
new LibraryLicense(name: "Grazie SpellChecker", libraryName: "com.intellij.grazie:grazie-spellchecker:0.1.6",
new LibraryLicense(name: "Grazie SpellChecker", libraryName: "com.intellij.grazie:grazie-spellchecker:0.1.8",
license: "Apache 2.0",
licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0.txt"),
new LibraryLicense(name: "Groovy", libraryName: "org.codehaus.groovy:groovy",

View File

@@ -25,7 +25,7 @@ class GroovySpellcheckerTest extends LightJavaCodeInsightFixtureTestCase {
void testParameterName() {
myFixture.configureByText 'a.groovy', '''
def test(int <TYPO descr="Typo: In word 'dddd'">dddd</TYPO>) {
def test(int <TYPO descr="Typo: In word 'ddddd'">ddddd</TYPO>) {
}
'''
checkTypos()
@@ -48,10 +48,10 @@ class SpockTest {
void testStringEscapes() {
myFixture.configureByText 'a.groovy', '''
def foo = "\\ntest \\n<TYPO descr="Typo: In word 'dddd'">dddd</TYPO>"
def foo1 = '\\ntest \\n<TYPO descr="Typo: In word 'dddd'">dddd</TYPO>'
def bar = """\\ntest \\n<TYPO descr="Typo: In word 'dddd'">dddd</TYPO>"""
def bar1 = \'''\\ntest \\n<TYPO descr="Typo: In word 'dddd'">dddd</TYPO>\'''
def foo = "\\ntest \\n<TYPO descr="Typo: In word 'ddddd'">ddddd</TYPO>"
def foo1 = '\\ntest \\n<TYPO descr="Typo: In word 'ddddd'">ddddd</TYPO>'
def bar = """\\ntest \\n<TYPO descr="Typo: In word 'ddddd'">ddddd</TYPO>"""
def bar1 = \'''\\ntest \\n<TYPO descr="Typo: In word 'ddddd'">ddddd</TYPO>\'''
'''
checkTypos()
}

View File

@@ -24,14 +24,14 @@
<orderEntry type="module" module-name="intellij.platform.statistics" />
<orderEntry type="library" name="Trove4j" level="project" />
<orderEntry type="module-library">
<library name="com.intellij.grazie:grazie-spellchecker:0.1.6" type="repository">
<properties include-transitive-deps="false" maven-id="com.intellij.grazie:grazie-spellchecker:0.1.6" />
<library name="com.intellij.grazie:grazie-spellchecker:0.1.8" type="repository">
<properties include-transitive-deps="false" maven-id="com.intellij.grazie:grazie-spellchecker:0.1.8" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/intellij/grazie/grazie-spellchecker/0.1.6/grazie-spellchecker-0.1.6.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/intellij/grazie/grazie-spellchecker/0.1.8/grazie-spellchecker-0.1.8.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/intellij/grazie/grazie-spellchecker/0.1.6/grazie-spellchecker-0.1.6-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/intellij/grazie/grazie-spellchecker/0.1.8/grazie-spellchecker-0.1.8-sources.jar!/" />
</SOURCES>
</library>
</orderEntry>

View File

@@ -1,22 +0,0 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.spellchecker.grazie
import com.intellij.grazie.speller.utils.spitter.Splitter
import com.intellij.openapi.util.TextRange
import com.intellij.spellchecker.inspections.IdentifierSplitter
internal object GrazieIdentifierSplitter : Splitter {
private val splitter = IdentifierSplitter.getInstance()
override fun split(text: String): Sequence<String> {
if (text.all { it.isLowerCase() && it.isLetter() }) return sequenceOf(text)
//It is safe to use here just list, since this splitter should never encounter too long words
val splits = ArrayList<String>()
splitter.split(text, TextRange.allOf(text)) {
splits.add(it.substring(text))
}
return splits.asSequence()
}
}

View File

@@ -13,6 +13,7 @@ import com.intellij.grazie.speller.suggestion.filter.ChainSuggestionFilter
import com.intellij.grazie.speller.suggestion.filter.feature.CasingSuggestionFilter
import com.intellij.grazie.speller.suggestion.filter.feature.ListSuggestionFilter
import com.intellij.grazie.speller.utils.DictionaryResources
import com.intellij.grazie.speller.utils.spitter.CamelCaseSplitter
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.io.FileUtil
import com.intellij.spellchecker.dictionary.Dictionary
@@ -23,7 +24,6 @@ import com.intellij.spellchecker.engine.Transformation
import com.intellij.spellchecker.grazie.async.GrazieAsyncSpeller
import com.intellij.spellchecker.grazie.async.WordListLoader
import com.intellij.spellchecker.grazie.dictionary.WordListAdapter
import com.intellij.spellchecker.util.Strings
import java.util.*
internal class GrazieSpellCheckerEngine(project: Project) : SpellCheckerEngine {
@@ -45,11 +45,11 @@ internal class GrazieSpellCheckerEngine(project: Project) : SpellCheckerEngine {
//transform main dictionary -- add lower-cased versions for misspelled check
TransformedDictionary(English.Lists.suggested, transformation),
//add splits to support camel-cased words
DictionaryResources.getSplitsDictionary(English.Lists.suggested, transformation, GrazieIdentifierSplitter),
DictionaryResources.getSplitsDictionary(English.Lists.suggested, transformation, CamelCaseSplitter),
//Should not be transformed, since it is already in lower case
TransformingDictionary(adapter, transformation)
),
splitter = GrazieIdentifierSplitter
splitter = CamelCaseSplitter
),
model = GrazieSpeller.UserConfig.Model(
filter = ChainSuggestionFilter(
@@ -59,7 +59,7 @@ internal class GrazieSpellCheckerEngine(project: Project) : SpellCheckerEngine {
)
)
),
GrazieSplittingSpeller.UserConfig(splitter = GrazieIdentifierSplitter)
GrazieSplittingSpeller.UserConfig(splitter = CamelCaseSplitter)
)
}
@@ -85,19 +85,8 @@ internal class GrazieSpellCheckerEngine(project: Project) : SpellCheckerEngine {
return mySpeller.isMisspelled(word).not()
}
/**
* Normalization used before suggestion.
*
* Without normalization distance between word and suggestion candidate
* can be too big.
*/
private fun normalize(word: String): String = when {
Strings.isUpperCase(word) -> word.toLowerCase(Locale.ENGLISH)
else -> word
}
override fun getSuggestions(word: String, maxSuggestions: Int, maxMetrics: Int): List<String> {
return mySpeller.suggest(normalize(word)).take(maxSuggestions).toMutableList()
return mySpeller.suggest(word).take(maxSuggestions).toMutableList()
}
override fun reset() {

View File

@@ -3,26 +3,9 @@ package com.intellij.spellchecker.grazie
import com.intellij.spellchecker.engine.SpellCheckerEngine
import com.intellij.spellchecker.engine.SuggestionProvider
import com.intellij.spellchecker.util.Strings
internal class GrazieSuggestionProvider(private val engine: SpellCheckerEngine) : SuggestionProvider {
override fun getSuggestions(text: String, maxSuggestions: Int, maxMetrics: Int): List<String> {
val suggestions = engine.getSuggestions(text, maxSuggestions, maxMetrics)
return alignSuggestionsToWord(text, suggestions)
}
private fun alignSuggestionsToWord(word: String, suggestions: List<String>): List<String> {
if (suggestions.isEmpty()) return emptyList()
if (engine.isCorrect(word)) return emptyList()
if (Strings.isCapitalized(word)) {
Strings.capitalize(suggestions)
}
else if (Strings.isUpperCase(word)) {
Strings.upperCase(suggestions)
}
return suggestions.distinct()
return engine.getSuggestions(text, maxSuggestions, maxMetrics).distinct()
}
}

View File

@@ -1,3 +1,3 @@
name=value
<TYPO descr="Typo: In word 'nmae'">nmae</TYPO>=Correct <TYPO descr="Typo: In word 'dsds'">dsds</TYPO>
make.<TYPO descr="Typo: In word 'filedf'">filedf</TYPO>=correct <TYPO descr="Typo: In word 'ffff'">ffff</TYPO> <TYPO descr="Typo: In word 'dddd'">dddd</TYPO>
make.<TYPO descr="Typo: In word 'filedf'">filedf</TYPO>=correct <TYPO descr="Typo: In word 'ffff'">ffff</TYPO> <TYPO descr="Typo: In word 'ddddd'">ddddd</TYPO>