public class Speller
extends java.lang.Object
See Jan Daciuk's s_fsa package.
| Modifier and Type | Class and Description |
|---|---|
private class |
Speller.CandidateData
Used to sort candidates according to edit distance, and possibly according
to their frequency in the future.
|
| Modifier and Type | Field and Description |
|---|---|
private java.nio.ByteBuffer |
byteBuffer
Internal reusable buffer for encoding words into byte arrays using
encoder. |
private char[] |
candidate |
private java.util.List<Speller.CandidateData> |
candidates
List of candidate strings, including same additional data such as edit
distance from the original word.
|
private int |
candLen |
private java.nio.CharBuffer |
charBuffer
Internal reusable buffer for encoding words into byte arrays using
encoder. |
private boolean |
containsSeparators |
private java.nio.charset.CharsetDecoder |
decoder
Charset decoder for the FSA.
|
private DictionaryMetadata |
dictionaryMetadata
Features of the compiled dictionary.
|
private int |
editDistance |
private int |
effectEditDistance |
private java.nio.charset.CharsetEncoder |
encoder
Charset encoder for the FSA.
|
private ByteSequenceIterator |
finalStatesIterator
An iterator for walking along the final states of
fsa. |
(package private) static int |
FIRST_RANGE_CODE |
(package private) static int |
FREQ_RANGES |
private FSA |
fsa
The FSA we are using.
|
private HMatrix |
hMatrix |
private FSATraversal |
matcher
An FSA used for lookups.
|
private MatchResult |
matchResult
Reusable match result.
|
private static int |
MAX_RECURSION_LEVEL |
static int |
MAX_WORD_LENGTH
Maximum length of the word to be checked.
|
private static int |
MIN_WORD_LENGTH |
private java.util.Map<java.lang.Character,java.util.List<char[]>> |
replacementsAnyToOne |
private java.util.Map<java.lang.String,java.util.List<char[]>> |
replacementsAnyToTwo |
private java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
replacementsTheRest |
private int |
rootNode
FSA's root node.
|
(package private) static int |
UPPER_SEARCH_LIMIT |
private int |
wordLen |
private char[] |
wordProcessed |
| Constructor and Description |
|---|
Speller(Dictionary dictionary) |
Speller(Dictionary dictionary,
int editDistance) |
| Modifier and Type | Method and Description |
|---|---|
private void |
addCandidate(int depth,
int dist) |
private boolean |
areEqual(char x,
char y) |
private java.nio.ByteBuffer |
charSequenceToBytes(java.lang.CharSequence word) |
private java.nio.ByteBuffer |
charsToBytes(java.nio.CharBuffer chars,
java.nio.ByteBuffer bytes)
Encode a character sequence into a byte buffer, optionally expanding
buffer.
|
(package private) static boolean |
containsNoDigit(java.lang.String s)
Checks whether a string contains a digit.
|
boolean |
convertsCase()
Used to determine whether the dictionary supports case conversions.
|
private void |
createReplacementsMaps() |
int |
cuted(int depth,
int wordIndex,
int candIndex)
Calculates cut-off edit distance.
|
int |
ed(int i,
int j,
int wordIndex,
int candIndex)
Calculates edit distance.
|
private void |
findRepl(int depth,
int node,
byte[] prevBytes,
int wordIndex,
int candIndex) |
java.util.List<java.lang.String> |
findReplacements(java.lang.String w)
Find suggestions by using K.
|
java.util.List<java.lang.String> |
getAllReplacements(java.lang.String str,
int fromIndex,
int level) |
int |
getCandLen() |
int |
getEffectiveED() |
int |
getFrequency(java.lang.CharSequence word)
Get the frequency value for a word form.
|
int |
getWordLen() |
private java.lang.CharSequence |
initialUppercase(java.lang.String wordToCheck) |
(package private) boolean |
isAllUppercase(java.lang.String str)
Returns true if
str is made up of all-uppercase characters
(ignoring characters for which no upper-/lowercase distinction exists). |
(package private) static boolean |
isAlphabetic(int codePoint)
Copy-paste of Character.isAlphabetic() (needed as we require only 1.6)
|
private boolean |
isArcNotTerminal(int arc,
int candIndex) |
private boolean |
isBeforeSeparator(int arc) |
boolean |
isCamelCase(java.lang.String str) |
private boolean |
isEndOfCandidate(int arc,
int wordIndex) |
boolean |
isInDictionary(java.lang.CharSequence word)
Test whether the word is found in the dictionary.
|
boolean |
isMisspelled(java.lang.String word)
Checks whether the word is misspelled, by performing a series of checks
according to properties of the dictionary.
|
(package private) boolean |
isMixedCase(java.lang.String str) |
(package private) boolean |
isNotAllLowercase(java.lang.String str)
Returns true if
str is made up of all-lowercase characters
(ignoring characters for which no upper-/lowercase distinction exists). |
(package private) boolean |
isNotCapitalizedWord(java.lang.String str) |
(package private) static boolean |
isNotEmpty(java.lang.String str)
Helper method to replace calls to "".equals().
|
private int |
matchAnyToOne(int wordIndex,
int candIndex) |
private int |
matchAnyToTwo(int wordIndex,
int candIndex) |
private static int |
min(int a,
int b,
int c) |
java.util.List<java.lang.String> |
replaceRunOnWords(java.lang.String original)
Propose suggestions for misspelled run-on words.
|
(package private) void |
setWordAndCandidate(java.lang.String word,
java.lang.String candidate)
Sets up the word and candidate.
|
public static final int MAX_WORD_LENGTH
static final int FREQ_RANGES
static final int FIRST_RANGE_CODE
static final int UPPER_SEARCH_LIMIT
private static final int MIN_WORD_LENGTH
private static final int MAX_RECURSION_LEVEL
private final int editDistance
private int effectEditDistance
private final HMatrix hMatrix
private char[] candidate
private int candLen
private int wordLen
private char[] wordProcessed
private java.util.Map<java.lang.Character,java.util.List<char[]>> replacementsAnyToOne
private java.util.Map<java.lang.String,java.util.List<char[]>> replacementsAnyToTwo
private java.util.Map<java.lang.String,java.util.List<java.lang.String>> replacementsTheRest
private final java.util.List<Speller.CandidateData> candidates
private boolean containsSeparators
private java.nio.ByteBuffer byteBuffer
encoder.private java.nio.CharBuffer charBuffer
encoder.private final MatchResult matchResult
private final DictionaryMetadata dictionaryMetadata
DictionaryMetadataprivate final java.nio.charset.CharsetEncoder encoder
private final java.nio.charset.CharsetDecoder decoder
private final FSATraversal matcher
private final int rootNode
private final FSA fsa
private final ByteSequenceIterator finalStatesIterator
fsa.public Speller(Dictionary dictionary)
public Speller(Dictionary dictionary, int editDistance)
private void createReplacementsMaps()
private java.nio.ByteBuffer charsToBytes(java.nio.CharBuffer chars,
java.nio.ByteBuffer bytes)
private java.nio.ByteBuffer charSequenceToBytes(java.lang.CharSequence word)
public boolean isMisspelled(java.lang.String word)
fsa.dict.speller.ignore-punctuation is set, then
all non-alphabetic characters are considered to be correctly spelled.
If the flag fsa.dict.speller.ignore-numbers is set, then all
words containing decimal digits are considered to be correctly spelled.
If the flag fsa.dict.speller.ignore-camel-case is set, then
all CamelCase words are considered to be correctly spelled.
If the flag fsa.dict.speller.ignore-all-uppercase is set, then
all alphabetic words composed of only uppercase characters are considered
to be correctly spelled.
Otherwise, the word is checked in the dictionary. If the test fails, and
the dictionary does not perform any case conversions (as set by
fsa.dict.speller.convert-case flag), then the method returns
false. In case of case conversions, it is checked whether a non-mixed case
word is found in its lowercase version in the dictionary, and for
all-uppercase words, whether the word is found in the dictionary with the
initial uppercase letter.word - - the word to be checkedprivate java.lang.CharSequence initialUppercase(java.lang.String wordToCheck)
public boolean isInDictionary(java.lang.CharSequence word)
word - the word to be testedpublic int getFrequency(java.lang.CharSequence word)
word - the word to be testedpublic java.util.List<java.lang.String> replaceRunOnWords(java.lang.String original)
original - The original misspelled word.public java.util.List<java.lang.String> findReplacements(java.lang.String w)
w - The original misspelled word.private void findRepl(int depth,
int node,
byte[] prevBytes,
int wordIndex,
int candIndex)
private boolean isArcNotTerminal(int arc,
int candIndex)
private boolean isEndOfCandidate(int arc,
int wordIndex)
private boolean isBeforeSeparator(int arc)
private void addCandidate(int depth,
int dist)
public int ed(int i,
int j,
int wordIndex,
int candIndex)
i - length of first word (here: misspelled) - 1;j - length of second word (here: candidate) - 1.wordIndex - (TODO: javadoc?)candIndex - (TODO: javadoc?)private boolean areEqual(char x,
char y)
public int cuted(int depth,
int wordIndex,
int candIndex)
depth - current length of candidates.wordIndex - (TODO: javadoc?)candIndex - (TODO: javadoc?)private int matchAnyToOne(int wordIndex,
int candIndex)
private int matchAnyToTwo(int wordIndex,
int candIndex)
private static int min(int a,
int b,
int c)
static boolean isAlphabetic(int codePoint)
codePoint - The input character.static boolean containsNoDigit(java.lang.String s)
s - Word to be checked.boolean isAllUppercase(java.lang.String str)
str is made up of all-uppercase characters
(ignoring characters for which no upper-/lowercase distinction exists).boolean isNotAllLowercase(java.lang.String str)
str is made up of all-lowercase characters
(ignoring characters for which no upper-/lowercase distinction exists).boolean isNotCapitalizedWord(java.lang.String str)
str - input stringstatic boolean isNotEmpty(java.lang.String str)
str - String to checkboolean isMixedCase(java.lang.String str)
str - input strpublic boolean isCamelCase(java.lang.String str)
str - The string to check.public boolean convertsCase()
public java.util.List<java.lang.String> getAllReplacements(java.lang.String str,
int fromIndex,
int level)
str - The string to find the replacements for.fromIndex - The index from which replacements are found.level - The recursion level. The search stops if level is > MAX_RECURSION_LEVEL.void setWordAndCandidate(java.lang.String word,
java.lang.String candidate)
word - the first wordcandidate - the second word used for edit distance calculationpublic final int getWordLen()
public final int getCandLen()
public final int getEffectiveED()