android129 zhihuibeijing 聊天机器人
上屏幕界面activity_main.xml:
- 语音识别界面
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ListView
- android:id="@+id/lv_list"
- android:layout_width="wrap_content"
- android:layout_height="0dp"
- android:divider="@null"
- android:layout_weight="1" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/bottom_bar"
- android:gravity="center"
- android:orientation="vertical" >
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dp"
- android:background="@drawable/btn_selector"
- android:onClick="startListen"
- android:text="点击开始语音识别"
- android:textColor="#000"
- android:textSize="16sp" />
- </LinearLayout>
- </LinearLayout>
MainActivity:
- package com.itheima.voicerobot;
- import java.util.ArrayList;
- import java.util.Random;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
- import com.google.gson.Gson;
- import com.iflytek.cloud.RecognizerResult;
- import com.iflytek.cloud.SpeechConstant;
- import com.iflytek.cloud.SpeechError;
- import com.iflytek.cloud.SpeechSynthesizer;
- import com.iflytek.cloud.SpeechUtility;
- import com.iflytek.cloud.ui.RecognizerDialog;
- import com.iflytek.cloud.ui.RecognizerDialogListener;
- import com.itheima.voicerobot.VoiceBean.CWBean;
- import com.itheima.voicerobot.VoiceBean.WSBean;
- public class MainActivity extends Activity {
- private ListView lvList;
- private ArrayList<ChatBean> mChatList = new ArrayList<ChatBean>();
- /*public class ChatBean {
- public String text;// 内容
- public boolean isAsker;// true表示提问者,否则是回答者
- public int imageId = -1;// 图片id
- public ChatBean(String text, boolean isAsker, int imageId) {
- this.text = text;
- this.isAsker = isAsker;
- this.imageId = imageId;
- }
- }*/
- private ChatAdapter mAdapter;
- private String[] mMMAnswers = new String[] { "约吗?", "讨厌!", "不要再要了!",
- "这是最后一张了!", "漂亮吧?" };
- private int[] mMMImageIDs = new int[] { R.drawable.p1, R.drawable.p2,
- R.drawable.p3, R.drawable.p4 };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- lvList = (ListView) findViewById(R.id.lv_list);
- mAdapter = new ChatAdapter();
- lvList.setAdapter(mAdapter);
- // 初始化语音引擎
- SpeechUtility.createUtility(this, SpeechConstant.APPID + "=54b8bca3");
- }
- StringBuffer mTextBuffer = new StringBuffer();
- //语音识别
- private RecognizerDialogListener recognizerDialogListener = new RecognizerDialogListener() {
- @Override
- public void onResult(RecognizerResult results, boolean isLast) {
- // System.out.println(results.getResultString());
- // System.out.println("isLast=" + isLast);
- String text = parseData(results.getResultString());
- mTextBuffer.append(text);
- if (isLast) {// 会话结束
- String finalText = mTextBuffer.toString();
- mTextBuffer = new StringBuffer();// 清理buffer
- System.out.println("最终结果:" + finalText);
- mChatList.add(new ChatBean(finalText, true, -1));
- String answer = "没听清";
- int imageId = -1;
- if (finalText.contains("你好")) {
- answer = "大家好,才是真的好!";
- } else if (finalText.contains("你是谁")) {
- answer = "我是你的小助手!";
- } else if (finalText.contains("天王盖地虎")) {
- answer = "小鸡炖蘑菇";
- imageId = R.drawable.m;
- } else if (finalText.contains("美女")) {
- Random random = new Random();
- int i = random.nextInt(mMMAnswers.length);//返回0,1,2,3
- int j = random.nextInt(mMMImageIDs.length);
- answer = mMMAnswers[i];
- imageId = mMMImageIDs[j];
- }
- mChatList.add(new ChatBean(answer, false, imageId));// 添加回答数据
- mAdapter.notifyDataSetChanged();// 刷新listview
- lvList.setSelection(mChatList.size() - 1);// 定位到最后一张
- read(answer);
- }
- }
- @Override
- public void onError(SpeechError arg0) {
- }
- };
- /**
- * 语音朗诵
- */
- public void read(String text) {
- SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(this, null);
- mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
- mTts.setParameter(SpeechConstant.SPEED, "50");
- mTts.setParameter(SpeechConstant.VOLUME, "80");
- mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
- mTts.startSpeaking(text, null);
- }
- /**
- * 开始语音识别,说完话之后开始识别。
- */
- public void startListen(View view) {
- RecognizerDialog iatDialog = new RecognizerDialog(this, null);
- // 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
- iatDialog.setParameter(SpeechConstant.DOMAIN, "iat");
- iatDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
- iatDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
- iatDialog.setListener(recognizerDialogListener);
- iatDialog.show();
- }
- class ChatAdapter extends BaseAdapter {
- @Override
- public int getCount() {
- return mChatList.size();
- }
- @Override
- public ChatBean getItem(int position) {
- return mChatList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if (convertView == null) {
- holder = new ViewHolder();
- convertView = View.inflate(MainActivity.this,R.layout.list_item, null);
- //list_item.xml
- /*<?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- 提问的文本框
- <TextView
- android:id="@+id/tv_ask"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true" 居右
- android:layout_alignParentTop="true"
- android:layout_margin="5dp"
- android:background="@drawable/asker_bubble" 气泡图片
- android:gravity="center"
- android:text="你吃饭了吗?"
- android:textColor="#000"
- android:textSize="16sp" />
- 回答的文本框
- <LinearLayout
- android:id="@+id/ll_answer"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/tv_ask" 回答的内容在提问的下面
- android:layout_margin="5dp"
- android:background="@drawable/answer_bubble"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tv_answer"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="5dp"
- android:text="吃个毛线啊!" 回答的内容
- android:textColor="#000"
- android:textSize="16sp" />
- <ImageView
- android:id="@+id/iv_pic"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" 回答时携带的图片
- android:src="@drawable/m"
- android:visibility="gone" />
- </LinearLayout>
- </RelativeLayout>*/
- holder.tvAsk = (TextView) convertView.findViewById(R.id.tv_ask);
- holder.tvAnswer = (TextView) convertView.findViewById(R.id.tv_answer);
- holder.llAnswer = (LinearLayout) convertView.findViewById(R.id.ll_answer);
- holder.ivPic = (ImageView) convertView.findViewById(R.id.iv_pic);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- ChatBean item = getItem(position);
- if (item.isAsker) {// 是提问者
- holder.tvAsk.setVisibility(View.VISIBLE);//问题显示
- holder.llAnswer.setVisibility(View.GONE);//回答隐藏
- holder.tvAsk.setText(item.text);
- } else {
- holder.tvAsk.setVisibility(View.GONE);
- holder.llAnswer.setVisibility(View.VISIBLE);
- holder.tvAnswer.setText(item.text);
- if (item.imageId != -1) {// 有图片
- holder.ivPic.setVisibility(View.VISIBLE);
- holder.ivPic.setImageResource(item.imageId);
- } else {
- holder.ivPic.setVisibility(View.GONE);
- }
- }
- return convertView;
- }
- }
- static class ViewHolder {
- public TextView tvAsk;
- public TextView tvAnswer;
- public LinearLayout llAnswer;
- public ImageView ivPic;
- }
- /**
- * 解析语音数据
- */
- protected String parseData(String resultString) {
- Gson gson = new Gson();
- VoiceBean bean = gson.fromJson(resultString, VoiceBean.class);
- /*
- * 语音信息封装
- *
- public class VoiceBean {
- public ArrayList<WSBean> ws;
- public class WSBean {
- public ArrayList<CWBean> cw;
- }
- public class CWBean {
- public String w;
- }
- }*/
- ArrayList<WSBean> ws = bean.ws;
- StringBuffer sb = new StringBuffer();
- for (WSBean wsBean : ws) {
- String text = wsBean.cw.get(0).w;
- sb.append(text);
- }
- return sb.toString();
- }
- }
json格式数据:
清单文件:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.itheima.voicerobot"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
- <!-- 连接网络权限,用于执行云端语音能力 -->
- <uses-permission android:name="android.permission.INTERNET" />
- <!-- 获取手机录音机使用权限,听写、识别、语义理解需要用到此权限 -->
- <uses-permission android:name="android.permission.RECORD_AUDIO" />
- <!-- 读取网络信息状态 -->
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <!-- 获取当前wifi状态 -->
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <!-- 允许程序改变网络连接状态 -->
- <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
- <!-- 读取手机信息权限 -->
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <!-- 读取联系人权限,上传联系人需要用到此权限 -->
- <uses-permission android:name="android.permission.READ_CONTACTS" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.itheima.voicerobot.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
android129 zhihuibeijing 聊天机器人的更多相关文章
- 【翻译】用AIML实现的Python人工智能聊天机器人
前言 用python的AIML包很容易就能写一个人工智能聊天机器人. AIML是Artificial Intelligence Markup Language的简写, 但它只是一个简单的XML. 下面 ...
- 3.C#面向对象基础聊天机器人
基于控制台的简单版的聊天机器人,词库可以自己添加. 聊天机器人1.0版本 源码如下: using System; using System.Collections.Generic; using Sys ...
- Python 简易聊天机器人
聊天机器人 | |-----MySql | |---module--"逻辑运算层" | | | |---ciku--"与词库交互" | | | |---dict ...
- 使用图灵机器人API实现聊天机器人
使用图灵机器人的API需要先注册,获取key才行,这我就不说了,自己到http://www.tuling123.com/注册一个账号即可. 下面就是一个简单的python调用API实现聊天机器人的简易 ...
- AngularJS作出简单聊天机器人
简单聊天机器人 很初级的对话框形式.以前做对话框使用js,今天尝试使用AngularJS做出来 这里直接使用自己写的JSON数据. <!DOCTYPE html> <html lan ...
- 用 AIML 开发人工智能聊天机器人
借助 Python 的 AIML 包,我们很容易实现人工智能聊天机器人.AIML 指的是 Artificial Intelligence Markup Language (人工智能标记语言),它不过是 ...
- 笔记5:QQ群聊天机器人
之前经常在别人群里看到有自动回复消息的机器人. 功能有好多,可以玩各种游戏.觉得还蛮有意思的.. 于是就去请教别人怎么弄得,但是他们都说得好复杂,好高大上,无非就是不想让别人弄 本人是个不会轻易放弃的 ...
- vue-miniQQ——基于Vue2实现的仿手机QQ单页面应用(接入了聊天机器人,能够进行正常对话)
使用Vue2进行的仿手机QQ的webapp的制作,作品由个人独立开发,源码中进行了详细的注释. 由于自己也是初学Vue2,所以注释写的不够精简,请见谅. 项目地址 https://github.com ...
- 学习笔记TF059:自然语言处理、智能聊天机器人
自然语言处理,语音处理.文本处理.语音识别(speech recognition),让计算机能够"听懂"人类语音,语音的文字信息"提取". 日本富国生命保险公司 ...
随机推荐
- Bad Request (Invalid Hostname)解决方法
当在Windows Server 2003+IIS6做Web服务器,出现打开如http://paullevi.oicp.net,出现,Bad Request (Invalid Hostname) 的提 ...
- ↗☻【HTML5秘籍 #BOOK#】第1章 HTML5简介
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- 嵌入式Linux USB WIFI驱动的移植
硬件平台:飞思卡尔MX258开发板 操作系统:Linux2.6.31 WIFI: RT2860 USB WIFI模组 交叉编译环境:gcc version 4.1.2 调试步骤: 第一步:测试U ...
- WPF学习笔记 - 在XAML里绑定
Binding除了默认构造函数外,还有一个可以传入Path的构造函数,下面两种方式实现的功能是一样的. <TextBlock x:Name="currentFolder" D ...
- 每天学点linux命令--tail,cut,sort,uniq
tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新, ...
- MSP430开学的序章
吐槽一下最近在搞什么~~~,星期三在等板子来,自己的板子,激动呀!!F5系列的板子,激动呀!结果板子到星期五才拿到!开始的时候,感觉自己没多大问题,结果一上手就问题百出,因为没仔细看用户手册,导致光盘 ...
- C: Answers to “The C programming language, Edition 2”
<The C programming language> Edition 2的习题答案地址: http://users.powernet.co.uk/eton/kandr2/index.h ...
- ASP.NET服务器端控件(class0617)
ASP.Net服务端基本控件介绍 ASP.Net服务端控件是ASP.Net对HTML的封装,在C#代码中就可以用txt1.Text=‘abc’这种方式来修改input的值,ASP.Net会将服务端控件 ...
- Codeforces Round #355 (Div. 2)
A 弯腰 #include<cstdio> #include<cstring> #include<iostream> #include<queue> # ...
- Bzoj 4591: [Shoi2015]超能粒子炮·改 数论,Lucas定理,排列组合
4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 178 Solved: 70[Submit][Stat ...