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 package android.view.textclassifier;
     17 
     18 /**
     19  *  Java wrapper for LangId native library interface.
     20  *  This class is used to detect languages in text.
     21  */
     22 final class LangId {
     23 
     24     static {
     25         System.loadLibrary("textclassifier");
     26     }
     27 
     28     private final long mModelPtr;
     29 
     30     /**
     31      * Creates a new instance of LangId predictor, using the provided model image.
     32      */
     33     LangId(int fd) {
     34         mModelPtr = nativeNew(fd);
     35     }
     36 
     37     /**
     38      * Detects the language for given text.
     39      */
     40     public ClassificationResult[] findLanguages(String text) {
     41         return nativeFindLanguages(mModelPtr, text);
     42     }
     43 
     44     /**
     45      * Frees up the allocated memory.
     46      */
     47     public void close() {
     48         nativeClose(mModelPtr);
     49     }
     50 
     51     private static native long nativeNew(int fd);
     52 
     53     private static native ClassificationResult[] nativeFindLanguages(
     54             long context, String text);
     55 
     56     private static native void nativeClose(long context);
     57 
     58     /** Classification result for findLanguage method. */
     59     static final class ClassificationResult {
     60         final String mLanguage;
     61         /** float range: 0 - 1 */
     62         final float mScore;
     63 
     64         ClassificationResult(String language, float score) {
     65             mLanguage = language;
     66             mScore = score;
     67         }
     68     }
     69 }
     70