• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.view.textclassifier.TextClassifier.EntityType;
23 
24 import com.android.internal.util.Preconditions;
25 
26 import java.util.List;
27 
28 /**
29  * Information about where text selection should be.
30  */
31 public final class TextSelection {
32 
33     private final int mStartIndex;
34     private final int mEndIndex;
35     @NonNull private final EntityConfidence<String> mEntityConfidence;
36     @NonNull private final List<String> mEntities;
37     @NonNull private final String mLogSource;
38 
TextSelection( int startIndex, int endIndex, @NonNull EntityConfidence<String> entityConfidence, @NonNull String logSource)39     private TextSelection(
40             int startIndex, int endIndex, @NonNull EntityConfidence<String> entityConfidence,
41             @NonNull String logSource) {
42         mStartIndex = startIndex;
43         mEndIndex = endIndex;
44         mEntityConfidence = new EntityConfidence<>(entityConfidence);
45         mEntities = mEntityConfidence.getEntities();
46         mLogSource = logSource;
47     }
48 
49     /**
50      * Returns the start index of the text selection.
51      */
getSelectionStartIndex()52     public int getSelectionStartIndex() {
53         return mStartIndex;
54     }
55 
56     /**
57      * Returns the end index of the text selection.
58      */
getSelectionEndIndex()59     public int getSelectionEndIndex() {
60         return mEndIndex;
61     }
62 
63     /**
64      * Returns the number of entities found in the classified text.
65      */
66     @IntRange(from = 0)
getEntityCount()67     public int getEntityCount() {
68         return mEntities.size();
69     }
70 
71     /**
72      * Returns the entity at the specified index. Entities are ordered from high confidence
73      * to low confidence.
74      *
75      * @throws IndexOutOfBoundsException if the specified index is out of range.
76      * @see #getEntityCount() for the number of entities available.
77      */
78     @NonNull
getEntity(int index)79     public @EntityType String getEntity(int index) {
80         return mEntities.get(index);
81     }
82 
83     /**
84      * Returns the confidence score for the specified entity. The value ranges from
85      * 0 (low confidence) to 1 (high confidence). 0 indicates that the entity was not found for the
86      * classified text.
87      */
88     @FloatRange(from = 0.0, to = 1.0)
getConfidenceScore(@ntityType String entity)89     public float getConfidenceScore(@EntityType String entity) {
90         return mEntityConfidence.getConfidenceScore(entity);
91     }
92 
93     /**
94      * Returns a tag for the source classifier used to generate this result.
95      * @hide
96      */
getSourceClassifier()97     public String getSourceClassifier() {
98         return mLogSource;
99     }
100 
101     @Override
toString()102     public String toString() {
103         return String.format("TextSelection {%d, %d, %s}",
104                 mStartIndex, mEndIndex, mEntityConfidence);
105     }
106 
107     /**
108      * Builder used to build {@link TextSelection} objects.
109      */
110     public static final class Builder {
111 
112         private final int mStartIndex;
113         private final int mEndIndex;
114         @NonNull private final EntityConfidence<String> mEntityConfidence =
115                 new EntityConfidence<>();
116         @NonNull private String mLogSource = "";
117 
118         /**
119          * Creates a builder used to build {@link TextSelection} objects.
120          *
121          * @param startIndex the start index of the text selection.
122          * @param endIndex the end index of the text selection. Must be greater than startIndex
123          */
Builder(@ntRangefrom = 0) int startIndex, @IntRange(from = 0) int endIndex)124         public Builder(@IntRange(from = 0) int startIndex, @IntRange(from = 0) int endIndex) {
125             Preconditions.checkArgument(startIndex >= 0);
126             Preconditions.checkArgument(endIndex > startIndex);
127             mStartIndex = startIndex;
128             mEndIndex = endIndex;
129         }
130 
131         /**
132          * Sets an entity type for the classified text and assigns a confidence score.
133          *
134          * @param confidenceScore a value from 0 (low confidence) to 1 (high confidence).
135          *      0 implies the entity does not exist for the classified text.
136          *      Values greater than 1 are clamped to 1.
137          */
setEntityType( @onNull @ntityType String type, @FloatRange(from = 0.0, to = 1.0) float confidenceScore)138         public Builder setEntityType(
139                 @NonNull @EntityType String type,
140                 @FloatRange(from = 0.0, to = 1.0) float confidenceScore) {
141             mEntityConfidence.setEntityType(type, confidenceScore);
142             return this;
143         }
144 
145         /**
146          * Sets a tag for the source classifier used to generate this result.
147          * @hide
148          */
setLogSource(@onNull String logSource)149         Builder setLogSource(@NonNull String logSource) {
150             mLogSource = Preconditions.checkNotNull(logSource);
151             return this;
152         }
153 
154         /**
155          * Builds and returns {@link TextSelection} object.
156          */
build()157         public TextSelection build() {
158             return new TextSelection(mStartIndex, mEndIndex, mEntityConfidence, mLogSource);
159         }
160     }
161 }
162