From ab9df0b6ddcc21ac7309071d3cec2047a15bf65c Mon Sep 17 00:00:00 2001 From: gutrse3321 Date: Mon, 25 Jun 2018 15:32:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=A9=E5=87=8Fchatroom=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=88=B0core=E6=96=87=E4=BB=B6=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .electron-vue/webpack.renderer.config.js | 1 + src/renderer/core/index.js | 93 +++++++++ src/renderer/core/tools.js | 127 ++++++++++++ src/renderer/main.js | 8 +- src/renderer/views/Home/chatroom.vue | 251 ++--------------------- src/renderer/views/Home/index.styl | 145 +++++++++++++ 6 files changed, 384 insertions(+), 241 deletions(-) create mode 100644 src/renderer/core/tools.js create mode 100644 src/renderer/views/Home/index.styl diff --git a/.electron-vue/webpack.renderer.config.js b/.electron-vue/webpack.renderer.config.js index f52186e..4912438 100644 --- a/.electron-vue/webpack.renderer.config.js +++ b/.electron-vue/webpack.renderer.config.js @@ -133,6 +133,7 @@ let rendererConfig = { resolve: { alias: { '@': path.join(__dirname, '../src/renderer'), + 'datastore': path.join(__dirname, '../src/datastore'), 'vue$': 'vue/dist/vue.esm.js' }, extensions: ['.js', '.vue', '.json', '.css', '.node'] diff --git a/src/renderer/core/index.js b/src/renderer/core/index.js index e69de29..d66e567 100644 --- a/src/renderer/core/index.js +++ b/src/renderer/core/index.js @@ -0,0 +1,93 @@ +import db from 'datastore' +import Segment from 'segment' +const segment = new Segment() +segment.useDefault() + +export default class MarisaCore { + /** + * 魔理沙与你的说话格式 + * @param {String} name + * @param {String} content + */ + static speak (name, content) { + let obj = { + name: name, + content: content + } + return obj + } + + /** + * 魔理沙的回复逻辑判断中枢 + * @param {String} content + */ + static reply (content) { + let memorise = db.get('memorise').value() + let _pplContent = segment.doSegment(content, {simple: true}) + let answer = '' + let keywords = [] + + // 处理获取的_content到数据库去遍历查询 + // 大于80%就回复对应回答 + for (let i = 0; i < memorise.length; i++) { + let ratio = 0 + keywords = memorise[i].keyword + for (let j = 0; j < keywords.length; j++) { + _pplContent.forEach(ppl => { + if (keywords[j] === ppl) { + ratio++ + } + }) + if ((ratio / keywords.length) >= 0.5) { + answer = memorise[i].answer + break + } + } + } + return answer + } + + /** + * 魔理沙学习中枢 + * @param {String} content + */ + static teach (content) { + // 将you`marisa格式转换为[you,marisa]数组 + let str = content.split('`') + // 将you的句子分词分一个数组 + let toPpl = segment.doSegment(str[0], {simple: true}) + // 获取数据库所有的记忆 + let memorise = db.get('memorise').value() + // 遍历数据库查询匹配的回答 + let keywords = [] + let memorey = {} + for (let i = 0; i < memorise.length; i++) { + let ratio = 0 + keywords = memorise[i].keyword + for (let j = 0; j < keywords.length; j++) { + toPpl.forEach(ppl => { + if (keywords[j] === ppl) { + ratio++ + } + }) + if ((ratio / keywords.length) >= 0.5) { + keywords.concat(toPpl) + // 去除重复的关键词或字 + keywords = Array.from(new Set(keywords.filter((x, i, self) => self.indexOf(x) === i))) + memorey = { + keyword: keywords, + answer: str[1] + } + break + } else { + memorey = { + keyword: toPpl, + answer: str[1] + } + break + } + } + } + return memorey + } +} diff --git a/src/renderer/core/tools.js b/src/renderer/core/tools.js new file mode 100644 index 0000000..cbc5a71 --- /dev/null +++ b/src/renderer/core/tools.js @@ -0,0 +1,127 @@ +/* + * @Author: webees [hi@webees.net] + * @Date: 2017-03-17 23:07:41 + * @Last Modified by: JockLee + * @Last Modified time: 2018-06-24 18:10:17 + */ +export default class Tools { + // 睡眠(毫秒) + sleep (ms) { + return new Promise(resolve => setTimeout(resolve, ms)) + } + + /** + * 获取Dom元素 + * @param return + */ + // const $ = document.querySelector.bind(document) + + // 删除左右两端的空格 + trim (str) { + return str.replace(/(^\s*)|(\s*$)/g, '') + } + + // 删除所有空格 + trimAll (str) { + return str.replace(/\s/g, '') + } + + // 异步加载js,id属性防止重复加载 + loadJS (url, id = null) { + let d = document + let s = 'script' + let js + let fjs = d.getElementsByTagName(s)[0] + if (d.getElementById(id)) return + js = d.createElement(s) + if (id !== null) js.id = id + js.src = url + fjs.parentNode.insertBefore(js, fjs) + } + + /* + * 时间戳转世界时间,传入时间单位秒 + * @format: 时间字符串 'yyyy-MM-dd EEE hh:mm:ss' + * 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, + * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) + * 例子: + * 'yyyy-MM-dd hh:mm:ss.S') ===> 2006-07-02 08:09:04.423 + * 'yyyy-M-d h:m:s.S') ===> 2006-7-2 8:9:4.18 + */ + stamp2utc (time, format) { + format = format === undefined ? 'yyyy-MM-dd hh:mm:ss' : format + let _date = new Date() + _date.setTime(time) + let o = { + 'M+': _date.getMonth() + 1, // 月份 + 'd+': _date.getDate(), // 日 + 'h+': _date.getHours(), // 小时 + 'm+': _date.getMinutes(), // 分 + 's+': _date.getSeconds(), // 秒 + 'q+': Math.floor((_date.getMonth() + 3) / 3), // 季度 + 'S': _date.getMilliseconds() // 毫秒 + } + if (/(y+)/i.test(format)) format = format.replace(RegExp.$1, (_date.getFullYear() + '').substr(4 - RegExp.$1.length)) + for (let k in o) { + if (new RegExp('(' + k + ')').test(format)) format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)) + } + return format + } + + // 获取URL参数 + urlQuery (name) { + var reg = new RegExp('(^|&)?' + name + '=([^&]*)(&|$)') + var r = window.location.href.substr(1).match(reg) + if (r !== null) return unescape(r[2]) + return null + } + + // 去除非数字字符 + onlyNum (str) { + // .replace(/^0+/,'') // 数字前面的零不去除,【bug】验证码前面的零被去除导致验证码错误 + if (str !== null) return str.replace(/[^\d]/ig, '') + } + + // 获取当前时间戳,精确到毫秒 + nowTimestamp () { + return (new Date()).valueOf() + } + + // 格式化数字,例如 5264887 => 5,264,887 + parseNum (str) { + return parseFloat(str).toLocaleString() + } + + /** + * 判断对象是否为空 + * @param {Object} obj 对象 + * @param return + */ + isNullObject (obj) { + for (let i in obj) { + return false + } + return true + } + + /** + * 获取随机数 + * + * @ * @param {any} minNum + * @param {any} maxNum + * @returns + */ + randomNum (minNum, maxNum) { + if (arguments.length === 1) return parseInt(Math.random() * minNum + 1, 10) + else if (arguments.length === 2) return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10) + else return 0 + } + + /** + * 深度拷贝对象 + * @param {*} obj + */ + deepCopy (obj) { + return JSON.parse(JSON.stringify(obj)) + } +} diff --git a/src/renderer/main.js b/src/renderer/main.js index 4310da5..4446b06 100644 --- a/src/renderer/main.js +++ b/src/renderer/main.js @@ -4,9 +4,9 @@ import db from '../datastore' import App from './App' import router from './router' import store from './store' -import Segment from 'segment' -const segment = new Segment() -segment.useDefault() +// import Segment from 'segment' +// const segment = new Segment() +// segment.useDefault() if (!process.env.IS_WEB) Vue.use(require('vue-electron')) Vue.http = Vue.prototype.$http = axios @@ -28,7 +28,7 @@ Vue.directive('focus', { // 注册全局$db Vue.prototype.$db = db // 注册全局$dictionary 分词字典 -Vue.prototype.$dictionary = segment +// Vue.prototype.$dictionary = segment /* eslint-disable no-new */ new Vue({ diff --git a/src/renderer/views/Home/chatroom.vue b/src/renderer/views/Home/chatroom.vue index 27426e0..aa2987d 100644 --- a/src/renderer/views/Home/chatroom.vue +++ b/src/renderer/views/Home/chatroom.vue @@ -42,6 +42,7 @@ diff --git a/src/renderer/views/Home/index.styl b/src/renderer/views/Home/index.styl new file mode 100644 index 0000000..ad49c1a --- /dev/null +++ b/src/renderer/views/Home/index.styl @@ -0,0 +1,145 @@ +@import '~@/assets/stylus/variable'; + +.chatroom { + width: 712px; + height: 512px; + background: $box-color; + border: 1px solid $border-color; +} + +.container { + display: flex; + justify-content: space-between; + margin: 0 auto; + margin-top: 6px; + width: 700px; + height: 502px; +} + +.talk-panel { + width: 470px; + height: 500px; + background: $container-color; + border: 1px solid $border-color; + + span { + display: block; + width: 100%; + height: 40px; + background: $title-color; + border-bottom: 1px solid $border-color; + font-size: 14px; + line-height: 40px; + text-indent: 9px; + color: $order-blue; + } + + .talk-place { + width: clac(100% - 10px); + height: 420px; + padding-left: 10px; + overflow-y: scroll; + + .talk_entry { + width: 100%; + font-size: 14px; + margin-top: 12px; + word-break: break-all; + line-height: 25px; + + .talk_item { + display: inline; + background: transparent; + border: none; + line-height: 0; + } + } + } + + .speak { + width: 100%; + height: 39px; + + input { + background: none; + outline: 0; + border: none; + } + + & input[name='you'] { + position: relative; + top: 10px; + left: 5px; + width: 382px; + height: 24px; + padding-left: 5px; + background: rgba(255, 255, 255, 0.2); + border: 1px solid $border-color; + transition: all 0.2s; + + &:hover, &:focus { + background: rgba(255, 255, 255, 0.6); + border-radius: 5px; + } + } + + & input[type='submit'] { + position: relative; + top: 9px; + left: 15px; + width: 48px; + height: 22px; + background: rgba(255, 255, 255, 0.2); + border: 1px solid $border-color; + border-radius: 5px; + font-size: 12px; + transition: all 0.2s; + } + } +} + +.profile { + width: 220px; + height: 502px; + + .avatar { + width: 218px; + height: 250px; + border: 1px solid $border-color; + background-image: url('./image/marisa.jpg'); + background-size: 333px; + background-repeat: no-repeat; + background-position: 58% 2%; + margin-bottom: 8px; + } + + .cmd { + width: 202px; + height: 232px; + padding: 4px 8px; + background: $container-color; + border: 1px solid $border-color; + color: $order-blue; + font-size: 12px; + + .system-cmd { + display: block; + width: 100%; + margin-bottom: 13px; + margin-top: 13px; + } + + .cmd-collect { + text-indent: 1em; + } + + .marisa-cmd { + display: inline; + font-weight: bold; + } + } +} + +.you_color { + color: #836FFF !important; +}