在海量文本中提取出能够反映主题或者主要内容的词或短语。





POST
| 参数名称 | 参数说明 | 请求类型 | 是否必须 | 数据类型 |
|---|---|---|---|---|
| size | 想要提取关键词的个数,size数不能超过文本字数 | Body:form-data | true | string |
| text | 想要提取关键词的文本 | Body:form-data | true | string |
| token | 使用API时系统需要验证的身份信息 | Headers | true | string |
| 参数名称 | 参数示例 |
|---|---|
| 参数为json格式,json 中包含除token外所有参数 | {"text":"我的希望是希望张晚霞的背影被晚霞映红"} |
<dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.13</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.11</version> </dependency> </dependencies>
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* nlp分词范例
*/
public class test {
public static void main(String[] args) {
//请求头中的token 需要进行替换成在官网申请的新token
String token="TOKEN请在官网自行获取";
//申请的接口地址
String url="http://comdo.hanlp.com/hanlp/v1/segment/nlp";
//处理分析的文本,作为params参数传入
Map<String,Object> params=new HashMap<String,Object>();
//参数传入要处理分析的文本
params.put("text", "我的希望是希望张晚霞的背影被晚霞映红");
//执行HanLP接口,result为返回的结果
String result=doHanlpApi(token,url,params);
//打印输出结果
System.out.println(result);
}
public static String doHanlpApi(String token,String url,Map<String,Object> params) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
//添加header请求头,token请放在header里
httpPost.setHeader("token", token);
// 创建参数列表
List<NameValuePair> paramList = new ArrayList<>();
if (params != null) {
for (String key : params.keySet()) {
//所有参数依次放在paramList中
paramList.add(new BasicNameValuePair(key, (String) params.get(key)));
}
//模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
return resultString;
} catch (Exception e) {
e.printStackTrace();
} finally {
if(response!=null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
{
"code":0,
"data":[
{
"nature":"r",
"word":"我"
},
{
"nature":"u",
"word":"的"
},
{
"nature":"n",
"word":"希望"
},
{
"nature":"v",
"word":"是"
},
{
"nature":"v",
"word":"希望"
},
{
"nature":"nr",
"word":"张晚霞"
},
{
"nature":"u",
"word":"的"
},
{
"nature":"n",
"word":"背影"
},
{
"nature":"p",
"word":"被"
},
{
"nature":"n",
"word":"晚霞"
},
{
"nature":"v",
"word":"映"
},
{
"nature":"a",
"word":"红"
}
]
}