自然语言处理预训练模型简历抽取(中文)服务 Java SDK示例

使用前提:服务开通与资源包购买

操作步骤:

1.添加pom依赖

    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.5.14</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-nlp-automl</artifactId>
        <version>0.0.8</version>
    </dependency>

2.Code Sample

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.nlp_automl.model.v20191111.RunPreTrainServiceRequest;
import com.aliyuncs.nlp_automl.model.v20191111.RunPreTrainServiceResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


//  自然语言处理预训练模型 简历抽取服务

public class JianLi {

    public static void main(String[] args) throws ClientException {

        DefaultProfile defaultProfile = DefaultProfile.getProfile("cn-hangzhou","XXXXXXXXXX","XXXXXXXXXX");
        IAcsClient client = new DefaultAcsClient(defaultProfile);
        String content = "张某 ID:61111111\n" +
                "186123333333\u0003\n" +
                "zhang@126.com\n" +
                "男|29 岁 (1990/07/27)|现居住南京-玄武区|8年工作经验\n" +
                "职位: 软件测试工程师";
        RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
        request.setServiceName("ResumeExtractorZH");
        request.setPredictContent(content);
        RunPreTrainServiceResponse response = client.getAcsResponse(request);
        System.out.println(response.getPredictResult());

       /* Gson gson=new GsonBuilder().disableHtmlEscaping().create();
        System.out.println(gson.toJson(response.getPredictResult()));*/

       String s=decodeUnicode(response.getPredictResult());
        System.out.println(s);

    }
    public static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len;) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }
                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }
}

3.测试结果

{"result": [{"tags": [{"class": "\u59d3\u540d", "source": "rule", "span": "\u5f20\u67d0"}], "id": 0, "sent_offsets": 0, "sentence": "\u5f20\u67d0 ID:61111111"}, {"tags": [{"start": 0, "end": 12, "conf": 0, "span": "186123333333", "source": "model", "class": "\u624b\u673a\u53f7"}], "id": 0, "sent_offsets": 1, "sentence": "186123333333\u0003"}, {"tags": [{"class": "\u7535\u5b50\u90ae\u7bb1", "source": "rule", "span": "zhang@126.com"}], "id": 0, "sent_offsets": 2, "sentence": "zhang@126.com"}, {"tags": [{"start": 8, "end": 18, "conf": 0, "span": "1990/07/27", "source": "model", "class": "\u51fa\u751f\u65e5\u671f"}, {"class": "\u6027\u522b", "source": "rule", "span": "\u7537"}], "id": 0, "sent_offsets": 3, "sentence": "\u7537|29 \u5c81 (1990/07/27)|\u73b0\u5c45\u4f4f\u5357\u4eac-\u7384\u6b66\u533a|8\u5e74\u5de5\u4f5c\u7ecf\u9a8c"}, {"tags": [{"start": 4, "end": 11, "conf": 0, "span": "\u8f6f\u4ef6\u6d4b\u8bd5\u5de5\u7a0b\u5e08", "source": "model", "class": "\u5c97\u4f4d\u540d\u79f0"}], "id": 0, "sent_offsets": 4, "sentence": "\u804c\u4f4d\uff1a \u8f6f\u4ef6\u6d4b\u8bd5\u5de5\u7a0b\u5e08"}], "message": "", "time": 22, "ret_code": 0}
{"result": [{"tags": [{"class": "姓名", "source": "rule", "span": "张某"}], "id": 0, "sent_offsets": 0, "sentence": "张某 ID:61111111"}, {"tags": [{"start": 0, "end": 12, "conf": 0, "span": "186123333333", "source": "model", "class": "手机号"}], "id": 0, "sent_offsets": 1, "sentence": "186123333333"}, {"tags": [{"class": "电子邮箱", "source": "rule", "span": "zhang@126.com"}], "id": 0, "sent_offsets": 2, "sentence": "zhang@126.com"}, {"tags": [{"start": 8, "end": 18, "conf": 0, "span": "1990/07/27", "source": "model", "class": "出生日期"}, {"class": "性别", "source": "rule", "span": "男"}], "id": 0, "sent_offsets": 3, "sentence": "男|29 岁 (1990/07/27)|现居住南京-玄武区|8年工作经验"}, {"tags": [{"start": 4, "end": 11, "conf": 0, "span": "软件测试工程师", "source": "model", "class": "岗位名称"}], "id": 0, "sent_offsets": 4, "sentence": "职位: 软件测试工程师"}], "message": "", "time": 22, "ret_code": 0}

更多参考

json中包含Unicode转码问题
响应返回JSON数据时出现的unicode编码问题
预训练模型使用教程
简历抽取(中文)服务

上一篇:探访硅谷大数据公司Pivotal Lab:工程师文化该如何被尊重


下一篇:Java项目服务器cpu占用100%解决办法