Java ---- baidu评价抽取关键词-商品评论
import org.json.JSONObject; import com.baidu.aip.nlp.AipNlp; import com.baidu.aip.nlp.ESimnetType; import cc.xxx.common.retus.CatReference; import cc.xxx.common.segment.json.Segmentaion; import cc.xxx.common.utils.JsonPoolUtils; /** * 评论关键词抽取 * @作者 light-zhang * @时间 2018年11月29日 * @product mall-utils * @package cc.zeelan.common.segment * @file SegmentationUtil.java * */ public final class SegmentationUtil extends CatReference { private static ThreadLocal<SegmentationUtil> instance = new ThreadLocal<SegmentationUtil>(); // 设置APPID/AK/SK private static final String APP_ID = "xxxxxx"; private static final String API_KEY = "xxxxxx"; private static final String SECRET_KEY = "xxxxxx"; private static AipNlp client; private static synchronized void syncInit() { client = new AipNlp(APP_ID, API_KEY, SECRET_KEY); client.setConnectionTimeoutInMillis(2000); client.setSocketTimeoutInMillis(60000); // 可选:设置网络连接参数 // client.setHttpProxy("proxy_host", proxy_port); // 设置http代理 // client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理 } static { if (instance.get() == null || client == null) { syncInit(); } } public static Segmentaion tag(String text) { JSONObject res = client.commentTag(text, ESimnetType.SHOPPING, null); Segmentaion segment = JsonPoolUtils.fromJson(res.toString(), Segmentaion.class); System.out.println(segment.getItems().size()); return segment; } public static void main(String[] args) { // 调用接口 String text = "红心的很不错,果肉挺多的,而且物美价廉,吃完回购!"; System.out.println(tag(text)); } }
pom.xml引入的包
<dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.8.0</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>