java 文本 聚类_文本聚类引擎 — BosonNLP HTTP API 1.0 documentation

本文档展示了如何使用BosonNLP的HTTP API进行文本聚类。通过Java代码示例,详细解释了从读取数据到调用API进行聚类分析、查看任务状态、获取结果及清理的全过程。
摘要由CSDN通过智能技术生成

package clusterApiExample;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import com.mashape.unirest.http.HttpResponse;

import com.mashape.unirest.http.JsonNode;

import com.mashape.unirest.http.Unirest;

import com.mashape.unirest.http.exceptions.UnirestException;

public class ClusterApiExample {

// 请把TASK_ID换成您自己的ID

public static final String CLUSTER_PUSH = "http://api.bosonnlp.com/cluster/push/TASK_ID";

public static final String CLUSTER_ANALYSIS = "http://api.bosonnlp.com/cluster/analysis/TASK_ID";

public static final String CLUSTER_STATUS = "http://api.bosonnlp.com/cluster/status/TASK_ID";

public static final String CLUSTER_RESULT = "http://api.bosonnlp.com/cluster/result/TASK_ID";

public static final String CLUSTER_CLEAR = "http://api.bosonnlp.com/cluster/clear/TASK_ID";

// 请记得把 YOUR_API_TOKEN 换成您申请的 Token

public static final String API_TOKEN = "YOUR_API_TOKEN";

public static String filePath = "clusterInfos.txt";

public static boolean flag;

public static void main(String[] args) throws JSONException,

UnirestException, IOException {

// 读取根目录下的clusterInfos.txt文件里的内容作为聚类上传的数据,请根据自身需求做更改

flag = readTxtFile(filePath);

// 聚类分析API调用

HttpResponse clusterAnalysisResponse = Unirest

.get(CLUSTER_ANALYSIS)

.header("Accept", "application/json")

.header("X-Token", API_TOKEN)

.asString();

System.out.println(clusterAnalysisResponse.getCode());

// 调用查看状态的API来查看任务处理进度,这里每隔一段时间查询一次

while (flag) {

// 聚类状态API调用

HttpResponse clusterStatusResponse = Unirest

.get(CLUSTER_STATUS)

.header("Accept", "application/json")

.header("X-Token", API_TOKEN)

.asString();

JSONObject jsonObj = new JSONObject(clusterStatusResponse.getBody());

String status = (String) jsonObj.get("status");

try{

Thread.sleep(2000);

}catch(InterruptedException ie){

ie.printStackTrace();

}

System.out.println(status);

if ("DONE".equals(status)) {

flag = false;

}

else

System.out.println("请稍等,数据处理中...");

}

// 聚类结果API调用

HttpResponse clusterResultResponse = Unirest

.get(CLUSTER_RESULT)

.header("Accept", "application/json")

.header("X-Token", API_TOKEN)

.asJson();

System.out.println("以下是返回的结果:");

System.out.println(clusterResultResponse.getBody());

// 聚类清除API调用

HttpResponse clusterClearResponse = Unirest

.get(CLUSTER_CLEAR)

.header("Accept", "application/json")

.header("X-Token", API_TOKEN)

.asString();

// 返回的结果是一段JSON,num 指该 cluster 包含的文档数目,_id 指该 cluster最具代表性的文档

// list 指所有属于该 cluster 的文档 _id

System.out.println(clusterClearResponse.getBody());

}

public static boolean readTxtFile(String filePath ) {

try {

String encoding = "utf-8";

File file = new File(filePath);

if (file.isFile() && file.exists()) { // 判断文件是否存在

InputStreamReader reader = new InputStreamReader(new FileInputStream(file), encoding);

BufferedReader bufferedReader = new BufferedReader(reader);

String lineTxt = null;

int i = 0;

while ((lineTxt = bufferedReader.readLine()) != null) {

System.out.println(i);

JSONArray jsonArray = new JSONArray();

JSONObject jsonObject = new JSONObject();

jsonObject.put("_id", ++i);

jsonObject.put("text", lineTxt);

jsonArray.put(jsonObject);

String body = jsonArray.toString();

System.out.println(body);

// 聚类上传API调用

// 上传的数据时JSON格式的,必须包含_id(文档的编号,可以采用整数或字符串)和text(文档的内容,原始的文章信息)

HttpResponse clusterPushResponse = Unirest.post(CLUSTER_PUSH)

.header("Accept", "application/json")

.header("Content-Type","application/json")

.header("X-Token", API_TOKEN)

.body(body).asJson();

}

reader.close();

return true;

}else{

System.out.println("找不到指定的文件");

return false;

}

} catch (Exception e) {

System.out.println("读取文件内容出错");

e.printStackTrace();

return false;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值