From 12dfc535e4d239c9e9aa4b7b7b8ecfd9f3defd19 Mon Sep 17 00:00:00 2001 From: gutrse3321 Date: Sun, 17 Jun 2018 16:31:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=86=E8=AF=8D=E5=AD=97?= =?UTF-8?q?=E5=85=B8=EF=BC=8C=E4=BC=98=E5=8C=96=E5=AD=A6=E4=B9=A0=E4=B8=8E?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/renderer/main.js | 5 + src/renderer/store/modules/Counter.js | 20 +- src/renderer/views/Home/chatroom.vue | 348 ++++++++++++++++---------- yarn.lock | 4 + 5 files changed, 229 insertions(+), 149 deletions(-) diff --git a/package.json b/package.json index d51e45f..e4ed381 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "axios": "^0.16.1", "fs-extra": "^6.0.1", "lowdb": "^1.0.0", + "segment": "^0.1.3", "vue": "^2.3.3", "vue-electron": "^1.0.6", "vue-router": "^2.5.3", diff --git a/src/renderer/main.js b/src/renderer/main.js index 8612449..4310da5 100644 --- a/src/renderer/main.js +++ b/src/renderer/main.js @@ -4,6 +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() if (!process.env.IS_WEB) Vue.use(require('vue-electron')) Vue.http = Vue.prototype.$http = axios @@ -24,6 +27,8 @@ Vue.directive('focus', { // 注册全局$db Vue.prototype.$db = db +// 注册全局$dictionary 分词字典 +Vue.prototype.$dictionary = segment /* eslint-disable no-new */ new Vue({ diff --git a/src/renderer/store/modules/Counter.js b/src/renderer/store/modules/Counter.js index 44a8ab4..9697463 100644 --- a/src/renderer/store/modules/Counter.js +++ b/src/renderer/store/modules/Counter.js @@ -1,22 +1,8 @@ -const state = { - main: 0 -} +const state = {} -const mutations = { - DECREMENT_MAIN_COUNTER (state) { - state.main-- - }, - INCREMENT_MAIN_COUNTER (state) { - state.main++ - } -} +const mutations = {} -const actions = { - someAsyncTask ({ commit }) { - // do something async - commit('INCREMENT_MAIN_COUNTER') - } -} +const actions = {} export default { state, diff --git a/src/renderer/views/Home/chatroom.vue b/src/renderer/views/Home/chatroom.vue index 76f6e2d..1530260 100644 --- a/src/renderer/views/Home/chatroom.vue +++ b/src/renderer/views/Home/chatroom.vue @@ -4,18 +4,13 @@
うるさい! うるさい.. うるさい...
-
-  :  +
+  :  +
- +
@@ -29,13 +24,16 @@ teach 进入内容教学模式 - forget 忘记最后所说的内容 + + forget 忘记最后所说的内容 - application 管理外部应用接口 + + application 管理外部应用接口 - status 查看目前知识所掌握情况 + + status 查看目前知识所掌握情况 另外你也可以通过输入 hint 查看其他人自定义的内容提示或小小线索

魔理沙无条件的相信你..她把你交给她的所有知识视作珍宝并会很认真的将其牢牢记住..不要让她学坏哦! @@ -79,7 +77,7 @@ export default { case 'teach': this.talk_list.push({ name: MARISA, - content: '好哒,你要教我什么呢?格式:You的消息`白絲魔理沙的回复' + content: '
好的,你要教我什么呢?
格式:You的消息`白絲魔理沙的回复' }) this.cmd_flag = 1 break @@ -88,14 +86,29 @@ export default { }, _marisaReply (_content) { let memorise = this.$db.get('memorise').value() + let _pplContent = this.$dictionary.doSegment(_content, {simple: true}) let answer = '' + let keywords = [] + + // 处理获取的_content到数据库去遍历查询 + // 大于80%就回复对应回答 for (let i = 0; i < memorise.length; i++) { - memorise[i].keyword.forEach(item => { - if (item === _content) { + let ratio = 0 + keywords = memorise[i].keyword + for (let j = 0; j < keywords.length; j++) { + _pplContent.forEach(ppl => { + if (keywords[j] === ppl) { + ratio++ + } + }) + console.log('ratio:' + ratio) + if ((ratio / keywords.length) > 0.8) { answer = memorise[i].answer + break } - }) + } } + if (answer !== '') { this.talk_list.push({ name: MARISA, @@ -106,20 +119,55 @@ export default { name: MARISA, content: '唔嗯...不懂你在说什么呢...' }) + console.log(this.$dictionary.doSegment(_content, { + simple: true + })) } }, _teachMarisa (_content) { + // 将you`marisa格式转换为[you,marisa]数组 let str = _content.split('`') - let memorey = { - keyword: [ - str[0] - ], - answer: str[1] + // 将you的句子分词分一个数组 + let toPpl = this.$dictionary.doSegment(str[0], {simple: true}) + // 获取数据库所有的记忆 + let memorise = this.$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 + } + } } + // 插入记忆 this.$db.get('memorise').push(memorey).write() + // 学习模式解除,默认回复,切换到聊天模式 this.talk_list.push({ name: MARISA, - content: `ok,当你说"${str[0]}"的时候,嗯...好!老子就说"${str[1]}"DA☆ZE` + content: '行,我知道了' }) this.cmd_flag = 0 }, @@ -136,120 +184,156 @@ export default { created () { let _startTalk = { name: MARISA, - content: '白絲魔理沙 Type 0.001,还在继续升级DA☆ZE!' + content: '白絲魔理沙 Type 0.003,还在继续升级DA☆ZE!' } this.talk_list.push(_startTalk) } } diff --git a/yarn.lock b/yarn.lock index fb656a6..b9fe3d3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6242,6 +6242,10 @@ schema-utils@^0.3.0: dependencies: ajv "^5.0.0" +segment@^0.1.3: + version "0.1.3" + resolved "http://registry.npm.taobao.org/segment/download/segment-0.1.3.tgz#605f80fd6c7131c46e38103a17b1688f09efafa7" + select-hose@^2.0.0: version "2.0.0" resolved "http://registry.npm.taobao.org/select-hose/download/select-hose-2.0.0.tgz#625d8658f865af43ec962bfc376a37359a4994ca"