TxApiService.java
3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.pipihelper.project.tx.service;
import com.alibaba.fastjson.JSONObject;
import com.pipihelper.project.tx.dto.TxConfig;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.nlp.v20190408.NlpClient;
import com.tencentcloudapi.nlp.v20190408.models.*;
import com.tencentcloudapi.tbp.v20190627.TbpClient;
import com.tencentcloudapi.tbp.v20190627.models.TextProcessRequest;
import com.tencentcloudapi.tbp.v20190627.models.TextProcessResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @Description: TODO
* @author: charles
* @date: 2022年06月06日 13:31
*/
@Service
public class TxApiService {
@Autowired
private NlpClient nlpClient;
@Autowired
private TbpClient tbpClient;
@Autowired
private TxConfig txConfig;
public JSONObject txKeywordsExtraction(String msgText){
KeywordsExtractionRequest req = new KeywordsExtractionRequest();
req.setText(msgText);
req.setNum(5L);
try {
// 返回的resp是一个KeywordsExtractionResponse的实例,与请求对象对应
KeywordsExtractionResponse resp = nlpClient.KeywordsExtraction(req);
// 输出json格式的字符串回包
return JSONObject.parseObject(KeywordsExtractionResponse.toJsonString(resp));
}catch (TencentCloudSDKException e) {
System.out.println(e.toString());
return null;
}
}
public TextProcessResponse txTextProcessRequest(String terminalId, String inputText){
try{
// 实例化一个请求对象,每个接口都会对应一个request对象
TextProcessRequest req = new TextProcessRequest();
req.setBotId(txConfig.getRobotId());
req.setBotEnv(txConfig.getRobotEnv());
req.setTerminalId(terminalId);
req.setInputText(inputText);
// 返回的resp是一个TextProcessResponse的实例,与请求对象对应
return tbpClient.TextProcess(req);
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return null;
}
//新增自定义词库词条
@Async
public CreateWordItemsResponse createWordItemsResponse(String dictId, String[] texts){
try {
CreateWordItemsRequest req = new CreateWordItemsRequest();
req.setDictId(dictId);
WordItem[] wordItems = new WordItem[texts.length];
for(int i=0;i<texts.length;i++){
WordItem wordItem1 = new WordItem();
wordItem1.setText(texts[i]);
wordItems[i] = wordItem1;
}
req.setWordItems(wordItems);
return nlpClient.CreateWordItems(req);
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
return null;
}
public List<String> lexicalAnalysisResponse(String dictId, String texts){
List<String> keywords =new ArrayList<>();
try {
LexicalAnalysisRequest req = new LexicalAnalysisRequest();
req.setDictId(dictId);
req.setText(texts);
// 返回的resp是一个LexicalAnalysisResponse的实例,与请求对象对应
LexicalAnalysisResponse resp = nlpClient.LexicalAnalysis(req);
PosToken[] PosTokens = resp.getPosTokens();
for(PosToken posToken: PosTokens){
if(posToken.getPos().equals("n")){
if(posToken.getWord().length()>1) {
if (!keywords.contains(posToken.getWord())) {
keywords.add(posToken.getWord());
}
}
}
}
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
return keywords;
}
}