1 /* 2 * Copyright (C) 2018 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 com.google.android.textclassifier; 18 19 import android.content.res.AssetFileDescriptor; 20 import java.util.concurrent.atomic.AtomicBoolean; 21 22 /** 23 * Java wrapper for LangId native library interface. This class is used to detect languages in text. 24 * 25 * @hide 26 */ 27 public final class LangIdModel implements AutoCloseable { 28 private final AtomicBoolean isClosed = new AtomicBoolean(false); 29 30 static { 31 System.loadLibrary("textclassifier"); 32 } 33 34 private long modelPtr; 35 36 /** Creates a new instance of LangId predictor, using the provided model image. */ LangIdModel(int fd)37 public LangIdModel(int fd) { 38 modelPtr = nativeNew(fd); 39 if (modelPtr == 0L) { 40 throw new IllegalArgumentException("Couldn't initialize LangId from given file descriptor."); 41 } 42 } 43 44 /** Creates a new instance of LangId predictor, using the provided model image. */ LangIdModel(String modelPath)45 public LangIdModel(String modelPath) { 46 modelPtr = nativeNewFromPath(modelPath); 47 if (modelPtr == 0L) { 48 throw new IllegalArgumentException("Couldn't initialize LangId from given file."); 49 } 50 } 51 52 /** 53 * Creates a new instance of LangId predictor, using the provided model image, given as an {@link 54 * AssetFileDescriptor}. 55 */ LangIdModel(AssetFileDescriptor assetFileDescriptor)56 public LangIdModel(AssetFileDescriptor assetFileDescriptor) { 57 modelPtr = 58 nativeNewWithOffset( 59 assetFileDescriptor.getParcelFileDescriptor().getFd(), 60 assetFileDescriptor.getStartOffset(), 61 assetFileDescriptor.getLength()); 62 if (modelPtr == 0L) { 63 throw new IllegalArgumentException("Couldn't initialize LangId from asset file descriptor."); 64 } 65 } 66 67 /** Creates a new instance of LangId predictor, using the provided model image. */ LangIdModel(int fd, long offset, long size)68 public LangIdModel(int fd, long offset, long size) { 69 modelPtr = nativeNewWithOffset(fd, offset, size); 70 if (modelPtr == 0L) { 71 throw new IllegalArgumentException("Couldn't initialize LangId from given file descriptor."); 72 } 73 } 74 75 /** Detects the languages for given text. */ detectLanguages(String text)76 public LanguageResult[] detectLanguages(String text) { 77 return nativeDetectLanguages(modelPtr, text); 78 } 79 80 /** Frees up the allocated memory. */ 81 @Override close()82 public void close() { 83 if (isClosed.compareAndSet(false, true)) { 84 nativeClose(modelPtr); 85 modelPtr = 0L; 86 } 87 } 88 89 @Override finalize()90 protected void finalize() throws Throwable { 91 try { 92 close(); 93 } finally { 94 super.finalize(); 95 } 96 } 97 98 /** Result for detectLanguages method. */ 99 public static final class LanguageResult { 100 final String mLanguage; 101 final float mScore; 102 LanguageResult(String language, float score)103 LanguageResult(String language, float score) { 104 mLanguage = language; 105 mScore = score; 106 } 107 getLanguage()108 public final String getLanguage() { 109 return mLanguage; 110 } 111 getScore()112 public final float getScore() { 113 return mScore; 114 } 115 } 116 117 /** Returns the version of the LangId model used. */ getVersion()118 public int getVersion() { 119 return nativeGetVersion(modelPtr); 120 } 121 getVersion(int fd)122 public static int getVersion(int fd) { 123 return nativeGetVersionFromFd(fd); 124 } 125 126 /** Returns the version of the model. */ getVersion(AssetFileDescriptor assetFileDescriptor)127 public static int getVersion(AssetFileDescriptor assetFileDescriptor) { 128 return nativeGetVersionWithOffset( 129 assetFileDescriptor.getParcelFileDescriptor().getFd(), 130 assetFileDescriptor.getStartOffset(), 131 assetFileDescriptor.getLength()); 132 } 133 getLangIdThreshold()134 public float getLangIdThreshold() { 135 return nativeGetLangIdThreshold(modelPtr); 136 } 137 138 /** Retrieves the pointer to the native object. */ getNativePointer()139 long getNativePointer() { 140 return modelPtr; 141 } 142 143 // Visible for testing. getLangIdNoiseThreshold()144 float getLangIdNoiseThreshold() { 145 return nativeGetLangIdNoiseThreshold(modelPtr); 146 } 147 148 // Visible for testing. getMinTextSizeInBytes()149 int getMinTextSizeInBytes() { 150 return nativeGetMinTextSizeInBytes(modelPtr); 151 } 152 153 /** 154 * Returns the pointer to the native object. Note: Need to keep the LangIdModel alive as long as 155 * the pointer is used. 156 */ getNativeLangIdPointer()157 long getNativeLangIdPointer() { 158 return modelPtr; 159 } 160 nativeNew(int fd)161 private static native long nativeNew(int fd); 162 nativeNewFromPath(String path)163 private static native long nativeNewFromPath(String path); 164 nativeNewWithOffset(int fd, long offset, long size)165 private static native long nativeNewWithOffset(int fd, long offset, long size); 166 nativeDetectLanguages(long nativePtr, String text)167 private native LanguageResult[] nativeDetectLanguages(long nativePtr, String text); 168 nativeClose(long nativePtr)169 private native void nativeClose(long nativePtr); 170 nativeGetVersion(long nativePtr)171 private native int nativeGetVersion(long nativePtr); 172 nativeGetVersionFromFd(int fd)173 private static native int nativeGetVersionFromFd(int fd); 174 nativeGetLangIdThreshold(long nativePtr)175 private native float nativeGetLangIdThreshold(long nativePtr); 176 nativeGetLangIdNoiseThreshold(long nativePtr)177 private native float nativeGetLangIdNoiseThreshold(long nativePtr); 178 nativeGetMinTextSizeInBytes(long nativePtr)179 private native int nativeGetMinTextSizeInBytes(long nativePtr); 180 nativeGetVersionWithOffset(int fd, long offset, long size)181 private static native int nativeGetVersionWithOffset(int fd, long offset, long size); 182 } 183