Skip to content

Commit

Permalink
Merge pull request #4 from gutrse3321/0.1
Browse files Browse the repository at this point in the history
0.1
  • Loading branch information
gutrse3321 authored Jun 25, 2018
2 parents 2abdee1 + ab9df0b commit 3e5954f
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 238 deletions.
1 change: 1 addition & 0 deletions .electron-vue/webpack.renderer.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
93 changes: 93 additions & 0 deletions src/renderer/core/index.js
Original file line number Diff line number Diff line change
@@ -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
}
}
127 changes: 127 additions & 0 deletions src/renderer/core/tools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* @Author: webees [[email protected]]
* @Date: 2017-03-17 23:07:41
* @Last Modified by: JockLee <[email protected]>
* @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))
}
}
8 changes: 4 additions & 4 deletions src/renderer/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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({
Expand Down
Loading

0 comments on commit 3e5954f

Please sign in to comment.