summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/java/android/view/textclassifier/LangId.java69
-rw-r--r--core/java/android/view/textclassifier/TextClassificationManager.java58
-rw-r--r--core/java/android/view/textclassifier/TextLanguage.java140
-rw-r--r--core/tests/coretests/src/android/view/textclassifier/TextClassificationManagerTest.java43
4 files changed, 0 insertions, 310 deletions
diff --git a/core/java/android/view/textclassifier/LangId.java b/core/java/android/view/textclassifier/LangId.java
deleted file mode 100644
index 23c7842780a9..000000000000
--- a/core/java/android/view/textclassifier/LangId.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package android.view.textclassifier;
-
-/**
- * Java wrapper for LangId native library interface.
- * This class is used to detect languages in text.
- */
-final class LangId {
-
- static {
- System.loadLibrary("textclassifier");
- }
-
- private final long mModelPtr;
-
- /**
- * Creates a new instance of LangId predictor, using the provided model image.
- */
- LangId(int fd) {
- mModelPtr = nativeNew(fd);
- }
-
- /**
- * Detects the language for given text.
- */
- public ClassificationResult[] findLanguages(String text) {
- return nativeFindLanguages(mModelPtr, text);
- }
-
- /**
- * Frees up the allocated memory.
- */
- public void close() {
- nativeClose(mModelPtr);
- }
-
- private static native long nativeNew(int fd);
-
- private static native ClassificationResult[] nativeFindLanguages(
- long context, String text);
-
- private static native void nativeClose(long context);
-
- /** Classification result for findLanguage method. */
- static final class ClassificationResult {
- final String mLanguage;
- /** float range: 0 - 1 */
- final float mScore;
-
- ClassificationResult(String language, float score) {
- mLanguage = language;
- mScore = score;
- }
- }
-}
diff --git a/core/java/android/view/textclassifier/TextClassificationManager.java b/core/java/android/view/textclassifier/TextClassificationManager.java
index efc88e23fa67..d7b07761a653 100644
--- a/core/java/android/view/textclassifier/TextClassificationManager.java
+++ b/core/java/android/view/textclassifier/TextClassificationManager.java
@@ -16,37 +16,22 @@
package android.view.textclassifier;
-import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemService;
import android.content.Context;
-import android.os.ParcelFileDescriptor;
-import android.util.Log;
import com.android.internal.util.Preconditions;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.Locale;
-
/**
* Interface to the text classification service.
*/
@SystemService(Context.TEXT_CLASSIFICATION_SERVICE)
public final class TextClassificationManager {
- private static final String LOG_TAG = "TextClassificationManager";
-
private final Object mTextClassifierLock = new Object();
- private final Object mLangIdLock = new Object();
private final Context mContext;
- private ParcelFileDescriptor mLangIdFd;
private TextClassifier mTextClassifier;
- private LangId mLangId;
/** @hide */
public TextClassificationManager(Context context) {
@@ -75,47 +60,4 @@ public final class TextClassificationManager {
mTextClassifier = textClassifier;
}
}
-
- /**
- * Returns information containing languages that were detected in the provided text.
- * This is a blocking operation you should avoid calling it on the UI thread.
- *
- * @throws IllegalArgumentException if text is null
- * @hide
- */
- public List<TextLanguage> detectLanguages(@NonNull CharSequence text) {
- Preconditions.checkArgument(text != null);
- try {
- if (text.length() > 0) {
- final LangId.ClassificationResult[] results =
- getLanguageDetector().findLanguages(text.toString());
- final TextLanguage.Builder tlBuilder = new TextLanguage.Builder(0, text.length());
- final int size = results.length;
- for (int i = 0; i < size; i++) {
- tlBuilder.setLanguage(
- new Locale.Builder().setLanguageTag(results[i].mLanguage).build(),
- results[i].mScore);
- }
-
- return Collections.unmodifiableList(Arrays.asList(tlBuilder.build()));
- }
- } catch (Throwable t) {
- // Avoid throwing from this method. Log the error.
- Log.e(LOG_TAG, "Error detecting languages for text. Returning empty result.", t);
- }
- // Getting here means something went wrong. Return an empty result.
- return Collections.emptyList();
- }
-
- private LangId getLanguageDetector() throws FileNotFoundException {
- synchronized (mLangIdLock) {
- if (mLangId == null) {
- mLangIdFd = ParcelFileDescriptor.open(
- new File("/etc/textclassifier/textclassifier.langid.model"),
- ParcelFileDescriptor.MODE_READ_ONLY);
- mLangId = new LangId(mLangIdFd.getFd());
- }
- return mLangId;
- }
- }
}
diff --git a/core/java/android/view/textclassifier/TextLanguage.java b/core/java/android/view/textclassifier/TextLanguage.java
deleted file mode 100644
index 209813a2beac..000000000000
--- a/core/java/android/view/textclassifier/TextLanguage.java
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.view.textclassifier;
-
-import android.annotation.FloatRange;
-import android.annotation.IntRange;
-import android.annotation.NonNull;
-import android.annotation.Nullable;
-
-import com.android.internal.util.Preconditions;
-
-import java.util.List;
-import java.util.Locale;
-
-/**
- * Specifies detected languages for a section of text indicated by a start and end index.
- * @hide
- */
-public final class TextLanguage {
-
- private final int mStartIndex;
- private final int mEndIndex;
- @NonNull private final EntityConfidence<Locale> mLanguageConfidence;
- @NonNull private final List<Locale> mLanguages;
-
- private TextLanguage(
- int startIndex, int endIndex, @NonNull EntityConfidence<Locale> languageConfidence) {
- mStartIndex = startIndex;
- mEndIndex = endIndex;
- mLanguageConfidence = new EntityConfidence<>(languageConfidence);
- mLanguages = mLanguageConfidence.getEntities();
- }
-
- /**
- * Returns the start index of the detected languages in the text provided to generate this
- * object.
- */
- public int getStartIndex() {
- return mStartIndex;
- }
-
- /**
- * Returns the end index of the detected languages in the text provided to generate this object.
- */
- public int getEndIndex() {
- return mEndIndex;
- }
-
- /**
- * Returns the number of languages found in the classified text.
- */
- @IntRange(from = 0)
- public int getLanguageCount() {
- return mLanguages.size();
- }
-
- /**
- * Returns the language locale at the specified index.
- * Language locales are ordered from high confidence to low confidence.
- *
- * @throws IndexOutOfBoundsException if the specified index is out of range.
- * @see #getLanguageCount() for the number of language locales available.
- */
- @NonNull
- public Locale getLanguage(int index) {
- return mLanguages.get(index);
- }
-
- /**
- * Returns the confidence score for the specified language. The value ranges from
- * 0 (low confidence) to 1 (high confidence). 0 indicates that the language was
- * not found for the classified text.
- */
- @FloatRange(from = 0.0, to = 1.0)
- public float getConfidenceScore(@Nullable Locale language) {
- return mLanguageConfidence.getConfidenceScore(language);
- }
-
- @Override
- public String toString() {
- return String.format("TextLanguage {%d, %d, %s}",
- mStartIndex, mEndIndex, mLanguageConfidence);
- }
-
- /**
- * Builder to build {@link TextLanguage} objects.
- */
- public static final class Builder {
-
- private final int mStartIndex;
- private final int mEndIndex;
- @NonNull private final EntityConfidence<Locale> mLanguageConfidence =
- new EntityConfidence<>();
-
- /**
- * Creates a builder to build {@link TextLanguage} objects.
- *
- * @param startIndex the start index of the detected languages in the text provided
- * to generate the result
- * @param endIndex the end index of the detected languages in the text provided
- * to generate the result. Must be greater than startIndex
- */
- public Builder(@IntRange(from = 0) int startIndex, @IntRange(from = 0) int endIndex) {
- Preconditions.checkArgument(startIndex >= 0);
- Preconditions.checkArgument(endIndex > startIndex);
- mStartIndex = startIndex;
- mEndIndex = endIndex;
- }
-
- /**
- * Sets a language locale with the associated confidence score.
- */
- public Builder setLanguage(
- @NonNull Locale locale, @FloatRange(from = 0.0, to = 1.0) float confidenceScore) {
- mLanguageConfidence.setEntityType(locale, confidenceScore);
- return this;
- }
-
- /**
- * Builds and returns a {@link TextLanguage}.
- */
- public TextLanguage build() {
- return new TextLanguage(mStartIndex, mEndIndex, mLanguageConfidence);
- }
- }
-}
diff --git a/core/tests/coretests/src/android/view/textclassifier/TextClassificationManagerTest.java b/core/tests/coretests/src/android/view/textclassifier/TextClassificationManagerTest.java
index 29447edfddd2..2a6c22e21487 100644
--- a/core/tests/coretests/src/android/view/textclassifier/TextClassificationManagerTest.java
+++ b/core/tests/coretests/src/android/view/textclassifier/TextClassificationManagerTest.java
@@ -32,9 +32,6 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import java.util.List;
-import java.util.Locale;
-
@SmallTest
@RunWith(AndroidJUnit4.class)
public class TextClassificationManagerTest {
@@ -177,20 +174,6 @@ public class TextClassificationManagerTest {
}
@Test
- public void testLanguageDetection() {
- if (isTextClassifierDisabled()) return;
-
- String text = "This is a piece of English text";
- assertThat(mTcm.detectLanguages(text), isDetectedLanguage("en"));
-
- text = "Das ist ein deutscher Text";
- assertThat(mTcm.detectLanguages(text), isDetectedLanguage("de"));
-
- text = "これは日本語のテキストです";
- assertThat(mTcm.detectLanguages(text), isDetectedLanguage("ja"));
- }
-
- @Test
public void testSetTextClassifier() {
TextClassifier classifier = mock(TextClassifier.class);
mTcm.setTextClassifier(classifier);
@@ -270,30 +253,4 @@ public class TextClassificationManagerTest {
}
};
}
-
- private static Matcher<List<TextLanguage>> isDetectedLanguage(final String language) {
- return new BaseMatcher<List<TextLanguage>>() {
- @Override
- public boolean matches(Object o) {
- if (o instanceof List) {
- List languages = (List) o;
- if (!languages.isEmpty()) {
- Object o1 = languages.get(0);
- if (o1 instanceof TextLanguage) {
- TextLanguage lang = (TextLanguage) o1;
- return lang.getLanguageCount() > 0
- && new Locale(language).getLanguage()
- .equals(lang.getLanguage(0).getLanguage());
- }
- }
- }
- return false;
- }
-
- @Override
- public void describeTo(Description description) {
- description.appendValue(String.format("%s", language));
- }
- };
- }
}