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;
}
}
}