Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LatinIME
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
keyboard
LatinIME
Commits
c2c44f94
Commit
c2c44f94
authored
14 years ago
by
Tadashi G. Takaoka
Browse files
Options
Downloads
Patches
Plain Diff
Cleanup Suggest to be able to be unit test ready
Bug: 3414081 Change-Id: Ia76afac4b1a61b8953a215b7cbdb7557736f7b9c
parent
dca305dd
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
java/src/com/android/inputmethod/latin/AutoCorrection.java
+14
-5
14 additions, 5 deletions
java/src/com/android/inputmethod/latin/AutoCorrection.java
java/src/com/android/inputmethod/latin/Suggest.java
+52
-59
52 additions, 59 deletions
java/src/com/android/inputmethod/latin/Suggest.java
with
66 additions
and
64 deletions
java/src/com/android/inputmethod/latin/AutoCorrection.java
+
14
−
5
View file @
c2c44f94
...
...
@@ -19,6 +19,7 @@ package com.android.inputmethod.latin;
import
android.util.Log
;
import
java.util.ArrayList
;
import
java.util.Collection
;
public
class
AutoCorrection
{
private
static
final
boolean
DBG
=
LatinImeLogger
.
sDBG
;
...
...
@@ -45,12 +46,12 @@ public class AutoCorrection {
return
mNormalizedScore
;
}
public
void
updateAutoCorrectionStatus
(
Suggest
sugg
es
t
,
public
void
updateAutoCorrectionStatus
(
Collection
<
Dictionary
>
dictionari
es
,
WordComposer
wordComposer
,
ArrayList
<
CharSequence
>
suggestions
,
int
[]
priorities
,
CharSequence
typedWord
,
double
autoCorrectionThreshold
,
int
correctionMode
,
CharSequence
quickFixedWord
)
{
if
(
hasAutoCorrectionForTypedWord
(
sugg
es
t
,
wordComposer
,
suggestions
,
typedWord
,
correctionMode
))
{
dictionari
es
,
wordComposer
,
suggestions
,
typedWord
,
correctionMode
))
{
mHasAutoCorrection
=
true
;
mAutoCorrectionWord
=
typedWord
;
}
else
if
(
hasAutoCorrectForBinaryDictionary
(
wordComposer
,
suggestions
,
correctionMode
,
...
...
@@ -63,9 +64,17 @@ public class AutoCorrection {
}
}
private
boolean
hasAutoCorrectionForTypedWord
(
Suggest
suggest
,
WordComposer
wordComposer
,
ArrayList
<
CharSequence
>
suggestions
,
CharSequence
typedWord
,
int
correctionMode
)
{
return
wordComposer
.
size
()
>
1
&&
suggestions
.
size
()
>
0
&&
suggest
.
isValidWord
(
typedWord
)
private
boolean
hasAutoCorrectionForTypedWord
(
Collection
<
Dictionary
>
dictionaries
,
WordComposer
wordComposer
,
ArrayList
<
CharSequence
>
suggestions
,
CharSequence
typedWord
,
int
correctionMode
)
{
boolean
isValidWord
=
false
;
for
(
final
Dictionary
dictionary
:
dictionaries
)
{
if
(
dictionary
.
isValidWord
(
typedWord
))
{
isValidWord
=
true
;
break
;
}
}
return
wordComposer
.
size
()
>
1
&&
suggestions
.
size
()
>
0
&&
isValidWord
&&
(
correctionMode
==
Suggest
.
CORRECTION_FULL
||
correctionMode
==
Suggest
.
CORRECTION_FULL_BIGRAM
);
}
...
...
This diff is collapsed.
Click to expand it.
java/src/com/android/inputmethod/latin/Suggest.java
+
52
−
59
View file @
c2c44f94
...
...
@@ -25,6 +25,10 @@ import android.view.View;
import
java.io.File
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Map
;
import
java.util.Set
;
/**
* This class loads a dictionary and provides a list of suggestions for a given sequence of
...
...
@@ -70,14 +74,13 @@ public class Suggest implements Dictionary.WordCallback {
private
AutoCorrection
mAutoCorrection
;
private
BinaryDictionary
mMainDict
;
private
Dictionary
mUserDictionary
;
private
Dictionary
mAutoDictionary
;
private
Dictionary
mContactsDictionary
;
private
Dictionary
mUserBigramDictionary
;
private
static
final
String
DICT_KEY_MAIN
=
"main"
;
private
static
final
String
DICT_KEY_CONTACTS
=
"contacts"
;
private
static
final
String
DICT_KEY_AUTO
=
"auto"
;
private
static
final
String
DICT_KEY_USER
=
"user"
;
private
static
final
String
DICT_KEY_USER_BIGRAM
=
"user_bigram"
;
private
final
Map
<
String
,
Dictionary
>
mUnigramDictionaries
=
new
HashMap
<
String
,
Dictionary
>();
private
final
Map
<
String
,
Dictionary
>
mBigramDictionaries
=
new
HashMap
<
String
,
Dictionary
>();
private
int
mPrefMaxSuggestions
=
12
;
...
...
@@ -101,16 +104,19 @@ public class Suggest implements Dictionary.WordCallback {
private
int
mCorrectionMode
=
CORRECTION_BASIC
;
public
Suggest
(
Context
context
,
int
dictionaryResId
)
{
mMainDict
=
BinaryDictionary
.
initDictionary
(
context
,
dictionaryResId
,
DIC_MAIN
);
init
();
init
(
BinaryDictionary
.
initDictionary
(
context
,
dictionaryResId
,
DIC_MAIN
));
}
/* package for test */
Suggest
(
File
dictionary
,
long
startOffset
,
long
length
)
{
mMainDict
=
BinaryDictionary
.
initDictionary
(
dictionary
,
startOffset
,
length
,
DIC_MAIN
);
init
();
init
(
BinaryDictionary
.
initDictionary
(
dictionary
,
startOffset
,
length
,
DIC_MAIN
));
}
private
void
init
()
{
private
void
init
(
BinaryDictionary
mainDict
)
{
if
(
mainDict
!=
null
)
{
mMainDict
=
mainDict
;
mUnigramDictionaries
.
put
(
DICT_KEY_MAIN
,
mainDict
);
mBigramDictionaries
.
put
(
DICT_KEY_MAIN
,
mainDict
);
}
mAutoCorrection
=
new
AutoCorrection
();
initPool
();
}
...
...
@@ -147,22 +153,28 @@ public class Suggest implements Dictionary.WordCallback {
* before the main dictionary, if set.
*/
public
void
setUserDictionary
(
Dictionary
userDictionary
)
{
mUserDictionary
=
userDictionary
;
if
(
userDictionary
!=
null
)
mUnigramDictionaries
.
put
(
DICT_KEY_USER
,
userDictionary
);
}
/**
* Sets an optional contacts dictionary resource to be loaded.
*/
public
void
setContactsDictionary
(
Dictionary
userDictionary
)
{
mContactsDictionary
=
userDictionary
;
public
void
setContactsDictionary
(
Dictionary
contactsDictionary
)
{
if
(
contactsDictionary
!=
null
)
{
mUnigramDictionaries
.
put
(
DICT_KEY_CONTACTS
,
contactsDictionary
);
mBigramDictionaries
.
put
(
DICT_KEY_CONTACTS
,
contactsDictionary
);
}
}
public
void
setAutoDictionary
(
Dictionary
autoDictionary
)
{
mAutoDictionary
=
autoDictionary
;
if
(
autoDictionary
!=
null
)
mUnigramDictionaries
.
put
(
DICT_KEY_AUTO
,
autoDictionary
);
}
public
void
setUserBigramDictionary
(
Dictionary
userBigramDictionary
)
{
mUserBigramDictionary
=
userBigramDictionary
;
if
(
userBigramDictionary
!=
null
)
mBigramDictionaries
.
put
(
DICT_KEY_USER_BIGRAM
,
userBigramDictionary
);
}
public
void
setAutoCorrectionThreshold
(
double
threshold
)
{
...
...
@@ -240,14 +252,8 @@ public class Suggest implements Dictionary.WordCallback {
if
(
mMainDict
!=
null
&&
mMainDict
.
isValidWord
(
lowerPrevWord
))
{
prevWordForBigram
=
lowerPrevWord
;
}
if
(
mUserBigramDictionary
!=
null
)
{
mUserBigramDictionary
.
getBigrams
(
wordComposer
,
prevWordForBigram
,
this
);
}
if
(
mContactsDictionary
!=
null
)
{
mContactsDictionary
.
getBigrams
(
wordComposer
,
prevWordForBigram
,
this
);
}
if
(
mMainDict
!=
null
)
{
mMainDict
.
getBigrams
(
wordComposer
,
prevWordForBigram
,
this
);
for
(
final
Dictionary
dictionary
:
mBigramDictionaries
.
values
())
{
dictionary
.
getBigrams
(
wordComposer
,
prevWordForBigram
,
this
);
}
char
currentChar
=
wordComposer
.
getTypedWord
().
charAt
(
0
);
char
currentCharUpper
=
Character
.
toUpperCase
(
currentChar
);
...
...
@@ -270,15 +276,13 @@ public class Suggest implements Dictionary.WordCallback {
}
else
if
(
wordComposer
.
size
()
>
1
)
{
// At second character typed, search the unigrams (scores being affected by bigrams)
if
(
mUserDictionary
!=
null
||
mContactsDictionary
!=
null
)
{
if
(
mUserDictionary
!=
null
)
{
mUserDictionary
.
getWords
(
wordComposer
,
this
);
}
if
(
mContactsDictionary
!=
null
)
{
mContactsDictionary
.
getWords
(
wordComposer
,
this
);
}
for
(
final
String
key
:
mUnigramDictionaries
.
keySet
())
{
// Skip AutoDictionary to lookup
if
(
key
.
equals
(
DICT_KEY_AUTO
))
continue
;
final
Dictionary
dictionary
=
mUnigramDictionaries
.
get
(
key
);
dictionary
.
getWords
(
wordComposer
,
this
);
}
if
(
mMainDict
!=
null
)
mMainDict
.
getWords
(
wordComposer
,
this
);
}
CharSequence
autoText
=
null
;
final
String
typedWordString
=
typedWord
==
null
?
null
:
typedWord
.
toString
();
...
...
@@ -324,8 +328,9 @@ public class Suggest implements Dictionary.WordCallback {
}
}
mAutoCorrection
.
updateAutoCorrectionStatus
(
this
,
wordComposer
,
mSuggestions
,
mPriorities
,
typedWord
,
mAutoCorrectionThreshold
,
mCorrectionMode
,
autoText
);
mAutoCorrection
.
updateAutoCorrectionStatus
(
mUnigramDictionaries
.
values
(),
wordComposer
,
mSuggestions
,
mPriorities
,
typedWord
,
mAutoCorrectionThreshold
,
mCorrectionMode
,
autoText
);
if
(
autoText
!=
null
)
{
mSuggestions
.
add
(
0
,
autoText
);
...
...
@@ -515,10 +520,11 @@ public class Suggest implements Dictionary.WordCallback {
if
(
word
==
null
||
word
.
length
()
==
0
||
mMainDict
==
null
)
{
return
false
;
}
return
mMainDict
.
isValidWord
(
word
)
||
(
mUserDictionary
!=
null
&&
mUserDictionary
.
isValidWord
(
word
))
||
(
mAutoDictionary
!=
null
&&
mAutoDictionary
.
isValidWord
(
word
))
||
(
mContactsDictionary
!=
null
&&
mContactsDictionary
.
isValidWord
(
word
));
for
(
final
Dictionary
dictionary
:
mUnigramDictionaries
.
values
())
{
if
(
dictionary
.
isValidWord
(
word
))
return
true
;
}
return
false
;
}
private
void
collectGarbage
(
ArrayList
<
CharSequence
>
suggestions
,
int
prefMaxSuggestions
)
{
...
...
@@ -539,25 +545,12 @@ public class Suggest implements Dictionary.WordCallback {
}
public
void
close
()
{
if
(
mMainDict
!=
null
)
{
mMainDict
.
close
();
mMainDict
=
null
;
}
if
(
mUserDictionary
!=
null
)
{
mUserDictionary
.
close
();
mUserDictionary
=
null
;
}
if
(
mUserBigramDictionary
!=
null
)
{
mUserBigramDictionary
.
close
();
mUserBigramDictionary
=
null
;
}
if
(
mContactsDictionary
!=
null
)
{
mContactsDictionary
.
close
();
mContactsDictionary
=
null
;
}
if
(
mAutoDictionary
!=
null
)
{
mAutoDictionary
.
close
();
mAutoDictionary
=
null
;
final
Set
<
Dictionary
>
dictionaries
=
new
HashSet
<
Dictionary
>();
dictionaries
.
addAll
(
mUnigramDictionaries
.
values
());
dictionaries
.
addAll
(
mBigramDictionaries
.
values
());
for
(
final
Dictionary
dictionary
:
dictionaries
)
{
dictionary
.
close
();
}
mMainDict
=
null
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment