ソースを参照

ai actions relevation fix

milan jurkulák 2 年 前
親
コミット
8f6728b9a3

+ 4 - 4
src/main/kotlin/eu/mjdev/desktop/extensions/Custom.kt

@@ -96,10 +96,10 @@ object Custom {
         ext: String = DesktopFile.EXTENSION
     ): List<DesktopFile> = listFiles(ext) { f -> DesktopFile(f) }
 
-//    inline fun <reified T : Any> MutableList<T>.addIfNotExists(
-//        element: T,
-//        equal: (e1: T, e2: T) -> Boolean = { e1, e2 -> e1 == e2 }
-//    ) = any { e -> equal(e, element) }.also { contains -> if (!contains) add(element) }
+    inline fun <reified T : Any> MutableList<T>.addIfNotExists(
+        element: T,
+        equal: (e1: T, e2: T) -> Boolean = { e1, e2 -> e1 == e2 }
+    ) = any { e -> equal(e, element) }.also { contains -> if (!contains) add(element) }
 
     operator fun File.get(name: String) =
         resolve(name)

+ 29 - 9
src/main/kotlin/eu/mjdev/desktop/managers/artificialintelligence/actions/ActionsProvider.kt

@@ -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
+            }
         }
     }
 }

+ 3 - 3
src/main/kotlin/eu/mjdev/desktop/managers/artificialintelligence/actions/base/Action.kt

@@ -10,12 +10,12 @@ package eu.mjdev.desktop.managers.artificialintelligence.actions.base
 
 // todo history with command strings to analyze best result
 @Suppress("unused")
-class Action(
+data class Action(
     val name: String,
     val text: String,
     val regExp: String? = null,
     val responseSuccess: String = "OK",
     val action: suspend ActionProviderScope.() -> ActionException,
-    var lastSeen: Long = 0
-//    val history : ActionHistory
+    var lastSeen: Long = 0,
+    var history: MutableList<String> = mutableListOf()
 )