本文主要是介绍智能春联写诗~C#开荒攻略,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
作者:goJhou
本期又来开教程贴啦,这次带来的是新品喔!热乎的C#智能春联与写诗调用教程~
文档参考这里哟!
http://ai.baidu.com/docs#/IntelligentWriting-API/f85d34cc
step 0! 检查权限!
各位小主进入自己的应用列表,编辑一下应用,看看结构化智能写作是否勾选上啦?
Step 1!获取accesstoken!
万事找入口,ak第一步!
String authHost = "https://aip.baidubce.com/oauth/2.0/token";
HttpClient client = new HttpClient();
List> paraList = new List>();
paraList.Add(new KeyValuePair("grant_type", "client_credentials"));
paraList.Add(new KeyValuePair("client_id", clientId));
paraList.Add(new KeyValuePair("client_secret", clientSecret));HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
TOKEN= JObject.Parse(result)["access_token"].ToString();
Step 2!燥起来!
核心代码供出:
string url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/couplets?access_token=" + TOKEN;
string poemURL = "https://aip.baidubce.com/rpc/2.0/nlp/v1/poem?access_token=" + TOKEN;JObject obj = new JObject();obj["text"] = "百度";
obj["index"] = 0;HttpWebRequest poem = (HttpWebRequest)WebRequest.Create(poemURL);poem.ContentType = "application/json";
poem.Method = "POST";byte[] poemcontent = Encoding.UTF8.GetBytes(obj.ToString());using (Stream poemStr = poem.GetRequestStream())
{
poemStr.Write(poemcontent, 0, poemcontent.Length);
}
HttpWebResponse poemresp = (HttpWebResponse)poem.GetResponse();
Stream poemreceiveStream = poemresp.GetResponseStream();
string poemres = new StreamReader(poemreceiveStream).ReadToEnd();
加上合适的UI呈现,简单的demo就完成啦~
下方是以 百度 生成的春联与诗喔~
本demo项目基于.net 4.5. 依赖newtonsoft.json
demo工程链接:https://gitee.com/Jack.Zhou/Baidu.SmartPoem
最后祝小主们 5分钟速通~good night~
这篇关于智能春联写诗~C#开荒攻略的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!