-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable step up and implement ranking #66
Changes from 4 commits
be98e4d
cec2fe8
1d9541f
72342f0
567c838
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.justai.jaicf.model.activation.selection | ||
|
||
import com.justai.jaicf.context.ActivatorContext | ||
import com.justai.jaicf.context.BotContext | ||
import com.justai.jaicf.model.activation.Activation | ||
import com.justai.jaicf.model.transition.Transition | ||
|
||
/** | ||
* A selector, that is used for selecting the most relevant [Activation] from all possible. | ||
* | ||
* @see [com.justai.jaicf.activator.Activator] | ||
* @see [ContextFirstActivationSelector] | ||
* @see [ContextRankingActivationSelector] | ||
*/ | ||
interface ActivationSelector { | ||
|
||
/** | ||
* Receives a list of all possible transitions and selects the most relevant one. | ||
* | ||
* @param botContext a current [BotContext]. | ||
* @param activations a list of pairs of [Transition] and [ActivatorContext] to choose from. | ||
* | ||
* @return [Activation] that is built from the most relevant transition and context, | ||
* or `null` if no [Activation] should be selected. | ||
*/ | ||
fun selectActivation( | ||
botContext: BotContext, | ||
activations: List<Pair<Transition, ActivatorContext>> | ||
): Activation? | ||
|
||
companion object { | ||
val default = ContextFirstActivationSelector() | ||
} | ||
} | ||
|
||
val Transition.isFromRoot get() = fromState == "/" | ||
fun Transition.isFrom(from: String) = fromState == from | ||
fun Transition.isTo(to: String) = toState == to |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.justai.jaicf.model.activation.selection | ||
|
||
import com.justai.jaicf.context.ActivatorContext | ||
import com.justai.jaicf.context.BotContext | ||
import com.justai.jaicf.model.activation.Activation | ||
import com.justai.jaicf.model.transition.Transition | ||
|
||
/** | ||
* Selects the most relevant activation, based on context specificity first and then confidence. | ||
* | ||
* Tries to select the most confident transition from current state to some of its child, | ||
* if no children available tries to stay in the same state, otherwise tries to select best transition | ||
* available from scenario root. | ||
*/ | ||
class ContextFirstActivationSelector : ActivationSelector { | ||
override fun selectActivation( | ||
botContext: BotContext, | ||
activations: List<Pair<Transition, ActivatorContext>> | ||
): Activation? { | ||
val current = botContext.dialogContext.currentContext | ||
|
||
val toChildren = activations.filter { it.first.isFrom(current) }.maxBy { it.second.confidence } | ||
val toCurrent = activations.filter { it.first.isTo(current) }.maxBy { it.second.confidence } | ||
val fromRoot = activations.filter { it.first.isFromRoot }.maxBy { it.second.confidence } | ||
|
||
val best = toChildren ?: toCurrent ?: fromRoot | ||
return best?.let { Activation(it.first.toState, it.second) } | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.justai.jaicf.model.activation.selection | ||
|
||
import com.justai.jaicf.context.ActivatorContext | ||
import com.justai.jaicf.context.BotContext | ||
import com.justai.jaicf.model.activation.Activation | ||
import com.justai.jaicf.model.transition.Transition | ||
|
||
/** | ||
* Selects the most relevant activation, based on confidence weighting. | ||
* | ||
* Default implementation multiplies activation confidence by a factor that is different | ||
* for different layers of scenario graph. More specific transition means greater factor. | ||
* Specificity is ordered as follows: | ||
* 1. Children of the current state; | ||
* 2. The current state itself; | ||
* 3. States available from the root of scenario. | ||
*/ | ||
open class ContextRankingActivationSelector : ActivationSelector { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not to use this implementation as default selector instead of |
||
override fun selectActivation( | ||
botContext: BotContext, | ||
activations: List<Pair<Transition, ActivatorContext>> | ||
): Activation? { | ||
return activations.maxBy { | ||
calculateScore(botContext, it.first, it.second) | ||
}?.let { Activation(it.first.toState, it.second) } | ||
} | ||
|
||
|
||
/** | ||
* Calculates the score of the given [Transition] and given [ActivatorContext]. | ||
* One can override this method and implement its own scoring mechanism. | ||
*/ | ||
protected open fun calculateScore( | ||
botContext: BotContext, | ||
transition: Transition, | ||
context: ActivatorContext | ||
): Float { | ||
val current = botContext.dialogContext.currentContext | ||
|
||
val factor = when { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why these magic numbers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just by trial-and-error. We have only three sets of states, so we can define constants for them instead of calculating some penalty at runtime |
||
transition.isFrom(current) -> 1f | ||
transition.isTo(current) -> 0.8f | ||
transition.isFromRoot -> 0.6f | ||
else -> 0f | ||
} | ||
|
||
return context.confidence * factor | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure this interface and its implementations should be placed to
model
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can move it to
com.justai.jaicf.activator.selection
, is it okay?