openFile method

void openFile(
  1. int tokenizerType,
  2. {String? modelFile,
  3. String? vocabFile,
  4. String? mergeFile}
)

Implementation

void openFile(
  int tokenizerType, {
  String? modelFile,
  String? vocabFile,
  String? mergeFile,
}) {
  close(); // for reopen

  ailiaTokenizer = ailia_tokenizer_dart.ailiaTokenizerFFI(
    ailiaCommonGetLibrary(ailiaCommonGetTokenizerPath()),
  );
  ppAiliaTokenizer = malloc<Pointer<ailia_tokenizer_dart.AILIATokenizer>>();

  int status = ailiaStatusSuccess;

  status = ailiaTokenizer.ailiaTokenizerCreate(
    ppAiliaTokenizer,
    tokenizerType,
    ailia_tokenizer_dart.AILIA_TOKENIZER_FLAG_NONE,
  );
  if (status != ailiaStatusSuccess) {
    throw Exception("ailiaTokenizerCreate error $status");
  }

  if (modelFile != null) {
    if (Platform.isWindows) {
      Pointer<Int16> nativeModelFile = modelFile.toNativeUtf16().cast<Int16>();
      status = ailiaTokenizer.ailiaTokenizerOpenModelFileW(
        ppAiliaTokenizer!.value,
        nativeModelFile,
      );
      calloc.free(nativeModelFile);
    } else {
      Pointer<Int8> nativeModelFile = modelFile.toNativeUtf8().cast<Int8>();
      status = ailiaTokenizer.ailiaTokenizerOpenModelFileA(
        ppAiliaTokenizer!.value,
        nativeModelFile,
      );
      calloc.free(nativeModelFile);
    }
    if (status != ailiaStatusSuccess) {
      throw Exception("ailiaTokenizerOpenModelFileA error $status");
    }
  }

  if (vocabFile != null) {
    if (Platform.isWindows) {
      Pointer<Int16> nativeVocablFile = vocabFile.toNativeUtf16().cast<Int16>();
      status = ailiaTokenizer.ailiaTokenizerOpenVocabFileW(
        ppAiliaTokenizer!.value,
        nativeVocablFile,
      );
      calloc.free(nativeVocablFile);
    } else {
      Pointer<Int8> nativeVocabFile = vocabFile.toNativeUtf8().cast<Int8>();
      status = ailiaTokenizer.ailiaTokenizerOpenVocabFileA(
        ppAiliaTokenizer!.value,
        nativeVocabFile,
      );
      calloc.free(nativeVocabFile);
    }
    if (status != ailiaStatusSuccess) {
      throw Exception(
          "ailiaTokenizerOpenVocabFile error $status : $vocabFile");
    }
  }

  if (mergeFile != null) {
    if (Platform.isWindows) {
      Pointer<Int16> nativeMergeFile = mergeFile.toNativeUtf16().cast<Int16>();
      status = ailiaTokenizer.ailiaTokenizerOpenMergeFileW(
        ppAiliaTokenizer!.value,
        nativeMergeFile,
      );
      calloc.free(nativeMergeFile);
    } else {
      Pointer<Int8> nativeMergeFile = mergeFile.toNativeUtf8().cast<Int8>();
      status = ailiaTokenizer.ailiaTokenizerOpenMergeFileA(
        ppAiliaTokenizer!.value,
        nativeMergeFile,
      );
      calloc.free(nativeMergeFile);
    }
    if (status != ailiaStatusSuccess) {
      throw Exception(
          "ailiaTokenizerOpenMergeFile error $status : $mergeFile");
    }
  }

  available = true;
}