解决字典不能实时生效问题
lsj19830812 发布于2019-01-03 15:21 浏览:1594 回复:4
0
收藏
0
收藏

论坛和QQ群中很多开发者反馈平台上的上传字典无法实时生效,甚至看不到是否已生效,测试意图时无法正常识别意图(平台体验亟待改善啊)。

而通过网页编辑字典则可以实时生效。我通过模拟http请求的方式,让字典文件加入平台,使之实时生效(为简便起见,不做模拟登录的操作,直接使用cookies)。

调用方法传入5个参数分别是:本地字典文件路径 技能ID 字典中文名 字典英文名 cookies字符串

本地字典文件路径:就是你的字典csv文件,格式用平台给的模板格式即可

技能ID:你的技能ID

字典中文名和字典英文名:就是你要新增的字典名,规则与平台要求保持一致

cookies字符串:特别长的一个串可以通过浏览器抓包获取到(类似下面的这个)

比如360浏览器==》在页面上点“审查元素”==进入“network”==选择dueros 的新增字典的xhr请求,查看“Header” == “Request Header” 里面有行是变量 Cookies,把下面Hm开头的整个复制下来即可

Hm_lvt_433a7faef9e64b18590d=154641790,15464751,15464891,1546990; Hm_lpvt_433a7faef9ed730ba864b18590d=15464198; Hm_lvt_15435005fb2d0ef13e32fdfb96=15299747; BAIDUID=5CE7E1542363541AC2A6AF135FDB277:FG=1; PSTM=154520839; BIDUPSID=E437FCAD8E8B2D461226440FDD4D16; USERTOKEN=d60a72829e99f7649b6648dc0c708a0; Hm_lvt_022b771c39f64c147b5cfd2b71754=154476353,154079837,154589443,154597741; Hm_lvt_668f5751b331d2ac31f2dc0253443=154597467,15496096,154597516,154598315; MCITY=-236%3A31%3A; BDORZ=B490B53CD02E5D22BCDA1598; H_PS_PSSID=1461_21094_28205_213126350_28140; Hm_lvt_2e0bc8c5e7ceb25796ba42e7b57387=154595354,156044831,154613501,154641260; Hm_lpvt_2e0bc8c5e7ceb257964962e7b57387=1546418260;
Hm_lvt_d300619958c1246b4f17c77929399c=154606935,154639105,154641371,156418709; Hm_lvt_0ba81fecb476740598cee2c6aa7f30=154596744,154631556,154640064,46475695; Hm_lpvt_0ba81fecb476740598cee63daa7f30=154647697; Hm_lpvt_d300619958c1246b4f17c779222939c=154640969; BDUSS=NCS2VJcnRQNkc1Y0M0a01FdURv2MXFDQnhQbVAwNGh2aVVrcENZOWItbFJjQVFBQUFBJCQAAAAAAAAAAAEAAAAmOHwAbHNqMTkzA4MTIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFttLVxbbS1cUn; Hm_lvt_1d2535694f40934a12d05b913e380=154617990,154648054,154640591,15464990; delPer=0; PSINO=1; Hm_lpvt_1d253dd94f40934a12d05b7913e380=154694198; ULOG_UID=jqg6wf1mz

上代码:

package com.baidu.dueros.dictutils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) throws IOException {
		String url = "https://dueros.baidu.com/dbp/nlu/addentity";
		String fileName = args[0];
		String botId = args[1];
		String zhName = args[2];
		String enName = args[3];
		String cookiesstr = "";
		for (int i = 4; i < args.length; i++) {
			cookiesstr += args[i];
		}
		JSONObject job = new JSONObject();
		job.put("botId", botId);
		job.put("dictType", "0");
		List<String> lines = FileUtils.readLines(new File(fileName), "utf-8");
		JSONArray entries = new JSONArray();
		for (int i = 0; i < lines.size(); i++) {
			job.put("entries" + i, lines.get(i).split(",")[0]);
			JSONObject entrie = new JSONObject();
			JSONArray synonyms = new JSONArray();

			for (int j = 1; j < lines.get(i).split(",").length; j++) {
				synonyms.add(lines.get(i).split(",")[j]);
			}

			entrie.put("synonyms", synonyms);
			entrie.put("value", lines.get(i).split(",")[0]);
			entries.add(entrie);
		}

		job.put("entries", entries);
		job.put("isEnum", false);
		job.put("name", enName);
		job.put("zhName", zhName);
		String body = job.toString();
		doPost(url, body, cookiesstr, "utf-8");
	}

	private static final String HTTP = "http";
	private static final String HTTPS = "https";
	private static SSLConnectionSocketFactory sslsf = null;
	private static PoolingHttpClientConnectionManager cm = null;
	private static SSLContextBuilder builder = null;
	static {
		try {
			builder = new SSLContextBuilder();
			// 全部信任 不做身份鉴定
			builder.loadTrustMaterial(null, new TrustStrategy() {

				public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
					// TODO Auto-generated method stub
					return true;
				}
			});
			sslsf = new SSLConnectionSocketFactory(builder.build(),
					new String[] { "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);
			Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
					.register(HTTP, new PlainConnectionSocketFactory()).register(HTTPS, sslsf).build();
			cm = new PoolingHttpClientConnectionManager(registry);
			cm.setMaxTotal(200);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String doPost(String url, String reqBody, String cookiesstr, String charset) {
		CloseableHttpClient httpClient = null;
		HttpPost httpPost = null;
		String result = null;
		try {
			BasicCookieStore cookieStore = new BasicCookieStore();
			String[] co = cookiesstr.split(";");
			for (int i = 0; i < co.length; i++) {
				String[] key = cookiesstr.split("=");
				BasicClientCookie cookie = new BasicClientCookie(key[0], key[1]);
				cookieStore.addCookie(cookie);
			}
			httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(cm)
					.setConnectionManagerShared(true).build();
			httpPost = new HttpPost(url);
			httpPost.addHeader("Accept", "application/json");
			httpPost.addHeader("Host", "dueros.baidu.com");
			httpPost.addHeader("Connection", "keep-alive");
			httpPost.addHeader("Origin", "https://dueros.baidu.com");
			httpPost.addHeader("X-Requested-With", "XMLHttpRequest");
			httpPost.addHeader("Content-Type", "text/plain;charset=UTF-8");
			httpPost.addHeader("Referer", "https://dueros.baidu.com/dbp/nlu/index");
			httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
			httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
			httpPost.addHeader("Content-Type", "text/plain;charset=UTF-8");
			httpPost.addHeader("User-Agent",
					"User-AgentMozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
			httpPost.addHeader("Cookie", cookiesstr);
			httpPost.setEntity(new StringEntity(reqBody, Charset.forName("UTF-8")));
			Header[] h = httpPost.getAllHeaders();
			CloseableHttpResponse res = httpClient.execute(httpPost);
			String resBody = EntityUtils.toString(res.getEntity(), "UTF-8");
			System.out.println(resBody);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}
}
收藏
点赞
0
个赞
共4条回复 最后由智小星无悔回复于2019-01-26 21:27
#2DuerOS-河徒回复于2019-01-03

比心

0
#3158******08回复于2019-01-04
该评论已删除
#4乌龟他孙子回复于2019-01-19
该评论已删除
#5智小星无悔回复于2019-01-26
该评论已删除
TOP