Commit 1a947679 authored by xuchaozou's avatar xuchaozou

feat: 完成语音打开菜单

parent f0ecb243
Pipeline #85721 passed with stages
...@@ -115,7 +115,7 @@ ...@@ -115,7 +115,7 @@
"@wisdom-map/arcgismap": "1.4.0-261", "@wisdom-map/arcgismap": "1.4.0-261",
"@wisdom-map/basemap": "1.1.0-41", "@wisdom-map/basemap": "1.1.0-41",
"@wisdom-map/util": "^1.0.28-0", "@wisdom-map/util": "^1.0.28-0",
"@wisdom-utils/components": "0.1.349", "@wisdom-utils/components": "0.1.350",
"@wisdom-utils/runtime": "0.0.48", "@wisdom-utils/runtime": "0.0.48",
"@wisdom-utils/utils": "0.1.388", "@wisdom-utils/utils": "0.1.388",
"@congcongcai/jieba.js" : "0.0.3", "@congcongcai/jieba.js" : "0.0.3",
......
...@@ -25,11 +25,9 @@ const Dialog = props => { ...@@ -25,11 +25,9 @@ const Dialog = props => {
content : "系统正在思考中,请稍后..." content : "系统正在思考中,请稍后..."
}) })
const msg = await directive.excute() const msg = await directive.excute()
setTimeout(() => { updateLastData({
updateLastData({ content : msg
content : msg })
})
}, 1000)
pandaRecordWebSocketRef.current.setStatus("playing") pandaRecordWebSocketRef.current.setStatus("playing")
return return
} }
...@@ -40,8 +38,18 @@ const Dialog = props => { ...@@ -40,8 +38,18 @@ const Dialog = props => {
pandaRecordWebSocketRef.current.setStatus("playing") pandaRecordWebSocketRef.current.setStatus("playing")
}, []) }, [])
useEffect(() => {
if(!ulRef.current.lastChild) return
ulRef.current.lastChild.scrollIntoView({
block: 'end',
behavior: 'smooth'
})
}, [data])
const frame = useCallback((data) => { const frame = useCallback((data) => {
const {resultText , resultTextTemp} = data const {resultText , resultTextTemp} = data
console.log(resultTextTemp)
console.log(resultText)
setData(data => { setData(data => {
const length = data.length const length = data.length
const lastData = data.at(-1) const lastData = data.at(-1)
......
...@@ -7,7 +7,7 @@ import Dialog from '../Dialog' ...@@ -7,7 +7,7 @@ import Dialog from '../Dialog'
const PandaTip = props => { const PandaTip = props => {
const [status, setStatus] = useState("open") const [status, setStatus] = useState("close")
const directiveRef = useRef(null) const directiveRef = useRef(null)
const {pandaRecordWebSocketRef} = props const {pandaRecordWebSocketRef} = props
const finish = useCallback(({resultText}) => { const finish = useCallback(({resultText}) => {
......
...@@ -235,7 +235,7 @@ class PandaRecordWebSocket extends PandaWebSocket { ...@@ -235,7 +235,7 @@ class PandaRecordWebSocket extends PandaWebSocket {
} }
if (jsonData.code == 0 && jsonData.data.status == 2) { if (jsonData.code == 0 && jsonData.data.status == 2) {
this.events.finish({ this.events.finish({
resultText: this.resultText, resultText: this.resultTextTemp ? this.resultTextTemp : this.resultText,
}); });
this.reconnect(); this.reconnect();
} }
......
...@@ -5,6 +5,7 @@ class BaseDirective { ...@@ -5,6 +5,7 @@ class BaseDirective {
this.directives = options.directives this.directives = options.directives
this.params = options.params || {} this.params = options.params || {}
this.data = null this.data = null
this.matchResult = null
} }
match () { match () {
......
import BaseDirective from "./BaseDirective"; import BaseDirective from "./BaseDirective";
import {store , event} from '@wisdom-utils/utils'
class CloseMenuDirective extends BaseDirective { class CloseMenuDirective extends BaseDirective {
match () { match () {
...@@ -6,12 +7,12 @@ class CloseMenuDirective extends BaseDirective { ...@@ -6,12 +7,12 @@ class CloseMenuDirective extends BaseDirective {
let result = null let result = null
result = text.match(/关闭(.*)(?=(?:菜单|功能|路径))/) result = text.match(/关闭(.*)(?=(?:菜单|功能|路径))/)
if (!result) { if (!result) {
result = text.match(/关闭(.*)(?=(?:,|,|。|))/) result = text.match(/关闭(.*)(?=(?:,|,|。))/)
} }
if (result && result.length >= 2) { if (result && result.length >= 2 && !this.matchResult) {
this.matchResult = result[1] this.matchResult = result[1]
} }
return !!(result && result.length >= 2) return !!this.matchResult
} }
async excute () { async excute () {
...@@ -19,16 +20,45 @@ class CloseMenuDirective extends BaseDirective { ...@@ -19,16 +20,45 @@ class CloseMenuDirective extends BaseDirective {
if(this.data) { if(this.data) {
} else { } else {
widgets = this.getCompleteMenus() widgets = this.getIsOneWdiget()
if(!widgets) {
widgets = this.getCompleteMenus()
}
if(!widgets) {
return "已打开菜单中没有查找到此菜单,请检查语音是否正确"
}
if(widgets.length == 1) {
event.emit("event:closeMenuByName", widgets[0].name)
return `关闭${widgets[0].name}菜单成功`
} else {
this.data = {}
this.data.type = "widgets"
this.data.widgets = widgets
return this.widgetsToMsg(widgets)
}
} }
} }
widgetsToMsg (widgets) {
return `有以下${widgets.length}个菜单打开\n${widgets.map((widget, index) => `${index + 1}.${widget.name}`).join("\n")},\n请语音确认一下关闭第几个菜单`
}
getCompleteMenus () { getCompleteMenus () {
const result = Utils.findMenus({ const widgets = this.getOpenedMenus()
name: this.matchResult const reg = new RegExp(this.matchResult)
}) const results = widgets.filter(i => reg.test(i.name))
const { widgets } = result return results.length > 0 ? results : null
// return widgets.length > 0 ? widgets : null }
getIsOneWdiget () {
const widgets = this.getOpenedMenus()
const widget = widgets.find(i => i.name == this.matchResult)
return widget ? [widget] : null
}
getOpenedMenus() {
const menus = store.get("event:openedMenus")
return Array.from(menus.current.values())
} }
} }
......
...@@ -7,21 +7,54 @@ class OpenMenuDirective extends BaseDirective { ...@@ -7,21 +7,54 @@ class OpenMenuDirective extends BaseDirective {
match() { match() {
const { text } = this const { text } = this
let result = null let result = null
result = text.match(/打开(.*)(?=(?:菜单|功能|路径))/) if(this.getBeforeLastGroupDirective()) {
result = text.match(/打开.*([一|二|三|四|五|六|七|八|九|十]+)/)
if(result && result.length >= 2) {
this.matchResult = Utils.chineseToNumber(result[1])
} else if(!result) {
result = text.match(/打开[\u4e00-\u9fa5]?(\d+)/)
if(result && result.length >= 2) {
this.matchResult = Number(result[1])
}
}
}
if(!result) {
result = text.match(/打开(.*)(?=(?:菜单|功能|路径))/)
}
if (!result) { if (!result) {
result = text.match(/打开(.*)(?=(?:,|,|。|))/) result = text.match(/打开(.*)(?=(?:,|,|。|))/)
} }
if (result && result.length >= 2) { if (result && result.length >= 2 && !this.matchResult) {
this.matchResult = result[1] this.matchResult = result[1]
} }
return !!(result && result.length >= 2) return !!this.matchResult
}
getBeforeLastGroupDirective () {
const lastMenu = this.directives.at(-1)
if(!lastMenu) return false
if(!lastMenu instanceof OpenMenuDirective ) return false
if(!lastMenu.data || lastMenu.data.type != "widgets" || lastMenu.data.widgets.length == 0) return false
return true
} }
async excute() { async excute() {
let widgets = null let widgets = null
if (this.data) { if(typeof this.matchResult == "number") {
const lastMenu = this.directives.at(-2)
} else { const widgets = lastMenu.data.widgets
if(this.matchResult == 0 || this.matchResult > widgets.length) {
this.data = {}
this.data.type = "widgets"
this.data.widgets = widgets
return `索引超出了界限,请重新语音选择第几个菜单`
} else {
const currentMenu = widgets[this.matchResult - 1]
this.openSingleMenu(currentMenu)
return `成功打开${currentMenu.label}菜单`
}
}
else {
widgets = this.getCompleteMenus() widgets = this.getCompleteMenus()
if (!widgets) { if (!widgets) {
widgets = this.getPatternMenus() widgets = this.getPatternMenus()
...@@ -38,7 +71,6 @@ class OpenMenuDirective extends BaseDirective { ...@@ -38,7 +71,6 @@ class OpenMenuDirective extends BaseDirective {
return `${this.widgetsToMsg(widgets)}` return `${this.widgetsToMsg(widgets)}`
} }
} }
//先判断一下是否能完全匹配一个菜单
} }
handleOneSingleMenu(widget) { handleOneSingleMenu(widget) {
...@@ -54,7 +86,7 @@ class OpenMenuDirective extends BaseDirective { ...@@ -54,7 +86,7 @@ class OpenMenuDirective extends BaseDirective {
} }
widgetsToMsg (widgets) { widgetsToMsg (widgets) {
return `有以下${widgets.length - 1}个菜单\n${widgets.map((widget, index) => `${index + 1}.${widget.label}`).join("\n")},\n请语音确认一下打开第几个菜单` return `有以下${widgets.length}个菜单\n${widgets.map((widget, index) => `${index + 1}.${widget.label}`).join("\n")},\n请语音确认一下打开第几个菜单`
} }
getCompleteMenus() { getCompleteMenus() {
......
import AwakenDirective from "./AwakenDirective" import AwakenDirective from "./AwakenDirective"
import OpenMenuDirective from "./OpenMenuDirective" import OpenMenuDirective from "./OpenMenuDirective"
import HomePageDirective from "./HomePageDirective" import HomePageDirective from "./HomePageDirective"
import CloseMenuDirective from "./CloseMenuDirective"
const DirectiveTypes = [ const DirectiveTypes = [
AwakenDirective, AwakenDirective,
HomePageDirective, HomePageDirective,
OpenMenuDirective OpenMenuDirective,
CloseMenuDirective
] ]
class Directive { class Directive {
constructor (options = {}) { constructor (options = {}) {
......
...@@ -56,31 +56,66 @@ const Utils = { ...@@ -56,31 +56,66 @@ const Utils = {
} }
return realLength; return realLength;
}, },
findMenus ({ name }) { findMenus({ name }) {
const result = { const result = {
widgets : [], widgets: [],
} }
window.globalConfig.widgets.map(widget => this._findMenus({widget, name , result})) window.globalConfig.widgets.map(widget => this._findMenus({ widget, name, result }))
return result return result
}, },
_findMenus({widget, name , result}) { _findMenus({ widget, name, result }) {
if(widget.hideInMenu) return if (widget.hideInMenu) return
if( name instanceof RegExp) { if (name instanceof RegExp) {
const isPattern = name.test(widget.label) const isPattern = name.test(widget.label)
if(isPattern) { if (isPattern) {
result.widgets.push(widget) result.widgets.push(widget)
} }
} else if(name == widget.label){ } else if (name == widget.label) {
result.widgets.push(widget) result.widgets.push(widget)
} }
if(widget.widgets && Array.isArray(widget.widgets)) { if (widget.widgets && Array.isArray(widget.widgets)) {
return widget.widgets.map(widget => this._findMenus({widget , name , result})) return widget.widgets.map(widget => this._findMenus({ widget, name, result }))
} }
}, },
openMenu ({widget = {}, url}) { openMenu({ widget = {}, url }) {
if(!((widget.url && widget.product) || url)) return if (!((widget.url && widget.product) || url)) return
const _url = url || `${widget.product}/${widget.url}` const _url = url || `${widget.product}/${widget.url}`
window.history.pushState(null, '', `/civbase/${_url}`); window.history.pushState(null, '', `/civbase/${_url}`);
},
chineseToNumber(str) {
if (str.length == 1) {
return this._chineseToNumber(str[0])
} else if (str.length == 3) {
const unit = this._chineseToNumber(str[0])
const decade = this._chineseToNumber(str[1])
const threed = this._chineseToNumber(str[2])
return decade * unit + threed
}
return 0
},
_chineseToNumber(str) {
switch (str) {
case "一":
return 1
case "二":
return 2
case "三":
return 3
case "四":
return 4
case "五":
return 5
case "六":
return 6
case "七":
return 7
case "八":
return 8
case "九":
return 9
case "十":
return 10
}
} }
}; };
......
...@@ -72,8 +72,14 @@ const SoundAi = props => { ...@@ -72,8 +72,14 @@ const SoundAi = props => {
} }
} }
}) })
// await testText("我需要打开铁山全景图菜单") // await testText("我需要打开浩源孪生平台菜单")
// await testText("我需要返回首页") // await testText("我需要返回首页")
// await testText("我需要打开礼泉孪生平台菜单")
// await testText("我需要打开浩源孪生平台菜单")
// await testText("我需要关闭平台菜单")
// await testText("我需要打开孪生平台菜单")
// await testText("我需要打开第二个菜单")
// await testText("我需要打开浩源孪生平台菜单")
} }
const testText = text => new Promise((resolve , reject) => { const testText = text => new Promise((resolve , reject) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment