2014-10-28 27 views
-1

我正在加载一个包含近80,000个单词的文件。它将被用作主拼写检查字典。这些词的顺序已经被随机化了。还有另一个文件,我正在加载拼写错误的单词,我必须检查。它还提供拼写错误的单词的建议。自定义拼写检查生成错误

public void spellCheckDocument(ArrayList<String> dictionary){ 
     long startCheck = System.currentTimeMillis(); 
     for(String words: collectionOfParagraphs) 
      for(String word: words.split("[^a-zA-Z_0-9']+")){ 
       int index = Collections.binarySearch(dictionary, word.toLowerCase()); 
       if(index<0 && word.length()>0){ 

        //collectionOfMisspelledWord.add(word+" Possible correct word: "+dictionary.get(-index+1)+" "+dictionary.get(-index)+" "+dictionary.get(-index-1)); 
        //System.out.printf("%s Misspelled, possible correct words: %s, %s, %s\n", word, dictionary.get(-index+1),dictionary.get(-index),dictionary.get(-index-1)); 
        possibleCorrectSpellings = new Document(word, dictionary.get(-index+1),dictionary.get(-index), dictionary.get(-index-1)); 
        collectionOfMisspelledWord.add(possibleCorrectSpellings); 
       }   
     } 

--------error---------- 
java.lang.IndexOutOfBoundsException: Index: 380, Size: 379 
    at java.util.ArrayList.rangeCheck(ArrayList.java:653) 
    at java.util.ArrayList.get(ArrayList.java:429) 
    at file.Document.spellCheckDocument(Document.java:82) 
+0

哪一行是82? – 2014-10-28 12:22:17

+0

ssibleCorrectSpellings = new Document(word,dictionary.get(-index + 1),dictionary.get(-index),dictionary.get(-index-1)); – V15720002000 2014-10-28 12:23:54

回答

0

Collections.binarySearch()文档:

否则,(-(insertion point) - 1)插入点被定义为键将被插入列表中的点:第一个元素的索引大于键,或者list.size(),如果列表中的所有元素小于指定的键。

这意味着你有时会得到一个超过列表中最后一个元素的索引。您需要为这种情况添加特殊处理(这可能意味着您不知道哪些词可能是正确的)。