没人陪你聊天?教你运用java制作专属智能陪聊设备人
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">近期</span>有小伙伴留言,“很久没人找我聊天了,<span style="color: black;">能够</span>写一个陪聊<span style="color: black;">设备</span>人展现一下程序员的浪漫吗?”,“安排!”</span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">1.智能陪聊<span style="color: black;">设备</span>人演示</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">人工智能<span style="color: black;">始终</span>是<span style="color: black;">近期</span>的热点<span style="color: black;">专题</span>,自动人工智能<span style="color: black;">然则</span><span style="color: black;">败兴</span>应用<span style="color: black;">行业</span>就<span style="color: black;">持续</span>的扩大,在<span style="color: black;">将来</span>人工智能<span style="color: black;">亦</span>会在人们的生活中<span style="color: black;">持续</span>普及与应用。这篇博文中的陪聊<span style="color: black;">设备</span>人,<span style="color: black;">运用</span>java进行编写,<span style="color: black;">能够</span><span style="color: black;">按照</span>你发的信息进行智能的<span style="color: black;">回复</span>,还算挺有意思的一个小玩意。<span style="color: black;">最后</span>效果的演示如下图~</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/5e51fa67653d43fd84d732ef523128e8~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1727628747&x-signature=azABaDPz96iQzH%2FBPnDMRbWsnkc%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">2.智能问答平台API介绍</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这个陪聊<span style="color: black;">设备</span>人项目<span style="color: black;">运用</span>了青云课的智能API,<span style="color: black;">经过</span>调用API得到信息反馈。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">详细</span>的调用格式如下:</span></span></p><span style="color: black;">http</span>:<span style="color: black;">//api.qingyunke.com/api.php?key=free&appid=0&msg=%s</span>其中的%s传入<span style="color: black;">咱们</span>需要发送给<span style="color: black;">设备</span>人的内容,就<span style="color: black;">能够</span>得到API调用结果的反馈。<span style="color: black;"><span style="color: black;">key 固定参数 free</span></span><span style="color: black;"><span style="color: black;">appid 设置成0,为智能识别</span></span><span style="color: black;"><span style="color: black;">msg 为搜索关键词</span></span><span style="color: black;"><span style="color: black;">result <span style="color: black;">暗示</span>返回状态,返回0<span style="color: black;">暗示</span>正常</span></span><span style="color: black;"><span style="color: black;">content api返回的信息内容</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">能够</span>看到数据是以JSON的形式进行返回。</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/7a28159bbdda45a0bcd824dd31274aa9~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1727628747&x-signature=LqSY8Cy0%2FOVKnr6gr9KyoqWSGfI%3D" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/097c1a5525c84c75bc68754c31153844~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1727628747&x-signature=1bhZpWd%2BHdxgbdpJL0KrzxebkYQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">3.整合第三方JSON开源库</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">Gson是Google<span style="color: black;">供给</span>的类库,<span style="color: black;">能够</span>用来处理java对象与JSON数据之间的映射,将一个JSON字符串转换成一个java对象,方便<span style="color: black;">咱们</span>对API返回的JSON格式的数据进行处理,下面演示<span style="color: black;">怎样</span>将Gson类库导入到<span style="color: black;">咱们</span>的工程中。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">首要</span><span style="color: black;">能够</span>去官网下载对应的jar包,<span style="color: black;">或</span>直接私信我获取。获取jar包之后找个全英文路径进行<span style="color: black;">保留</span>。<span style="color: black;">这儿</span><span style="color: black;">咱们</span><span style="color: black;">运用</span>的编辑器是IDEA,<span style="color: black;">因此</span><span style="color: black;">运用</span>IDEA进行演示,小伙伴们<span style="color: black;">运用</span>的是其他编辑器的话导入<span style="color: black;">办法</span>都是类似的哦。在IDEA打开如下界面,找到jar包导入<span style="color: black;">就可</span>。</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/d4b4cb9b347d4dc38576e6a936992b06~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1727628747&x-signature=XAiLNQp3dWEOzo6%2BRpitFfo5rU4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">4.智能<span style="color: black;">设备</span>人项目框架搭建与模块划分</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">项目搭建:搭建的部分无太多<span style="color: black;">需求</span>,只需要<span style="color: black;">运用</span>IDEA创建一个新的普通java工程<span style="color: black;">就可</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">项目模块搭建:</span></span></p><span style="color: black;"><span style="color: black;">model 类 用来存放请求所返回的对象</span></span><span style="color: black;"><span style="color: black;">util 类用来存放工程所用到的工具类,<span style="color: black;">例如</span>说HTTP请求解析类</span></span><span style="color: black;"><span style="color: black;">app 类用来当作<span style="color: black;">设备</span>人项目的入口</span></span><span style="color: black;"><span style="color: black;">service 类用来实现业务的接口</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">关联</span>的两个实体类如下:</span></span></p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Request</span> {</span>
<span style="color: black;">private</span> String key = <span style="color: black;">"free"</span>;
<span style="color: black;">private</span> String appid = <span style="color: black;">"0"</span>;
<span style="color: black;">private</span> String msg = <span style="color: black;">""</span>;
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">Request</span><span style="color: black;">()</span></span>{
}
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">Request</span><span style="color: black;">(String msg)</span></span>{
<span style="color: black;">this</span>.msg = msg;
}
<span style="color: black;"><span style="color: black;">public</span> String <span style="color: black;">getKey</span><span style="color: black;">()</span> </span>{
<span style="color: black;">return</span> key;
}
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">void</span> <span style="color: black;">setKey</span><span style="color: black;">(String key)</span> </span>{
<span style="color: black;">this</span>.key = key;
}
<span style="color: black;"><span style="color: black;">public</span> String <span style="color: black;">getAppid</span><span style="color: black;">()</span> </span>{
<span style="color: black;">return</span> appid;
}
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">void</span> <span style="color: black;">setAppid</span><span style="color: black;">(String appid)</span> </span>{
<span style="color: black;">this</span>.appid = appid;
}
<span style="color: black;"><span style="color: black;">public</span> String <span style="color: black;">getMsg</span><span style="color: black;">()</span> </span>{
<span style="color: black;">return</span> msg;
}
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">void</span> <span style="color: black;">setMsg</span><span style="color: black;">(String msg)</span> </span>{
<span style="color: black;">this</span>.msg = msg;
}
}
<span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Response</span> {</span>
<span style="color: black;">private</span> <span style="color: black;">int</span> code;
<span style="color: black;">private</span> String content;
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">int</span> <span style="color: black;">getCode</span><span style="color: black;">()</span> </span>{
<span style="color: black;">return</span> code;
}
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">void</span> <span style="color: black;">setCode</span><span style="color: black;">(<span style="color: black;">int</span> code)</span> </span>{
<span style="color: black;">this</span>.code = code;
}
<span style="color: black;"><span style="color: black;">public</span> String <span style="color: black;">getContent</span><span style="color: black;">()</span> </span>{
<span style="color: black;">return</span> content;
}
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">void</span> <span style="color: black;">setContent</span><span style="color: black;">(String content)</span> </span>{
<span style="color: black;">this</span>.content = content;
}
}<h1 style="color: black; text-align: left; margin-bottom: 10px;">5.封装一个<span style="color: black;">设备</span>人HTTP工具类</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">HTTP工具类<span style="color: black;">重点</span>用来对api进行请求,获取返回的内容</span></span></p><span style="color: black;">public</span> <span style="color: black;">class</span> HttpUtils {
<span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">String</span> request(<span style="color: black;">String</span>api){
HttpURLConnection connection =<span style="color: black;">null</span>;
int responseCode = <span style="color: black;">0</span>;
<span style="color: black;">try</span>{
URL url = <span style="color: black;">new</span> URL(api);
<span style="color: black;">//获取对应的连接对象</span>connection = (HttpURLConnection) url.openConnection();
responseCode = connection.getResponseCode();
}<span style="color: black;">catch</span> (Exception e){
e.printStackTrace();
}
<span style="color: black;">if</span>(<span style="color: black;">200</span><= responseCode && responseCode<=<span style="color: black;">299</span>){
<span style="color: black;">try</span>(InputStream inputStream = connection.getInputStream();
BufferedReader <span style="color: black;">in</span> = <span style="color: black;">new</span> BufferedReader(<span style="color: black;">new</span>InputStreamReader(inputStream));
){
StringBuilder response =<span style="color: black;">new</span> StringBuilder();
<span style="color: black;">String</span> currentLine;
<span style="color: black;">while</span> ((currentLine = <span style="color: black;">in</span>.readLine())!= <span style="color: black;">null</span>){
response.append(currentLine);
}<span style="color: black;">String</span> result = response.toString();
<span style="color: black;">return</span> result;
}<span style="color: black;">catch</span> (Exception e){
e.printStackTrace();
}
}
<span style="color: black;">return</span> <span style="color: black;">null</span>;
}
}<h1 style="color: black; text-align: left; margin-bottom: 10px;">6.实现<span style="color: black;">设备</span>人service层的接口与定义</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">实现<span style="color: black;">设备</span>人接口层</span></span></p><span style="color: black;">public</span> <span style="color: black;">interface</span> <span style="color: black;">RobotService</span> {
<span style="color: black;">Response <span style="color: black;">qa</span>(<span style="color: black;">String msg</span>)</span> ;
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">实现<span style="color: black;">设备</span>人接口实现类,这个类用来实现API的请求,将结果进行封装成实体类返回</span></span></p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">QkyRobotServiceImpl</span> <span style="color: black;">implements</span> <span style="color: black;">RobotService</span> </span>{
<span style="color: black;">private</span> <span style="color: black;">static</span> <span style="color: black;">final</span> String apiTpl = <span style="color: black;">"http://api.qingyunke.com/api.php?key=free&appid=0&msg=%s"</span>;
<span style="color: black;">private</span> <span style="color: black;">static</span> <span style="color: black;">final</span> Gson gson = <span style="color: black;">new</span> Gson();
<span style="color: black;">@Override</span>
<span style="color: black;"><span style="color: black;">public</span> Response <span style="color: black;">qa</span><span style="color: black;">(String msg)</span> </span>{
String api = <span style="color: black;">null</span>;
<span style="color: black;">try</span> {
api = String.format(apiTpl, URLEncoder.encode(msg,<span style="color: black;">"UTF-8"</span>) );
} <span style="color: black;">catch</span>(UnsupportedEncodingException e) {
e.printStackTrace();
}
String result = HttpUtils.request(api);<span style="color: black;">//<span style="color: black;">能够</span>做<span style="color: black;">规律</span>判断,<span style="color: black;">例如</span>null的时候,<span style="color: black;">或</span>出错</span>Response response = gson.fromJson(result,Response<span style="color: black;">.<span style="color: black;">class</span>)</span>;
<span style="color: black;">return</span> response;
}
}<h1 style="color: black; text-align: left; margin-bottom: 10px;">7.制作专属于你的<span style="color: black;">设备</span>人入口</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">编写入口主类,调用封装好的模块进行<span style="color: black;">设备</span>人入口主类的编写</span></span></p><span style="color: black;">public</span> <span style="color: black;">class</span> <span style="color: black;">Main</span> {
<span style="color: black;">private</span> <span style="color: black;">static</span>final RobotService robotService =<span style="color: black;">new</span> QkyRobotServiceImpl();
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span>(<span style="color: black;">String[] args</span>)throws Exception</span> {
Scanner scanner = <span style="color: black;">new</span> Scanner(System.<span style="color: black;">in</span>);
System.<span style="color: black;">out</span>.println(<span style="color: black;">"尊敬的C站大佬,请给我取个响亮的名字!!"</span>);
System.<span style="color: black;">out</span>.println(<span style="color: black;">"-------------------------------"</span>);
String name = scanner.nextLine();
System.<span style="color: black;">out</span>.println(<span style="color: black;">"大佬好,我是大数据小禅博客里的<span style="color: black;">设备</span>人,直接给我下达指令哦~"</span>);
System.<span style="color: black;">out</span>.println(<span style="color: black;">"-------------------------------"</span>);
<span style="color: black;">while</span> (<span style="color: black;">true</span>){
String input = scanner.nextLine();<span style="color: black;">if</span>(<span style="color: black;">"88"</span>.equalsIgnoreCase(input)){
System.<span style="color: black;">out</span>.println(<span style="color: black;">"欢迎下次<span style="color: black;">运用</span>,拜拜"</span>);
<span style="color: black;">break</span>;
}<span style="color: black;">else</span> {
Response response = robotService.qa(input);
<span style="color: black;">if</span>(response !=<span style="color: black;">null</span> && response.getCode() == <span style="color: black;">0</span>){
System.<span style="color: black;">out</span>.println(<span style="color: black;">"-------------------------------"</span>);
System.<span style="color: black;">out</span>.println(name+<span style="color: black;">":"</span>+ <span style="color: black;">new</span>String(response.getContent().getBytes(),<span style="color: black;">"UTF-8"</span>));
System.<span style="color: black;">out</span>.println(<span style="color: black;">"-------------------------------"</span>);
}<span style="color: black;">else</span> {
System.<span style="color: black;">out</span>.println(name+<span style="color: black;">": 大佬你<span style="color: black;">刚才</span>这句话我没听懂,可否再<span style="color: black;">描述</span>一次~"</span>);
}
}
}
scanner.close();
}
}<h1 style="color: black; text-align: left; margin-bottom: 10px;">8.把你的<span style="color: black;">设备</span>人打包<span style="color: black;">运用</span></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">为了方便<span style="color: black;">咱们</span>对项目的<span style="color: black;">运用</span>,<span style="color: black;">这儿</span><span style="color: black;">咱们</span><span style="color: black;">运用</span>IDEA将项目打包成jar包。<span style="color: black;">经过</span>下面的<span style="color: black;">过程</span>,就<span style="color: black;">能够</span>将<span style="color: black;">咱们</span>项目里的<span style="color: black;">所有</span>模块与类库打包,需要调用的时候只需要<span style="color: black;">运用</span> java -jar jar名字 <span style="color: black;">就可</span>。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">首要</span>点开IDEA的Project Structure之后找到Artifacts选项</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/20299089d6944abbb29dbe57bf6d7c7c~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1727628747&x-signature=uOsAF2i3sp35CgomvRbhRsGO6Cw%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">点击Bulid,将项目进行打包</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/7501dbdf01c843d08562d700a7e841c1~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1727628747&x-signature=7iPPiV8R74jTEAhWNRBck%2B1CIc8%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">最后回产生一个out文件夹,<span style="color: black;">这儿</span>面的jar包<span style="color: black;">亦</span><span style="color: black;">便是</span><span style="color: black;">咱们</span>打包后的<span style="color: black;">最后</span>结果。</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/268895fa103146e8897a5298df99bcc6~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1727628747&x-signature=PisJCdtcPR7AKk5kfzyQER1p7uk%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">之后上传到有java环境的终端就<span style="color: black;">能够</span>运行。</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/2b630248b4f140de964603f62c956e95~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1727628747&x-signature=AIvgyP7FfhRpSUIvW4wcEdSK%2Fbc%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">打包完成后<span style="color: black;">咱们</span>的<span style="color: black;">设备</span>人项目就完成啦,希望小伙伴们<span style="color: black;">经过</span>这篇博文<span style="color: black;">能够</span>有所收获。</span></span></p>
你的见解独到,让我受益匪浅,非常感谢。 楼主节操掉了,还不快捡起来! 你的见解独到,让我受益匪浅,非常感谢。 “NB”(牛×的缩写,表示叹为观止)
页:
[1]