This commit is contained in:
Владимир Фёдоров 2025-12-07 02:06:32 +07:00
parent 09b04de9c3
commit ee76743097
4 changed files with 85 additions and 77 deletions

View File

@ -0,0 +1,39 @@
package story_service
import "strings"
var (
replaceMap = map[string]string{
"a": "а",
"e": "е",
"o": "о",
"c": "с",
"p": "р",
"x": "х",
"y": "у",
"k": "к",
"m": "м",
"t": "т",
"h": "н",
"b": "в",
"u": "и",
}
)
func clearMatch(s string) string {
s = strings.TrimPrefix(s, "(")
s = strings.TrimPrefix(s, "[")
s = strings.TrimSuffix(s, ")")
s = strings.TrimSuffix(s, "]")
return clearCode(s)
}
func clearCode(code string) string {
code = strings.ToLower(code)
code = strings.TrimSpace(code)
code = strings.ReplaceAll(code, "-", "")
for latin, cyrillic := range replaceMap {
code = strings.ReplaceAll(code, latin, cyrillic)
}
return code
}

View File

@ -0,0 +1,16 @@
package story_service
type Story struct {
Places []*Place `json:"places"`
}
type Place struct {
Code string `json:"code"`
Name string `json:"name"`
Text string `json:"text"`
Applications []*Application `json:"applications"`
}
type Application struct {
Name string `json:"name"`
}

View File

@ -0,0 +1,23 @@
package story_service
type Graph struct {
Nodes []*GraphNode
Edges []*GraphEdge
}
type GraphNode struct {
ID int32
Label string
Name string
Text string
Applications []*GraphApplication
}
type GraphEdge struct {
From int32
To int32
}
type GraphApplication struct {
Name string
}

View File

@ -10,61 +10,6 @@ import (
"strings"
)
var (
replaceMap = map[string]string{
"a": "а",
"e": "е",
"o": "о",
"c": "с",
"p": "р",
"x": "х",
"y": "у",
"k": "к",
"m": "м",
"t": "т",
"h": "н",
"b": "в",
"u": "и",
}
)
type Story struct {
Places []*Place `json:"places"`
}
type Place struct {
Code string `json:"code"`
Name string `json:"name"`
Text string `json:"text"`
Applications []*Application `json:"applications"`
}
type Application struct {
Name string `json:"name"`
}
type Graph struct {
Nodes []*GraphNode
Edges []*GraphEdge
}
type GraphNode struct {
ID int32
Label string
Name string
Text string
Applications []*GraphApplication
}
type GraphEdge struct {
From int32
To int32
}
type GraphApplication struct {
Name string
}
type StoryService struct {
story *Story
}
@ -141,10 +86,13 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
matches := re.FindAllString(place.Text, -1)
for _, match := range matches {
edges = append(edges, &GraphEdge{
From: m[clearCode(place.Code)],
To: m[clearMatch(match)],
})
edges = append(
edges,
&GraphEdge{
From: m[clearCode(place.Code)],
To: m[clearMatch(match)],
},
)
}
}
@ -153,21 +101,3 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
Edges: edges,
}
}
func clearMatch(s string) string {
s = strings.TrimPrefix(s, "(")
s = strings.TrimPrefix(s, "[")
s = strings.TrimSuffix(s, ")")
s = strings.TrimSuffix(s, "]")
return clearCode(s)
}
func clearCode(code string) string {
code = strings.ToLower(code)
code = strings.TrimSpace(code)
code = strings.ReplaceAll(code, "-", "")
for latin, cyrillic := range replaceMap {
code = strings.ReplaceAll(code, latin, cyrillic)
}
return code
}