/// <summary> /// 页面加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { //初始化客户端 client = new Nlp(PartOfSpeech.API_KEY, PartOfSpeech.SECRET_KEY); //设置超时时间 client.Timeout = 60000; }
public static void SimNet() { var nlp = new Nlp("Api Key", "Secret Key"); var result = nlp.Simnet("你好百度", "你好世界"); Console.Write(result); }
public void WordSeg() { Nlp nlp = new Nlp("Api Key", "Secret Key"); var result = nlp.WordSeg("今天天气不错"); Console.Write(result); }
// 短文本相似度 // 短文本相似度接口用来判断两个文本的相似度得分 public static SimnetSerialize SimnetDemo(string text1, string text2) { //"浙富股份","万事通自考网" client = new Nlp(API_KEY, SECRET_KEY); // 调用短文本相似度,可能会抛出网络等异常,请使用try/catch捕获 var result = client.Simnet(text1, text2); //System.Diagnostics.Debug.WriteLine(result); /* * // 如果有可选参数 * var options = new Dictionary<string, object>{ * {"model", "CNN"} * }; * * // 带参数调用短文本相似度 * result = client.Simnet(text1, text2, options); * //System.Diagnostics.Debug.WriteLine(result); */ SimnetSerialize res = new SimnetSerialize(); res.score = result["score"].ToString(); return(res); }
// 词法分析 public static ResultSerialize LexerDemo(string text) { client = new Nlp(API_KEY, SECRET_KEY); JObject result = client.Lexer(text); //System.Diagnostics.Debug.WriteLine(result); ResultSerialize res = new ResultSerialize(); res.log_id = result["log_id"].ToString(); res.text = result["text"].ToString(); res.items = (JArray)result["items"]; for (int i = 0; i < res.items.Count; i++) { LexerSerialize _item = new LexerSerialize(); //loc_details = _item.byte_offset = int.Parse(res.items[i]["byte_offset"].ToString()); _item.uri = res.items[i]["uri"].ToString(); _item.pos = res.items[i]["pos"].ToString(); _item.ne = res.items[i]["ne"].ToString(); _item.item = res.items[i]["item"].ToString(); //basic_words = _item.byte_length = int.Parse(res.items[i]["byte_length"].ToString()); _item.formal = res.items[i]["formal"].ToString(); //System.Diagnostics.Debug.Print("item是 " + res.items[i]["item"].ToString()); } return(res); }
public static void WordPos() { var nlp = new Nlp("Api Key", "Secret Key"); var result = nlp.WordPos("今天天气不错"); Console.Write(result); }
public void WordSeg() { Nlp nlp = new Nlp(AiKeySecret.ApiKey, AiKeySecret.SecretKey); var result = nlp.WordSeg("今天天气不错"); Console.Write(result); }
public static void Dnnlm() { var nlp = new Nlp("Api Key", "Secret Key"); var result = nlp.DNN_LM_Cn("今天天气不错"); Console.Write(result); }
// 词义相似度 // 两个文本相似度得分 public static WordSimEmbeddingSerialize WordSimEmbeddingDemo(string word1, string word2, Dictionary <string, object> options = null) { //"北京", "上海" client = new Nlp(API_KEY, SECRET_KEY); // 调用词义相似度,可能会抛出网络等异常,请使用try/catch捕获 var result = client.WordSimEmbedding(word1, word2); //System.Diagnostics.Debug.WriteLine(result); // 如果有可选参数 /* * var options = new Dictionary<string, object>{ * {"mode", 0} * }; */ // 带参数调用词义相似度 //var result = client.WordSimEmbedding(word1, word2, options); //System.Diagnostics.Debug.WriteLine(result); WordSimEmbeddingSerialize res = new WordSimEmbeddingSerialize(); res.score = result["score"].ToString(); return(res); }
/// <summary> /// 文本纠错,主要程序 /// </summary> /// <param name="path">excel试题库</param> /// <param name="resultpath">错误内容存放的位置</param> public static void Ecnet(string path, string resultpath, out int count) { count = 0; DataTable dt = util.ExcelToDataTable(path, true); List <Question> list = util.DatatableConvertToQuestion(dt); Nlp client = util.CreateClient(); StringBuilder sb = null; StreamWriter errorprint = new StreamWriter("error.txt", true); StreamWriter ResultPrint = new StreamWriter(resultpath, true); foreach (Question question in list) { sb = new StringBuilder(question.Title.Replace("_______", util.GetAnswerStr(question, question.Answer))); sb.Append(question.Choosea); sb.Append(question.Chooseb); sb.Append(question.Choosec); sb.Append(question.Choosed); sb.Append(question.Explain); int i = 0; while (sb.Length - 255 * i > 0) { try { JObject result = client.Ecnet(sb.Length - 255 * i > 255 ? sb.ToString().Substring(i * 255, 255) : sb.ToString().Substring(i * 255, sb.Length - 255 * i)); JToken error_code; //如果发生错误进行记录 if (result.TryGetValue("error_code", out error_code)) { string ErrorPrint = question.AllID + "||" + error_code.ToString() + "||" + result["error_msg"]; errorprint.WriteLine(ErrorPrint); errorprint.Flush(); Thread.Sleep(500); break; } else //如果获得正确结果的处理 { decimal score = Convert.ToDecimal(result["item"]["score"]); string CorrectPrint = "Count:" + count + "||path:" + path + "||ALLID:" + question.AllID + "||SNID:" + question.SNID + "||log_id:" + result["log_id"]; Console.WriteLine(CorrectPrint); //如果有需要纠错的内容 if (score != 0) { ResultPrint.WriteLine(CorrectPrint + result.ToString()); ResultPrint.Flush(); Console.WriteLine(result); } } i++; Thread.Sleep(500); } catch (Exception e) { errorprint.WriteLine(e.Message); errorprint.Flush(); } } count++; } errorprint.Close(); ResultPrint.Close(); }
public static void WordPos() { var nlp = new Nlp(AiKeySecret.ApiKey, AiKeySecret.SecretKey); var result = nlp.WordPos("今天天气不错"); Console.Write(result); }
public void Lexer() { Nlp nlp = new Nlp("Api Key", "Secret Key"); var result = nlp.Lexer("今天天气不错"); Console.Write(result); }
public static void CommentTag() { var nlp = new Nlp("Api Key", "Secret Key"); var result = nlp.CommentTag("个人觉得这车不错,外观漂亮年轻,动力和操控性都不错", 10); Console.Write(result); }
public static void SentimentClassify() { var nlp = new Nlp("Api Key", "Secret Key"); var result = nlp.SentimentClassify("个人觉得这车不错,外观漂亮年轻,动力和操控性都不错"); Console.Write(result); }
// 依存句法分析 public static ResultSerialize DepParserDemo(string text) { client = new Nlp(API_KEY, SECRET_KEY); // 调用依存句法分析,可能会抛出网络等异常,请使用try/catch捕获 JObject result = client.DepParser(text); //System.Diagnostics.Debug.WriteLine(result); ResultSerialize res = new ResultSerialize(); res.log_id = result["log_id"].ToString(); res.text = result["text"].ToString(); res.items = (JArray)result["items"]; for (int i = 0; i < res.items.Count; i++) { DepParserSerialize _gammar = new DepParserSerialize(); //_gammar.id = res.items[i]["id"].ToString(); _gammar.word = res.items[i]["word"].ToString(); _gammar.postag = res.items[i]["postag"].ToString(); _gammar.head = res.items[i]["head"].ToString(); _gammar.deprel = res.items[i]["deprel"].ToString(); } return(res); // 如果有可选参数 var options = new Dictionary <string, object> { { "mode", 1 } }; // 带参数调用依存句法分析 result = client.DepParser(text, options); }
public Form1() { InitializeComponent(); txtRecorder = new TxtRecorder(); GetSettings(); nlp = new Nlp(txtAppKey.Text, txtSecretKey.Text); imageClassify = new ImageClassify(txtAppKey.Text, txtSecretKey.Text); }
private Nlp CreateLanguageClient() { var apiKey = string.IsNullOrEmpty(App.CloudKey) ? App.ApiKey : App.CloudKey; var secretKey = string.IsNullOrEmpty(App.CloudSecret) ? App.SecretKey : App.CloudSecret; var client = new Nlp(apiKey, secretKey); client.Timeout = 60000; return(client); }
private Nlp getClient() { if (this.nlp != null) { return(this.nlp); } this.nlp = new Nlp(APIKEY, SECRETKEY); this.nlp.Timeout = 60000; return(this.nlp); }
public static void WordEmbedding() { var nlp = new Nlp("Api Key", "Secret Key"); // 词相似度 var result = nlp.WordEmbeddingSimilarity("北京", "上海"); Console.Write(result); // 词向量 result = nlp.WordEmbeddingVector("北京"); Console.Write(result); }
public static void DepParser() { var nlp = new Nlp(AiKeySecret.ApiKey, AiKeySecret.SecretKey); var options = new Dictionary <string, object>() { { "mode", 1 } }; var result = nlp.DepParser("今天天气不错", options); Console.Write(result); }
public async Task <string> AnalyseAsync(long id) { var news = await _context.News.FindAsync(id); if (news.Analyseresult == null) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); StreamReader sr = new StreamReader("AnalyzerConfigs/apiconfig.json"); string json = sr.ReadToEnd(); //json文件转为 对象 T 创建的类 字段名 应该和json文件中的保持一致 var data = JsonConvert.DeserializeObject <T>(json); var APP_ID = data.appid; var API_KEY = data.apikey; var SECRET_KEY = data.secretkey; var client = new Nlp(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 string src = news.Content; if (news.Content.Length > 1020) { if (news.Content.Length > 2980) { _ = src.Substring(0, 2980); } var content = src; var maxSummaryLen = 300; // 如果有可选参数 var options = new Dictionary <string, object> { { "title", news.Title } }; // 带参数调用新闻摘要接口 var ress = client.NewsSummary(content, maxSummaryLen, options); Console.WriteLine(ress); src = (string)ress["summary"]; } var result = client.SentimentClassify(src); String[] res = { "negative", "neutral", "positive" }; if (result.ContainsKey("error_code")) { return(result.ToString()); } news.Analyseresult = res[(int)result["items"][0]["sentiment"]]; _context.Entry(news).State = EntityState.Modified; await _context.SaveChangesAsync(); return(res[(int)result["items"][0]["sentiment"]]); } return(news.Analyseresult); }
private void SaveSettings() { Settings.Default.AppKey = txtAppKey.Text; Settings.Default.SecretKey = txtSecretKey.Text; Settings.Default.ConnectString = txtConnectString.Text; Settings.Default.TextTable = txtTable.Text; Settings.Default.TextColumn = txtColumn.Text; Settings.Default.OverLoad = txtOverLoad.Text; Settings.Default.TopicLimit = txtTopicLimit.Text; Settings.Default.TestTik = txtTestTik.Text; Settings.Default.Save(); nlp = new Nlp(txtAppKey.Text, txtSecretKey.Text); }
public BaiduAi(ILogger <BaiduAi> logger, Orc orc, FaceRecognition faceRecognition, BodyAnalysis bodyAnalysis, Speech speech, ImageClassify imageClassify, ImageSearch imageSearch, ImageEffects imageEffects, Nlp nlp) { _logger = logger; _orc = orc; _faceRecognition = faceRecognition; _bodyAnalysis = bodyAnalysis; _speech = speech; _imageClassify = imageClassify; _imageSearch = imageSearch; _imageEffects = imageEffects; _nlp = nlp; }
// 词向量表示 public static WordEmbeddingSerialize WordEmbeddingDemo(string text) { client = new Nlp(API_KEY, SECRET_KEY); // 调用词向量表示,可能会抛出网络等异常,请使用try/catch捕获 JObject result = client.WordEmbedding(text); //System.Diagnostics.Debug.WriteLine(result); WordEmbeddingSerialize res = new WordEmbeddingSerialize(); res.word = result["word"].ToString(); res.vec = (JArray)result["vec"]; return(res); }
public static void SentimentClassify() { var nlp = new Nlp(AppKey, AppSecret); const string sqlStr = "SELECT id,accountid,t_mk,vm_id FROM dbo.Sys_TaskDaily"; var taskDailyList = DapperHelper.Query <TaskDailyEntity>(sqlStr, null); foreach ( var result in from item in taskDailyList where !string.IsNullOrWhiteSpace(item.T_mk) select nlp.SentimentClassify(item.T_mk)) { SimpleLog.Instance.WriteLogForFile("用户反馈情感分析", JsonConvert.SerializeObject(result)); } Console.WriteLine("执行完毕!"); }
// 评论观点抽取 public static CommentTagSerialize CommentTagDemo(string text, Dictionary <string, object> options = null) { //"三星电脑电池不给力" client = new Nlp(API_KEY, SECRET_KEY); // 调用评论观点抽取,可能会抛出网络等异常,请使用try/catch捕获 JObject result = null; try { //result = client.CommentTag(text); //不传option会报错 //System.Diagnostics.Debug.WriteLine(result); // 如果有可选参数 //var options = new Dictionary<string, object>{{"type", 13}}; // 带参数调用评论观点抽取 result = client.CommentTag(text, options); } catch { //System.Console.WriteLine("是否存在 ==>> " + (result != null)); //System.Console.WriteLine("result子物体 ==>> " + result.Count); } CommentTagSerialize res = new CommentTagSerialize(); res.items = (JArray)result["items"]; //System.Console.WriteLine("res子物体"); //System.Console.WriteLine("finally ==>> " + res.items.First.First); for (int i = 0; i < res.items.Count; i++) { CommentTag_Sub _item = new CommentTag_Sub(); _item.prop = res.items[i]["prop"].ToString(); _item.adj = res.items[i]["adj"].ToString(); _item.sentiment = int.Parse(res.items[i]["sentiment"].ToString()); _item.begin_pos = int.Parse(res.items[i]["begin_pos"].ToString()); _item.end_pos = int.Parse(res.items[i]["end_pos"].ToString()); _item.abs = res.items[i]["abstract"].ToString(); } return(res); }
protected override void Init() { base.Init(); nlpData = new Dictionary <WordType, string>(); client = new Nlp(API_KEY, SECRET_KEY) { Timeout = 60000 }; recognizer = new DictationRecognizer { AutoSilenceTimeoutSeconds = 1 }; recognizer.DictationComplete += DictationComplete; recognizer.DictationResult += DictationResult; recognizer.DictationError += DictationError; recognizer.DictationHypothesis += DictationHypothesis; recognizer.Start(); }
/// <summary> /// 分词 /// </summary> public static void Nlp(string text) { try { if (nlp == null) { string APP_ID = "14902717"; string API_KEY = "1qHCYskEsmQyMYYwGa2b4RI9"; string SECRET_KEY = "34uRO8hYapy7OTKGzuEDK3EeLyDsZOMt"; nlp = new Nlp(API_KEY, SECRET_KEY); nlp.Timeout = 60000; // 修改超时时间 } // 调用词法分析,可能会抛出网络等异常,请使用try/catch捕获 var result = nlp.Lexer(text); BaiduNlp nlpEntity = JsonConvert.DeserializeObject <BaiduNlp>(result.ToString()); } catch (Exception) { throw; } }
// DNN语言模型 public static DnnlmCnSerialize DnnlmCnDemo(string text) { //床前明月光; client = new Nlp(API_KEY, SECRET_KEY); // 调用DNN语言模型,可能会抛出网络等异常,请使用try/catch捕获 JObject result = client.DnnlmCn(text); //System.Diagnostics.Debug.WriteLine(result); DnnlmCnSerialize res = new DnnlmCnSerialize(); res.log_id = result["log_id"].ToString(); res.text = result["text"].ToString(); res.items = (JArray)result["items"]; for (int i = 0; i < res.items.Count; i++) { DnnlmCn_Sub _item = new DnnlmCn_Sub(); _item.word = res.items[i]["word"].ToString(); _item.prob = double.Parse(res.items[i]["prob"].ToString()); } res.ppl = double.Parse(result["ppl"].ToString()); return(res); }
public void Test() { nlp = new Nlp("GksxjvtX80wjcSN660ZW8QZu", "ccTZ6jSyR2OMukV9CIPukoHy27GjGzVy"); nlp.Timeout = 3000; bool shouldContinue = true; while (shouldContinue) { Console.WriteLine(" 开始英译中测试\n 输入esc退出测试 输入\'?\'百度翻译\n"); while (TestEng2ChiSingle()) { ; } wordList.Reset(); Console.WriteLine(" 开始中译英测试\n 输入esc退出测试 输入\'?\'百度翻译\n"); while (TestChi2EngSingle()) { ; } if (shouldMarkAsPass && testPassFlag.Key && testPassFlag.Value) { MarkAsPass(); } shouldContinue = chooseContinue(); if (shouldContinue) { wordList.Reset(); } } }