135 lines
4.7 KiB
Go
135 lines
4.7 KiB
Go
package domain
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"git.cin.su/m/Assistant/contracts/interaction"
|
|
)
|
|
|
|
// StripAssistantVocative removes a leading assistant name used as an address.
|
|
// The original utterance remains the historical source; this value is only for
|
|
// interpretation, cognitive prompts and external queries.
|
|
func StripAssistantVocative(message string) string {
|
|
trimmed := strings.TrimSpace(message)
|
|
lower := strings.ToLower(trimmed)
|
|
for _, name := range []string{"дед", "домовой"} {
|
|
if !strings.HasPrefix(lower, name) {
|
|
continue
|
|
}
|
|
rest := trimmed[len(name):]
|
|
if rest == "" || !strings.ContainsRune(" \t,;:!?—-", []rune(rest)[0]) {
|
|
continue
|
|
}
|
|
return strings.TrimSpace(strings.TrimLeft(rest, " \t,;:!?—-"))
|
|
}
|
|
return trimmed
|
|
}
|
|
|
|
// IndependentRequestParts keeps separate user actions visible to the
|
|
// orchestrator. It is intentionally limited to the ordinary coordinating
|
|
// conjunction; the cognitive route remains responsible for ambiguous prose.
|
|
func IndependentRequestParts(message string) []string {
|
|
cleaned := StripAssistantVocative(message)
|
|
parts := strings.Split(cleaned, " и ")
|
|
if len(parts) < 2 {
|
|
return []string{cleaned}
|
|
}
|
|
result := make([]string, 0, len(parts))
|
|
for _, part := range parts {
|
|
part = strings.TrimSpace(part)
|
|
if part != "" {
|
|
result = append(result, part)
|
|
}
|
|
}
|
|
if len(result) < 2 {
|
|
return []string{cleaned}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// LocalSearchClarification prevents a proximity search from silently using a
|
|
// default city when the user did not provide a place.
|
|
func LocalSearchClarification(message string) string {
|
|
tokens := semanticTokens(StripAssistantVocative(message))
|
|
if !containsStemInTokens(tokens, "ресторан", "кафе") || !containsStemInTokens(tokens, "ближ", "рядом") {
|
|
return ""
|
|
}
|
|
for index, token := range tokens {
|
|
if token != "в" && token != "на" && token != "около" && !containsStem(token, "район", "город") {
|
|
continue
|
|
}
|
|
if index+1 < len(tokens) || containsStem(token, "район", "город") {
|
|
return ""
|
|
}
|
|
}
|
|
return "В каком городе или районе искать ресторан?"
|
|
}
|
|
|
|
// ContinueLocalSearch turns a location-only follow-up into the previous local
|
|
// search request, but only when that request is present in this conversation.
|
|
func ContinueLocalSearch(message string, history []interaction.HumanUtterance) string {
|
|
current := strings.TrimSpace(StripAssistantVocative(message))
|
|
tokens := semanticTokens(current)
|
|
if len(tokens) == 0 || len(tokens) > 3 || (tokens[0] != "в" && tokens[0] != "на" && tokens[0] != "около") {
|
|
return ""
|
|
}
|
|
for index := len(history) - 1; index >= 0; index-- {
|
|
previous := strings.TrimSpace(StripAssistantVocative(history[index].Content))
|
|
previousTokens := semanticTokens(previous)
|
|
if containsStemInTokens(previousTokens, "ресторан", "кафе") && containsStemInTokens(previousTokens, "ближ", "рядом") {
|
|
return strings.TrimSpace(previous + " " + current)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// TravelSearchClarification keeps an incomplete travel request in dialogue
|
|
// instead of reducing it to an unrelated generic web search.
|
|
func TravelSearchClarification(message string, history []interaction.HumanUtterance) string {
|
|
current := semanticTokens(StripAssistantVocative(message))
|
|
travel := travelCue(current)
|
|
if !travel && travelFollowUpCue(current) {
|
|
for index := len(history) - 1; index >= 0; index-- {
|
|
if travelCue(semanticTokens(history[index].Content)) {
|
|
travel = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if !travel {
|
|
return ""
|
|
}
|
|
origin := containsStemInTokens(current, "из", "откуда", "от")
|
|
destination := containsStemInTokens(current, "в", "куда", "до")
|
|
date := containsStemInTokens(current, "сегодня", "завтра", "послезавтр", "январ", "феврал", "март", "апрел", "мая", "июн", "июл", "август", "сентябр", "октябр", "ноябр", "декабр")
|
|
if !date {
|
|
for _, token := range current {
|
|
if hasDigit(token) {
|
|
date = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if origin && destination && date {
|
|
return ""
|
|
}
|
|
return "Уточните маршрут и дату: откуда, куда и на какой день нужны билеты?"
|
|
}
|
|
|
|
func travelCue(tokens []string) bool {
|
|
return containsStemInTokens(tokens, "билет", "рейс", "поезд", "автобус", "перелет", "перелёт")
|
|
}
|
|
|
|
func travelFollowUpCue(tokens []string) bool {
|
|
return containsStemInTokens(tokens, "куда", "откуда", "маршрут", "летим", "дата", "число")
|
|
}
|
|
|
|
func hasDigit(value string) bool {
|
|
for _, runeValue := range value {
|
|
if runeValue >= '0' && runeValue <= '9' {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|