289 lines
9.7 KiB
Go
289 lines
9.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 ""
|
|
}
|
|
var latest *interaction.HumanUtterance
|
|
for index := range history {
|
|
candidate := history[index]
|
|
candidateText := strings.TrimSpace(StripAssistantVocative(candidate.Content))
|
|
candidateTokens := semanticTokens(candidateText)
|
|
if !containsStemInTokens(candidateTokens, "ресторан", "кафе") || !containsStemInTokens(candidateTokens, "ближ", "рядом") {
|
|
continue
|
|
}
|
|
if latest == nil || candidate.ReceivedAtUnixMillis > latest.ReceivedAtUnixMillis {
|
|
latest = &candidate
|
|
}
|
|
}
|
|
if latest != nil {
|
|
previous := strings.TrimSpace(StripAssistantVocative(latest.Content))
|
|
return strings.TrimSpace(previous + " " + current)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// ContinueTravelOrigin turns a short location answer into the previous travel
|
|
// task. The original utterance remains history; only the effective task is
|
|
// completed for routing and research.
|
|
func ContinueTravelOrigin(message string, history []interaction.HumanUtterance) string {
|
|
current := semanticTokens(StripAssistantVocative(message))
|
|
if len(current) == 0 || len(current) > 6 {
|
|
return ""
|
|
}
|
|
location := ""
|
|
if current[0] == "я" {
|
|
if len(current) < 3 {
|
|
return ""
|
|
}
|
|
for index, token := range current {
|
|
if (token == "в" || token == "из") && index+1 < len(current) {
|
|
location = capitalizeWord(current[index+1])
|
|
break
|
|
}
|
|
}
|
|
} else if len(current) <= 2 && !isTravelDateWord(current[0]) && len([]rune(current[0])) >= 3 {
|
|
location = capitalizeWord(current[0])
|
|
}
|
|
if location == "" {
|
|
return ""
|
|
}
|
|
var latest *interaction.HumanUtterance
|
|
for index := range history {
|
|
candidate := history[index]
|
|
if !travelContextCue(semanticTokens(candidate.Content)) {
|
|
continue
|
|
}
|
|
if latest == nil || candidate.ReceivedAtUnixMillis > latest.ReceivedAtUnixMillis {
|
|
latest = &candidate
|
|
}
|
|
}
|
|
if latest == nil {
|
|
return ""
|
|
}
|
|
previous := strings.TrimRight(strings.TrimSpace(StripAssistantVocative(latest.Content)), "?!.,;:")
|
|
preposition := " из "
|
|
if current[0] != "я" {
|
|
preposition = " из города "
|
|
}
|
|
return strings.TrimSpace(previous + preposition + location)
|
|
}
|
|
|
|
// 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 {
|
|
travel = containsStemInTokens(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 containsStemInTokens(current, "ближ") {
|
|
date = true
|
|
}
|
|
if !date {
|
|
for _, token := range current {
|
|
if hasDigit(token) {
|
|
date = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if origin && destination && date {
|
|
return ""
|
|
}
|
|
ticketRequest := containsStemInTokens(current, "билет", "рейс", "самолет", "самолёт", "вылет")
|
|
if !ticketRequest && origin && destination {
|
|
return ""
|
|
}
|
|
if !destination {
|
|
if contextDestination := travelContextDestination(history); contextDestination != "" {
|
|
destination = true
|
|
if !origin && !date {
|
|
return "Из какого города вылетать в " + contextDestination + " и на какую дату?"
|
|
}
|
|
if !origin {
|
|
return "Из какого города вылетать в " + contextDestination + "?"
|
|
}
|
|
}
|
|
}
|
|
if !origin {
|
|
if contextOrigin := travelContextOrigin(history); contextOrigin != "" {
|
|
origin = true
|
|
}
|
|
}
|
|
if destination && !origin && !date {
|
|
if !ticketRequest {
|
|
return "Из какого города выезжать в " + destinationLabel(current, history) + "? Подберу самолёт, поезд, автобус или машину."
|
|
}
|
|
return "Из какого города вылетать и на какую дату?"
|
|
}
|
|
if destination && !origin {
|
|
return "Из какого города вылетать?"
|
|
}
|
|
if destination && !date {
|
|
return "На какую дату нужны билеты?"
|
|
}
|
|
return "Уточните маршрут и дату: откуда, куда и на какой день нужны билеты?"
|
|
}
|
|
|
|
func isTravelDateWord(value string) bool {
|
|
return containsStem(value, "сегодня", "завтра", "послезавтр", "январ", "феврал", "март", "апрел", "мая", "июн", "июл", "август", "сентябр", "октябр", "ноябр", "декабр")
|
|
}
|
|
|
|
func destinationLabel(current []string, history []interaction.HumanUtterance) string {
|
|
for index, token := range current {
|
|
if (token == "в" || token == "до") && index+1 < len(current) {
|
|
return capitalizeWord(current[index+1])
|
|
}
|
|
}
|
|
if destination := travelContextDestination(history); destination != "" {
|
|
return destination
|
|
}
|
|
return "пункт назначения"
|
|
}
|
|
|
|
func travelContextDestination(history []interaction.HumanUtterance) string {
|
|
for index := len(history) - 1; index >= 0; index-- {
|
|
tokens := semanticTokens(history[index].Content)
|
|
if !travelContextCue(tokens) {
|
|
continue
|
|
}
|
|
for tokenIndex, token := range tokens {
|
|
if token != "в" && token != "до" {
|
|
continue
|
|
}
|
|
if tokenIndex+1 < len(tokens) && len([]rune(tokens[tokenIndex+1])) >= 3 {
|
|
return capitalizeWord(tokens[tokenIndex+1])
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func travelContextOrigin(history []interaction.HumanUtterance) string {
|
|
for index := len(history) - 1; index >= 0; index-- {
|
|
tokens := semanticTokens(history[index].Content)
|
|
if len(tokens) < 3 || tokens[0] != "я" || !containsStemInTokens(tokens, "сейчас", "нахож", "жив") {
|
|
continue
|
|
}
|
|
for tokenIndex, token := range tokens {
|
|
if (token == "в" || token == "из") && tokenIndex+1 < len(tokens) {
|
|
return capitalizeWord(tokens[tokenIndex+1])
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func travelContextCue(tokens []string) bool {
|
|
return travelCue(tokens) || containsStemInTokens(tokens, "добрат", "передвиг", "ехать", "едем", "лететь", "поездк")
|
|
}
|
|
|
|
func capitalizeWord(value string) string {
|
|
runes := []rune(value)
|
|
if len(runes) == 0 {
|
|
return value
|
|
}
|
|
return strings.ToUpper(string(runes[0])) + string(runes[1:])
|
|
}
|
|
|
|
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
|
|
}
|