Summary of issues encountered by Java using CRF

Source: Internet
Author: User

1, libcrfpp.so placed in the idea project resources, hit jar package in the jar.

Jar Package Tool Class

/*
* Class Nativeutils is published under the the The MIT License:
*
* Copyright (c) Adam Heinrich <[email protected]>
*
* Permission is hereby granted, free of charge, to all person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* In the software without restriction, including without limitation the rights
* To use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* Copies of the software, and to permit persons to whom the software are
* Furnished to does so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall is included in all
* Copies or substantial portions of the software.
*
* The software is provided ' as is ', without WARRANTY of any KIND, EXPRESS OR
* implied, including but not LIMITED to the warranties of merchantability,
* FITNESS for A particular PURPOSE and noninfringement. In NO EVENT shall the
* AUTHORS or COPYRIGHT holders be liable for any CLAIM, damages or other
* Liability, WHETHER in a ACTION of contract, TORT OR OTHERWISE, arising from,
* Out of OR in CONNECTION with the software or the use or other dealings in the
* Software.
*/
Package cz.adamh.utils;

Import java.io.*;

/**
* A Simple Library class which helps with loading dynamic libraries stored in the
* JAR Archive. These libraries usualy contain implementation of some methods in
* Native code (using Jni-java native Interface).
*
* @see Http://adamheinrich.com/blog/2012/how-to-load-native-jni-library-from-jar
* @see Https://github.com/adamheinrich/native-utils
*
*/
public class Nativeutils {

/**
* Private Constructor-this class would never be instanced
*/
Private Nativeutils () {
}

/**
* Loads Library from the current JAR archive
*
* The file from JAR is copied to system temporary directory and then loaded. The temporary file is deleted after exiting.
* Method uses String as filename because the pathname is "abstract" and not system-dependent.
*
* @param path The path of file inside JAR as absolute path (beginning with '/'), e.g./package/file.ext
* @throws ioexception If temporary file creation or Read/write operation fails
* @throws illegalargumentexception If source file (param path) does not exist
* @throws IllegalArgumentException If the path is not absolute or If the filename is shorter than three characters (Restri Ction of {@see file#createtempfile (java.lang.String, Java.lang.String)}).
*/
public static void Loadlibraryfromjar (String path) throws IOException {

if (!path.startswith ("/")) {
throw new IllegalArgumentException ("The path has been absolute (start with '/').");
}

Obtain filename from path
string[] Parts = Path.split ("/");
String filename = (parts.length > 1)? PARTS[PARTS.LENGTH-1]: null;

Split filename to prexif and suffix (extension)
String prefix = "";
String suffix = null;
if (filename! = null) {
Parts = filename.split ("\ \", 2);
prefix = parts[0];
suffix = (parts.length > 1)? "." +PARTS[PARTS.LENGTH-1]: null; Thanks, davs! :-)
}

Check if the filename is okay
if (filename = = NULL | | prefix.length () < 3) {
throw new IllegalArgumentException ("The filename has to be at least 3 characters long.");
}

Prepare Temporary file
File temp = file.createtempfile (prefix, suffix);
Temp.deleteonexit ();

if (!temp.exists ()) {
throw new FileNotFoundException ("File" + temp.getabsolutepath () + "does not exist.");
}

Prepare Buffer for data copying
byte[] buffer = new byte[1024];
int readbytes;

Open and check input stream
InputStream is = NativeUtils.class.getResourceAsStream (path);
if (is = = null) {
throw new FileNotFoundException ("File" + path + "is not found inside JAR.");
}

Open output stream and copy data between source file in JAR and the temporary file
OutputStream OS = new FileOutputStream (temp);
try {
while ((readbytes = is.read (buffer))! =-1) {
Os.write (buffer, 0, readbytes);
}
} finally {
If Read/write fails, close streams safely before throwing an exception
Os.close ();
Is.close ();
}

Finally, load the library
System.load (Temp.getabsolutepath ());
}
}

2. Need to install CRF related information

There are two ways to find it online:

This occurs because a library file such as libcrfpp.so.0 cannot be found, and the solution is (as if this method is not used by the root user):

    1. Modify the/etc/ld.so.conf file
    2. Join Include/usr/local/lib
    3. Execute/sbin/ldconfig-v, refresh Lib Library

Solution Two is to establish the following symbolic link:

  ln -s /usr/local/lib/libcrfpp.a /usr/lib/libcrfpp.a  ln -s /usr/local/lib/libcrfpp.so /usr/lib/libcrfpp.so  ln -s /usr/local/lib/libcrfpp.so.0 /usr/lib/libcrfpp.so.0
  连接 https://zxdcs.github.io/post/16/crf_java/
  python 用户连接 http://midday.me/article/94d6bd4973264e1a801f8445904a810d
  公司线上环境是docker容器方式不可用,实际用的方式一。

3、再有是连接库使用训练出来的model文件。路径网上均采用相对路劲,实际容器中不可用,采用绝对路径后解决。

caused By:java.lang.RuntimeException:feature_index.cpp (193) [Mmap_.open (Model_filename)] Mmap.h (153) [(FD =:: Open ( FileName, flag | o_binary)) >= 0] Open Failed:model
At Org.chasen.crfpp.CRFPPJNI.new_Tagger (Native Method)
At Org.chasen.crfpp.tagger.<init> (tagger.java:183)
At Com.jd.app.server.loadcrfmodel.<clinit> (loadcrfmodel.java:89)
... More

This error can be solved with 3.

Summary of issues encountered by Java using CRF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.