73 lines
2.3 KiB
Go
73 lines
2.3 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
|
|
}
|
|
|
|
// 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 {
|
|
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 hasDigit(value string) bool {
|
|
for _, runeValue := range value {
|
|
if runeValue >= '0' && runeValue <= '9' {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|