1923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project/* 2443c360d0afdbab091994244f045f4756feaf2b4Jean-Baptiste Queru * Copyright (C) 2008 The Android Open Source Project 3043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalard * 4923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * use this file except in compliance with the License. You may obtain a copy of 6923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * the License at 7043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalard * 8923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalard * 10923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * License for the specific language governing permissions and limitations under 14923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * the License. 15923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project */ 16923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project 17923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Projectpackage com.android.inputmethod.latin; 18923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project 19043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalardimport com.android.inputmethod.keyboard.ProximityInfo; 20043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalard 21923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project/** 22923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key 23923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * strokes. 24923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project */ 25e26ef1bccddc942fdaeada3409c8e8ff18a35008Tadashi G. Takaokapublic abstract class Dictionary { 26923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project /** 27923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * Whether or not to replicate the typed word in the suggested list, even if it's valid. 28923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project */ 29923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project protected static final boolean INCLUDE_TYPED_WORD_IF_VALID = false; 30043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalard 31923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project /** 32923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * The weight to give to a word if it's length is the same as the number of typed characters. 33923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project */ 34e7a2512aa3666e1b891dc7dfc5a0cb28fd66bea9Tadashi G. Takaoka protected static final int FULL_WORD_SCORE_MULTIPLIER = 2; 35937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung 36937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung public static enum DataType { 37937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung UNIGRAM, BIGRAM 38937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung } 39937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung 40923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project /** 41923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * Interface to be implemented by classes requesting words to be fetched from the dictionary. 42923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @see #getWords(WordComposer, WordCallback) 43923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project */ 44923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project public interface WordCallback { 45923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project /** 46923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * Adds a word to a list of suggestions. The word is expected to be ordered based on 47e7a2512aa3666e1b891dc7dfc5a0cb28fd66bea9Tadashi G. Takaoka * the provided score. 48923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @param word the character array containing the word 49923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @param wordOffset starting offset of the word in the character array 50923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @param wordLength length of valid characters in the character array 51e7a2512aa3666e1b891dc7dfc5a0cb28fd66bea9Tadashi G. Takaoka * @param score the score of occurrence. This is normalized between 1 and 255, but 52923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * can exceed those limits 534ee3676cf38f3f9b2587f37e259b6d7511ef4ab1satok * @param dicTypeId of the dictionary where word was from 54937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung * @param dataType tells type of this data 55923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @return true if the word was added, false if no more words are required 56923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project */ 57e7a2512aa3666e1b891dc7dfc5a0cb28fd66bea9Tadashi G. Takaoka boolean addWord(char[] word, int wordOffset, int wordLength, int score, int dicTypeId, 58937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung DataType dataType); 59923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project } 60923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project 61923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project /** 62043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalard * Searches for words in the dictionary that match the characters in the composer. Matched 63923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * words are added through the callback object. 64923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @param composer the key sequence to match 65923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @param callback the callback object to send matched words to as possible candidates 66043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalard * @param proximityInfo the object for key proximity. May be ignored by some implementations. 670c8d5ca023d54b7c9ef6c20eb7988288132bacb5Jean Chalard * @see WordCallback#addWord(char[], int, int, int, int, DataType) 68923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project */ 69043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalard abstract public void getWords(final WordComposer composer, final WordCallback callback, 70043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalard final ProximityInfo proximityInfo); 71923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project 72923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project /** 73937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung * Searches for pairs in the bigram dictionary that matches the previous word and all the 74937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung * possible words following are added through the callback object. 75937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung * @param composer the key sequence to match 76e26ef1bccddc942fdaeada3409c8e8ff18a35008Tadashi G. Takaoka * @param previousWord the word before 77937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung * @param callback the callback object to send possible word following previous word 78937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung */ 79937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung public void getBigrams(final WordComposer composer, final CharSequence previousWord, 80887f11ee43ad621aa6ad93d535ab7f48dec73fc7Tadashi G. Takaoka final WordCallback callback) { 81937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung // empty base implementation 82937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung } 83937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung 84937d5ad0131267aa4273f3e5d75b203a1f263c18Jae Yong Sung /** 85923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * Checks if the given word occurs in the dictionary 86923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @param word the word to search for. The search should be case-insensitive. 87923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @return true if the word exists, false otherwise 88923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project */ 89923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project abstract public boolean isValidWord(CharSequence word); 90043f7841985916717f4fa821fe3e423daf3ff2f5Jean Chalard 91923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project /** 92923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * Compares the contents of the character array with the typed word and returns true if they 93923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * are the same. 94923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @param word the array of characters that make up the word 95923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @param length the number of valid characters in the character array 96923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @param typedWord the word to compare with 97923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project * @return true if they are the same, false otherwise. 98923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project */ 99923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project protected boolean same(final char[] word, final int length, final CharSequence typedWord) { 100923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project if (typedWord.length() != length) { 101923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project return false; 102923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project } 103923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project for (int i = 0; i < length; i++) { 104923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project if (word[i] != typedWord.charAt(i)) { 105923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project return false; 106923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project } 107923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project } 108923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project return true; 109923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project } 110923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project 11136fcf25833f7c8876cbc8286e0c159b052dc2626Amith Yamasani /** 11236fcf25833f7c8876cbc8286e0c159b052dc2626Amith Yamasani * Override to clean up any resources. 11336fcf25833f7c8876cbc8286e0c159b052dc2626Amith Yamasani */ 11436fcf25833f7c8876cbc8286e0c159b052dc2626Amith Yamasani public void close() { 115e26ef1bccddc942fdaeada3409c8e8ff18a35008Tadashi G. Takaoka // empty base implementation 11636fcf25833f7c8876cbc8286e0c159b052dc2626Amith Yamasani } 117923bf41f853a544fd0d71fbf7dc90359ec35981The Android Open Source Project} 118