Merge "Adds support for a fall-back model when the language-specific model is not found." into pi-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
f9593d62b3
@@ -355,12 +355,10 @@ public final class TextClassifierImpl implements TextClassifier {
|
||||
final List<Locale.LanguageRange> languageRangeList = Locale.LanguageRange.parse(languages);
|
||||
|
||||
ModelFile bestModel = null;
|
||||
int bestModelVersion = -1;
|
||||
for (ModelFile model : listAllModelsLocked()) {
|
||||
if (model.isAnyLanguageSupported(languageRangeList)) {
|
||||
if (model.getVersion() >= bestModelVersion) {
|
||||
if (model.isPreferredTo(bestModel)) {
|
||||
bestModel = model;
|
||||
bestModelVersion = model.getVersion();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -482,6 +480,7 @@ public final class TextClassifierImpl implements TextClassifier {
|
||||
private final String mName;
|
||||
private final int mVersion;
|
||||
private final List<Locale> mSupportedLocales;
|
||||
private final boolean mLanguageIndependent;
|
||||
|
||||
/** Returns null if the path did not point to a compatible model. */
|
||||
static @Nullable ModelFile fromPath(String path) {
|
||||
@@ -496,12 +495,14 @@ public final class TextClassifierImpl implements TextClassifier {
|
||||
Log.d(DEFAULT_LOG_TAG, "Ignoring " + file.getAbsolutePath());
|
||||
return null;
|
||||
}
|
||||
final boolean languageIndependent = supportedLocalesStr.equals("*");
|
||||
final List<Locale> supportedLocales = new ArrayList<>();
|
||||
for (String langTag : supportedLocalesStr.split(",")) {
|
||||
supportedLocales.add(Locale.forLanguageTag(langTag));
|
||||
}
|
||||
closeAndLogError(modelFd);
|
||||
return new ModelFile(path, file.getName(), version, supportedLocales);
|
||||
return new ModelFile(path, file.getName(), version, supportedLocales,
|
||||
languageIndependent);
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(DEFAULT_LOG_TAG, "Failed to peek " + file.getAbsolutePath(), e);
|
||||
return null;
|
||||
@@ -525,7 +526,7 @@ public final class TextClassifierImpl implements TextClassifier {
|
||||
|
||||
/** Returns whether the language supports any language in the given ranges. */
|
||||
boolean isAnyLanguageSupported(List<Locale.LanguageRange> languageRanges) {
|
||||
return Locale.lookup(languageRanges, mSupportedLocales) != null;
|
||||
return mLanguageIndependent || Locale.lookup(languageRanges, mSupportedLocales) != null;
|
||||
}
|
||||
|
||||
/** All locales supported by the model. */
|
||||
@@ -533,6 +534,25 @@ public final class TextClassifierImpl implements TextClassifier {
|
||||
return Collections.unmodifiableList(mSupportedLocales);
|
||||
}
|
||||
|
||||
public boolean isPreferredTo(ModelFile model) {
|
||||
// A model is preferred to no model.
|
||||
if (model == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// A language-specific model is preferred to a language independent
|
||||
// model.
|
||||
if (!mLanguageIndependent && model.mLanguageIndependent) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// A higher-version model is preferred.
|
||||
if (getVersion() > model.getVersion()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (this == other) {
|
||||
@@ -555,11 +575,13 @@ public final class TextClassifierImpl implements TextClassifier {
|
||||
mPath, mName, mVersion, localesJoiner.toString());
|
||||
}
|
||||
|
||||
private ModelFile(String path, String name, int version, List<Locale> supportedLocales) {
|
||||
private ModelFile(String path, String name, int version, List<Locale> supportedLocales,
|
||||
boolean languageIndependent) {
|
||||
mPath = path;
|
||||
mName = name;
|
||||
mVersion = version;
|
||||
mSupportedLocales = supportedLocales;
|
||||
mLanguageIndependent = languageIndependent;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user