Spellchecker try to straighten logic a bit (still under old API)

This commit is contained in:
Alexey Gopachenko
2012-09-06 21:34:36 +02:00
parent 7500a16637
commit e2f5f65cf4
4 changed files with 52 additions and 57 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,6 +42,14 @@ public class AggregatedDictionary implements EditableDictionary {
return false;
}
@Override
public String toString() {
return "AggregatedDictionary{" +
"cachedDictionary=" + cachedDictionary +
", projectDictionary=" + projectDictionary +
'}';
}
public boolean contains(String word) {
if (word == null) {
return false;
@@ -82,11 +90,11 @@ public class AggregatedDictionary implements EditableDictionary {
public Set<String> getWords() {
return cachedDictionary!=null?cachedDictionary.getWords():null;
return cachedDictionary.getWords();
}
public int size() {
return (cachedDictionary!=null?cachedDictionary.size():0);
return (cachedDictionary.size());
}
@Nullable
@@ -60,21 +60,18 @@ public class ProjectDictionary implements EditableDictionary {
if (word == null || dictionaries == null) {
return false;
}
int negatives = 0;
int errors = 0;
for (Dictionary dictionary : dictionaries) {
try {
if (dictionary.contains(word)) {
return true;
}
else {
negatives++;
}
}
catch (EncodingException e) {
//System.out.println("e.getMessage() = " + e.getMessage() + " " + word);
errors++;
}
}
if (negatives==dictionaries.size()) throw new EncodingException("WORD_OF_ENTIRELY_UNKNOWN_LETTERS_FOR_ALL");
if (errors==dictionaries.size()) throw new EncodingException("WORD_OF_ENTIRELY_UNKNOWN_LETTERS_FOR_ALL");
return false;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package com.intellij.spellchecker.dictionary;
import com.intellij.spellchecker.compress.EncodingException;
import com.intellij.util.Consumer;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
@@ -39,11 +40,13 @@ public class UserDictionary implements EditableDictionary {
}
public boolean contains(String word) {
return words.contains(word);
boolean contains = words.contains(word);
if(contains) return true;
throw new EncodingException("WE_CARE_ABOUT_CONTAINS_ONLY");
}
public int size() {
return words == null ? 0 : words.size();
return words.size();
}
@Nullable
@@ -36,7 +36,6 @@ import com.intellij.spellchecker.dictionary.Loader;
import com.intellij.util.Consumer;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.UIUtil;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -49,7 +48,7 @@ public class BaseSpellChecker implements SpellCheckerEngine {
private final Transformation transform = new Transformation();
private final Set<EditableDictionary> dictionaries = new THashSet<EditableDictionary>();
private final Set<EditableDictionary> dictionaries = new HashSet<EditableDictionary>();
private final List<Dictionary> bundledDictionaries = ContainerUtil.createEmptyCOWList();
private final Metrics metrics = new LevenshteinDistance();
@@ -70,23 +69,9 @@ public class BaseSpellChecker implements SpellCheckerEngine {
}
}
else {
loadFixedDictionary(loader);
}
}
private void loadFixedDictionary(final @NotNull Loader loader) {
/*if (ApplicationManager.getApplication().isUnitTestMode()) {
loadCompressedDictionary(loader);
}
else {
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
public void run() {
loadCompressedDictionary(loader);
}
});
}*/
loadCompressedDictionary(loader);
}
private void loadCompressedDictionary(@NotNull Loader loader) {
@@ -248,45 +233,47 @@ public class BaseSpellChecker implements SpellCheckerEngine {
return result;
}
private static boolean isCorrect(@NotNull String transformed, @Nullable Collection dictionaries) {
/**
* @param transformed
* @param dictionaries
* @return -1 (all)failed / 0 (any) ok / >0 all alien
*/
private static int isCorrect(@NotNull String transformed, @Nullable Collection<? extends Dictionary> dictionaries) {
if (dictionaries == null) {
return true;
return -1;
}
for (Object o : dictionaries) {
if (o instanceof Dictionary) {
boolean result = isCorrect(transformed, (Dictionary)o);
if (result) {
return true;
}
//System.out.println("dictionaries = " + dictionaries);
int errors = 0;
for (Dictionary dictionary : dictionaries) {
try {
if (dictionary == null) continue;
//System.out.print("\tBSC.isCorrect " + transformed + " " + dictionary);
boolean contains = dictionary.contains(transformed);
//System.out.println("\tcontains = " + contains);
if (contains) return 0;
}
catch (EncodingException e) {
++errors;
//System.out.println(e.getMessage() + " " + transformed);
//return true;
}
}
return false;
}
private static boolean isCorrect(@NotNull String transformed, @Nullable Dictionary dictionary) {
if (dictionary == null) {
return true;
}
try {
return dictionary.contains(transformed);
}
catch (EncodingException e) {
//System.out.println("e.getMessage() = " + e.getMessage() + " " + transformed);
return true;
}
if(errors==dictionaries.size()) return errors;
return -1;
}
public boolean isCorrect(@NotNull String word) {
//System.out.println("---\n"+word);
final String transformed = transform.transform(word);
if (transformed == null) {
if (myLoadingDictionaries.get() || transformed == null) {
return true;
}
return myLoadingDictionaries.get() || isCorrect(transformed, bundledDictionaries) || isCorrect(transformed, dictionaries);
int bundled = isCorrect(transformed, bundledDictionaries);
int user = isCorrect(transformed, dictionaries);
//System.out.println("bundled = " + bundled);
//System.out.println("user = " + user);
return bundled == 0 || user==0 || bundled>0 && user>0;
}