Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 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 | |
Abodunrinwa Toki | f67f0a5 | 2017-02-09 22:41:58 +0000 | [diff] [blame] | 17 | package android.view.textclassifier; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 18 | |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 20 | import android.content.res.AssetFileDescriptor; |
| 21 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 22 | /** |
| 23 | * Java wrapper for SmartSelection native library interface. |
| 24 | * This library is used for detecting entities in text. |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 25 | */ |
Abodunrinwa Toki | f67f0a5 | 2017-02-09 22:41:58 +0000 | [diff] [blame] | 26 | final class SmartSelection { |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 27 | |
| 28 | static { |
Abodunrinwa Toki | 099ff11 | 2017-03-02 20:43:01 +0000 | [diff] [blame] | 29 | System.loadLibrary("textclassifier"); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Abodunrinwa Toki | d2d1399 | 2017-03-24 21:43:13 +0000 | [diff] [blame] | 32 | /** Hints the classifier that this may be a url. */ |
| 33 | static final int HINT_FLAG_URL = 0x01; |
| 34 | /** Hints the classifier that this may be an email. */ |
| 35 | static final int HINT_FLAG_EMAIL = 0x02; |
| 36 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 37 | private final long mCtx; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new instance of SmartSelect predictor, using the provided model image, |
| 41 | * given as a file descriptor. |
| 42 | */ |
Abodunrinwa Toki | f67f0a5 | 2017-02-09 22:41:58 +0000 | [diff] [blame] | 43 | SmartSelection(int fd) { |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 44 | mCtx = nativeNew(fd); |
| 45 | } |
| 46 | |
| 47 | /** |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 48 | * Creates a new instance of SmartSelect predictor, using the provided model image, given as a |
| 49 | * file path. |
| 50 | */ |
| 51 | SmartSelection(String path) { |
| 52 | mCtx = nativeNewFromPath(path); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Creates a new instance of SmartSelect predictor, using the provided model image, given as an |
| 57 | * AssetFileDescriptor. |
| 58 | */ |
| 59 | SmartSelection(AssetFileDescriptor afd) { |
| 60 | mCtx = nativeNewFromAssetFileDescriptor(afd, afd.getStartOffset(), afd.getLength()); |
| 61 | if (mCtx == 0L) { |
| 62 | throw new IllegalArgumentException( |
| 63 | "Couldn't initialize TC from given AssetFileDescriptor"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 68 | * Given a string context and current selection, computes the SmartSelection suggestion. |
| 69 | * |
| 70 | * The begin and end are character indices into the context UTF8 string. selectionBegin is the |
| 71 | * character index where the selection begins, and selectionEnd is the index of one character |
| 72 | * past the selection span. |
| 73 | * |
| 74 | * The return value is an array of two ints: suggested selection beginning and end, with the |
| 75 | * same semantics as the input selectionBeginning and selectionEnd. |
| 76 | */ |
| 77 | public int[] suggest(String context, int selectionBegin, int selectionEnd) { |
| 78 | return nativeSuggest(mCtx, context, selectionBegin, selectionEnd); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Given a string context and current selection, classifies the type of the selected text. |
| 83 | * |
| 84 | * The begin and end params are character indices in the context string. |
| 85 | * |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 86 | * Returns an array of ClassificationResult objects with the probability |
| 87 | * scores for different collections. |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 88 | */ |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 89 | public ClassificationResult[] classifyText( |
Abodunrinwa Toki | d2d1399 | 2017-03-24 21:43:13 +0000 | [diff] [blame] | 90 | String context, int selectionBegin, int selectionEnd, int hintFlags) { |
Abodunrinwa Toki | b5ab259 | 2017-03-27 21:09:23 +0100 | [diff] [blame] | 91 | return nativeClassifyText(mCtx, context, selectionBegin, selectionEnd, hintFlags); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 95 | * Annotates given input text. Every word of the input is a part of some annotation. |
| 96 | * The annotations are sorted by their position in the context string. |
| 97 | * The annotations do not overlap. |
| 98 | */ |
| 99 | public AnnotatedSpan[] annotate(String text) { |
| 100 | return nativeAnnotate(mCtx, text); |
| 101 | } |
| 102 | |
| 103 | /** |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 104 | * Frees up the allocated memory. |
| 105 | */ |
| 106 | public void close() { |
| 107 | nativeClose(mCtx); |
| 108 | } |
| 109 | |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 110 | /** |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 111 | * Returns a comma separated list of locales supported by the model as BCP 47 tags. |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 112 | */ |
Jan Althaus | ef0156d | 2018-01-29 19:28:41 +0100 | [diff] [blame] | 113 | public static String getLanguages(int fd) { |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 114 | return nativeGetLanguage(fd); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the version of the model. |
| 119 | */ |
| 120 | public static int getVersion(int fd) { |
| 121 | return nativeGetVersion(fd); |
| 122 | } |
| 123 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 124 | private static native long nativeNew(int fd); |
| 125 | |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 126 | private static native long nativeNewFromPath(String path); |
| 127 | |
| 128 | private static native long nativeNewFromAssetFileDescriptor(AssetFileDescriptor afd, |
| 129 | long offset, long size); |
| 130 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 131 | private static native int[] nativeSuggest( |
| 132 | long context, String text, int selectionBegin, int selectionEnd); |
| 133 | |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 134 | private static native ClassificationResult[] nativeClassifyText( |
Abodunrinwa Toki | b5ab259 | 2017-03-27 21:09:23 +0100 | [diff] [blame] | 135 | long context, String text, int selectionBegin, int selectionEnd, int hintFlags); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 136 | |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 137 | private static native AnnotatedSpan[] nativeAnnotate(long context, String text); |
| 138 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 139 | private static native void nativeClose(long context); |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 140 | |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 141 | private static native String nativeGetLanguage(int fd); |
| 142 | |
| 143 | private static native int nativeGetVersion(int fd); |
| 144 | |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 145 | /** Classification result for classifyText method. */ |
| 146 | static final class ClassificationResult { |
| 147 | final String mCollection; |
| 148 | /** float range: 0 - 1 */ |
| 149 | final float mScore; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 150 | @Nullable final DatetimeParseResult mDatetime; |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 151 | |
| 152 | ClassificationResult(String collection, float score) { |
| 153 | mCollection = collection; |
| 154 | mScore = score; |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 155 | mDatetime = null; |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 156 | } |
Jan Althaus | 705b9e9 | 2018-01-22 18:22:29 +0100 | [diff] [blame] | 157 | |
| 158 | ClassificationResult(String collection, float score, DatetimeParseResult datetime) { |
| 159 | mCollection = collection; |
| 160 | mScore = score; |
| 161 | mDatetime = datetime; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** Parsed date information for the classification result. */ |
| 166 | static final class DatetimeParseResult { |
| 167 | long mMsSinceEpoch; |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 168 | } |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 169 | |
| 170 | /** Represents a result of Annotate call. */ |
| 171 | public static final class AnnotatedSpan { |
| 172 | final int mStartIndex; |
| 173 | final int mEndIndex; |
| 174 | final ClassificationResult[] mClassification; |
| 175 | |
| 176 | AnnotatedSpan(int startIndex, int endIndex, ClassificationResult[] classification) { |
| 177 | mStartIndex = startIndex; |
| 178 | mEndIndex = endIndex; |
| 179 | mClassification = classification; |
| 180 | } |
| 181 | |
| 182 | public int getStartIndex() { |
| 183 | return mStartIndex; |
| 184 | } |
| 185 | |
| 186 | public int getEndIndex() { |
| 187 | return mEndIndex; |
| 188 | } |
| 189 | |
| 190 | public ClassificationResult[] getClassification() { |
| 191 | return mClassification; |
| 192 | } |
| 193 | } |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 194 | } |