Home | History | Annotate | Download | only in textclassifier
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.view.textclassifier;
     18 
     19 import android.annotation.FloatRange;
     20 import android.annotation.IntRange;
     21 import android.annotation.NonNull;
     22 import android.annotation.Nullable;
     23 
     24 import com.android.internal.util.Preconditions;
     25 
     26 import java.util.List;
     27 import java.util.Locale;
     28 
     29 /**
     30  * Specifies detected languages for a section of text indicated by a start and end index.
     31  * @hide
     32  */
     33 public final class TextLanguage {
     34 
     35     private final int mStartIndex;
     36     private final int mEndIndex;
     37     @NonNull private final EntityConfidence<Locale> mLanguageConfidence;
     38     @NonNull private final List<Locale> mLanguages;
     39 
     40     private TextLanguage(
     41             int startIndex, int endIndex, @NonNull EntityConfidence<Locale> languageConfidence) {
     42         mStartIndex = startIndex;
     43         mEndIndex = endIndex;
     44         mLanguageConfidence = new EntityConfidence<>(languageConfidence);
     45         mLanguages = mLanguageConfidence.getEntities();
     46     }
     47 
     48     /**
     49      * Returns the start index of the detected languages in the text provided to generate this
     50      * object.
     51      */
     52     public int getStartIndex() {
     53         return mStartIndex;
     54     }
     55 
     56     /**
     57      * Returns the end index of the detected languages in the text provided to generate this object.
     58      */
     59     public int getEndIndex() {
     60         return mEndIndex;
     61     }
     62 
     63     /**
     64      * Returns the number of languages found in the classified text.
     65      */
     66     @IntRange(from = 0)
     67     public int getLanguageCount() {
     68         return mLanguages.size();
     69     }
     70 
     71     /**
     72      * Returns the language locale at the specified index.
     73      * Language locales are ordered from high confidence to low confidence.
     74      *
     75      * @throws IndexOutOfBoundsException if the specified index is out of range.
     76      * @see #getLanguageCount() for the number of language locales available.
     77      */
     78     @NonNull
     79     public Locale getLanguage(int index) {
     80         return mLanguages.get(index);
     81     }
     82 
     83     /**
     84      * Returns the confidence score for the specified language. The value ranges from
     85      * 0 (low confidence) to 1 (high confidence). 0 indicates that the language was
     86      * not found for the classified text.
     87      */
     88     @FloatRange(from = 0.0, to = 1.0)
     89     public float getConfidenceScore(@Nullable Locale language) {
     90         return mLanguageConfidence.getConfidenceScore(language);
     91     }
     92 
     93     @Override
     94     public String toString() {
     95         return String.format("TextLanguage {%d, %d, %s}",
     96                 mStartIndex, mEndIndex, mLanguageConfidence);
     97     }
     98 
     99     /**
    100      * Builder to build {@link TextLanguage} objects.
    101      */
    102     public static final class Builder {
    103 
    104         private final int mStartIndex;
    105         private final int mEndIndex;
    106         @NonNull private final EntityConfidence<Locale> mLanguageConfidence =
    107                 new EntityConfidence<>();
    108 
    109         /**
    110          * Creates a builder to build {@link TextLanguage} objects.
    111          *
    112          * @param startIndex the start index of the detected languages in the text provided
    113          *      to generate the result
    114          * @param endIndex the end index of the detected languages in the text provided
    115          *      to generate the result. Must be greater than startIndex
    116          */
    117         public Builder(@IntRange(from = 0) int startIndex, @IntRange(from = 0) int endIndex) {
    118             Preconditions.checkArgument(startIndex >= 0);
    119             Preconditions.checkArgument(endIndex > startIndex);
    120             mStartIndex = startIndex;
    121             mEndIndex = endIndex;
    122         }
    123 
    124         /**
    125          * Sets a language locale with the associated confidence score.
    126          */
    127         public Builder setLanguage(
    128                 @NonNull Locale locale, @FloatRange(from = 0.0, to = 1.0) float confidenceScore) {
    129             mLanguageConfidence.setEntityType(locale, confidenceScore);
    130             return this;
    131         }
    132 
    133         /**
    134          * Builds and returns a {@link TextLanguage}.
    135          */
    136         public TextLanguage build() {
    137             return new TextLanguage(mStartIndex, mEndIndex, mLanguageConfidence);
    138         }
    139     }
    140 }
    141