import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.Objects;
/**
* Information for generating a widget to handle classified text.
@Override
public String toString() {
return String.format(Locale.US,
- "TextClassification {text=%s, entities=%s, actions=%s, id=%s}",
- mText, mEntityConfidence, mActions, mId);
+ "TextClassification {text=%s, entities=%s, actions=%s, id=%s, extras=%s}",
+ mText, mEntityConfidence, mActions, mId, mExtras);
}
/**
private Bundle buildExtras() {
final Bundle extras = mExtras == null ? new Bundle() : mExtras.deepCopy();
- if (!mActionIntents.isEmpty()) {
+ if (mActionIntents.stream().anyMatch(Objects::nonNull)) {
ExtrasUtils.putActionsIntents(extras, mActionIntents);
}
if (mForeignLanguageExtra != null) {
refTime.getZone().getId(),
localesString),
mContext,
- // TODO: Pass the locale list once it is supported in
- // native side.
- LocaleList.getDefault().get(0).toLanguageTag()
+ getResourceLocaleString()
);
if (results.length > 0) {
return createClassificationResult(
nativeConversation,
null,
mContext,
- // TODO: Pass the locale list once it is supported in native side.
- LocaleList.getDefault().get(0).toLanguageTag());
+ getResourceLocaleString());
return createConversationActionResult(request, nativeSuggestions);
} catch (Throwable t) {
// Avoid throwing from this method. Log the error.
TextLanguage textLanguage = detectLanguage(request);
int localeHypothesisCount = textLanguage.getLocaleHypothesisCount();
List<String> languageTags = new ArrayList<>();
- // TODO: Reconsider this and probably make the score threshold configurable.
for (int i = 0; i < localeHypothesisCount; i++) {
ULocale locale = textLanguage.getLocale(i);
- if (textLanguage.getConfidenceScore(locale) < 0.5) {
+ if (textLanguage.getConfidenceScore(locale) < getForeignLanguageThreshold()) {
break;
}
languageTags.add(locale.toLanguageTag());
}
}
- final float foreignTextThreshold = mSettings.getLangIdThresholdOverride() >= 0
- ? mSettings.getLangIdThresholdOverride()
- : 0.5f /* TODO: Load this from the langId model. */;
- final Bundle foreignLanguageBundle =
- detectForeignLanguage(classifiedText, foreignTextThreshold);
+ final Bundle foreignLanguageBundle = detectForeignLanguage(classifiedText);
builder.setForeignLanguageExtra(foreignLanguageBundle);
boolean isPrimaryAction = true;
- final ArrayList<Intent> sourceIntents = new ArrayList<>();
List<LabeledIntent> labeledIntents = mIntentFactory.create(
mContext,
classifiedText,
/**
* Returns a bundle with the language and confidence score if it finds the text to be
- * in a foreign language. Otherwise returns null.
+ * in a foreign language. Otherwise returns null. This algorithm defines what the system thinks
+ * is a foreign language.
*/
+ // TODO: Revisit this algorithm.
+ // TODO: Consider making this public API.
@Nullable
- private Bundle detectForeignLanguage(String text, float threshold) {
- if (threshold > 1) {
- return null;
- }
-
- // TODO: Revisit this algorithm.
+ private Bundle detectForeignLanguage(String text) {
try {
+ final float threshold = getForeignLanguageThreshold();
+ if (threshold > 1) {
+ Log.v(LOG_TAG, "Foreign language detection disabled.");
+ return null;
+ }
+
final LangIdModel langId = getLangIdImpl();
final LangIdModel.LanguageResult[] langResults = langId.detectLanguages(text);
if (langResults.length <= 0) {
if (highestScoringResult.getScore() < threshold) {
return null;
}
- // TODO: Remove
- Log.d(LOG_TAG, String.format("Language detected: <%s:%s>",
+
+ Log.v(LOG_TAG, String.format("Language detected: <%s:%s>",
highestScoringResult.getLanguage(), highestScoringResult.getScore()));
final Locale detected = new Locale(highestScoringResult.getLanguage());
return null;
}
+ private float getForeignLanguageThreshold() {
+ try {
+ return mSettings.getLangIdThresholdOverride() >= 0
+ ? mSettings.getLangIdThresholdOverride()
+ : getLangIdImpl().getTranslateThreshold();
+ } catch (FileNotFoundException e) {
+ final float defaultThreshold = 0.5f;
+ Log.v(LOG_TAG, "Using default foreign language threshold: " + defaultThreshold);
+ return defaultThreshold;
+ }
+ }
+
@Override
public void dump(@NonNull IndentingPrintWriter printWriter) {
synchronized (mLock) {
}
/**
+ * Returns the locale string for the current resources configuration.
+ */
+ private String getResourceLocaleString() {
+ // TODO: Pass the locale list once it is supported in native side.
+ try {
+ return mContext.getResources().getConfiguration().getLocales().get(0).toLanguageTag();
+ } catch (NullPointerException e) {
+ // NPE is unexpected. Erring on the side of caution.
+ return LocaleList.getDefault().get(0).toLanguageTag();
+ }
+ }
+
+ /**
* Helper class to store the information from which RemoteActions are built.
*/
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)