智能春联写诗~C#开荒攻略

阅读: 评论:0

智能春联写诗~C#开荒攻略

智能春联写诗~C#开荒攻略

作者:goJhou

本期又来开教程贴啦,这次带来的是新品喔!热乎的C#智能春联与写诗调用教程~

文档参考这里哟!

step 0! 检查权限!

各位小主进入自己的应用列表,编辑一下应用,看看结构化智能写作是否勾选上啦?

Step 1!获取accesstoken!

万事找入口,ak第一步!

String authHost = ".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 = ".0/nlp/v1/couplets?access_token=" + TOKEN;
string poemURL = ".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项目基于 4.5. 依赖newtonsoft.json

demo工程链接:.Zhou/Baidu.SmartPoem

最后祝小主们 5分钟速通~good night~

本文发布于:2024-02-03 07:26:24,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170691638449524.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:春联   攻略   智能
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23