|
|
@@ -8,6 +8,7 @@
|
|
|
|
|
|
package eu.mjdev.desktop.managers.artificialintelligence.actions
|
|
|
|
|
|
+import eu.mjdev.desktop.extensions.Custom.addIfNotExists
|
|
|
import eu.mjdev.desktop.managers.artificialintelligence.actions.base.Action
|
|
|
import eu.mjdev.desktop.managers.artificialintelligence.actions.base.ActionException
|
|
|
import eu.mjdev.desktop.managers.artificialintelligence.actions.base.ActionException.ActionFail
|
|
|
@@ -17,6 +18,7 @@ import eu.mjdev.desktop.provider.DesktopProvider
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
+import me.xdrop.fuzzywuzzy.FuzzySearch
|
|
|
|
|
|
@Suppress("unused")
|
|
|
class ActionsProvider(
|
|
|
@@ -51,21 +53,39 @@ class ActionsProvider(
|
|
|
)
|
|
|
}
|
|
|
|
|
|
- // todo regexp & fuzzy
|
|
|
+ // todo regexp
|
|
|
fun tryAction(
|
|
|
text: String
|
|
|
): ActionException {
|
|
|
- return text.trim().let { t ->
|
|
|
- firstOrNull { a ->
|
|
|
- a.text.contentEquals(t, true) || a.text.contains(t, true)
|
|
|
- }?.let { a ->
|
|
|
- a.lastSeen = System.currentTimeMillis()
|
|
|
+ return text.trim().lowercase().let { t ->
|
|
|
+ var action = firstOrNull { a ->
|
|
|
+ a.text.contentEquals(t, true) || a.history.contains(t)
|
|
|
+ }
|
|
|
+ if (action != null) {
|
|
|
+ println("got action ${action}, relevance: exact.")
|
|
|
+ } else {
|
|
|
+ val fuzzy: Pair<Int, Action>? = map { a ->
|
|
|
+ Pair(FuzzySearch.ratio(a.text, t), a)
|
|
|
+ }.maxByOrNull { p ->
|
|
|
+ p.first
|
|
|
+ }?.let { p -> if (p.first > 70) p else null }
|
|
|
+ action = fuzzy?.second
|
|
|
+ if (action != null) {
|
|
|
+ println("got action ${fuzzy?.second}, relevance: ${fuzzy?.first}.")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (action != null) {
|
|
|
+ action.lastSeen = System.currentTimeMillis()
|
|
|
+ action.history.addIfNotExists(t) { t1, t2 -> t1.contentEquals(t2, true) }
|
|
|
runBlocking(Dispatchers.IO) {
|
|
|
- a.action.invoke(ActionProviderScope(api)).let { r ->
|
|
|
- if(r is ActionFail) r else ActionSuccess(a.responseSuccess, r)
|
|
|
+ action.action.invoke(ActionProviderScope(api)).let { r ->
|
|
|
+ if (r is ActionFail) r
|
|
|
+ else ActionSuccess(action.responseSuccess, r)
|
|
|
}
|
|
|
}
|
|
|
- } ?: ActionException.ActionNone
|
|
|
+ } else {
|
|
|
+ ActionException.ActionNone
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|