146 lines
3.5 KiB
Go
Raw Normal View History

2018-10-31 16:05:18 +00:00
package main
import (
"fmt"
2018-10-31 16:05:18 +00:00
"strings"
"github.com/animenotifier/arn"
"github.com/fatih/color"
2018-10-31 16:05:18 +00:00
)
func parseCharacterDescription(input string) (output string, attributes []*arn.CharacterAttribute) {
// Parse attributes like these:
// - Position: Club Manager
// - Height: 162 cm (5' 4")
// - Weight: 48 kg (106 lb)
// - Birthday: November 24
// - Hair color: Brown
// - Eyes: Blue (anime), Green (manga)
paragraphs := strings.Split(input, "\n\n")
finalParagraphs := make([]string, 0, len(paragraphs))
for _, paragraph := range paragraphs {
// Is paragraph full of attributes?
if strings.Contains(paragraph, "\n") {
lines := strings.Split(paragraph, "\n")
var lastAttribute *arn.CharacterAttribute
for _, line := range lines {
2018-10-31 23:31:11 +00:00
if !strings.Contains(line, ":") {
// Remove list indicators
line = strings.TrimPrefix(line, "- ")
line = strings.TrimPrefix(line, "* ")
// Add to previous attribute
if lastAttribute != nil {
2018-10-31 23:31:11 +00:00
if lastAttribute.Value != "" {
lastAttribute.Value += ", "
}
lastAttribute.Value += line
}
continue
}
name, value := parseAttribute(line)
if name != "" && value != "" {
lastAttribute = &arn.CharacterAttribute{
Name: name,
Value: value,
}
attributes = append(attributes, lastAttribute)
}
}
2018-10-31 16:05:18 +00:00
continue
}
// Remove all kinds of starting and ending parantheses.
if strings.HasPrefix(paragraph, "(") {
paragraph = strings.TrimPrefix(paragraph, "(")
paragraph = strings.TrimSuffix(paragraph, ")")
}
// Replace source paragraph with an attribute
if strings.HasPrefix(paragraph, "Source:") || strings.HasPrefix(paragraph, "source:") {
source := paragraph[len("source:"):]
source = strings.TrimSpace(source)
attributes = append(attributes, &arn.CharacterAttribute{
Name: "Source",
Value: source,
})
continue
}
2018-11-01 00:11:11 +00:00
paragraph = strings.TrimSpace(paragraph)
// Skip paragraph if it's too short.
2018-11-01 00:43:12 +00:00
if len(paragraph) < 30 && !strings.HasSuffix(paragraph, ".") || strings.HasSuffix(paragraph, "...") {
continue
}
// Is it an attribute?
name, value := parseAttribute(paragraph)
2018-11-01 00:43:12 +00:00
if name != "" && value != "" {
attributes = append(attributes, &arn.CharacterAttribute{
Name: name,
Value: value,
})
continue
2018-11-01 00:11:11 +00:00
}
2018-10-31 16:05:18 +00:00
finalParagraphs = append(finalParagraphs, paragraph)
}
2018-10-31 16:05:18 +00:00
output = strings.Join(finalParagraphs, "\n\n")
output = strings.TrimSpace(output)
2018-10-31 16:05:18 +00:00
return output, attributes
}
2018-10-31 16:05:18 +00:00
func parseAttribute(line string) (string, string) {
if !strings.Contains(line, ":") {
return "", ""
}
2018-10-31 16:05:18 +00:00
parts := strings.Split(line, ":")
name := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
2018-10-31 16:05:18 +00:00
// Remove list indicators
name = strings.TrimPrefix(name, "- ")
name = strings.TrimPrefix(name, "* ")
2018-10-31 16:05:18 +00:00
if strings.HasPrefix(name, "~") && strings.HasSuffix(value, "~") {
name = strings.TrimPrefix(name, "~")
value = strings.TrimSuffix(value, "~")
}
2018-10-31 16:05:18 +00:00
if strings.HasPrefix(name, "[") && strings.HasSuffix(value, "]") {
name = strings.TrimPrefix(name, "[")
value = strings.TrimSuffix(value, "]")
}
2018-10-31 16:05:18 +00:00
if strings.HasPrefix(name, "(") && strings.HasSuffix(value, ")") {
name = strings.TrimPrefix(name, "(")
value = strings.TrimSuffix(value, ")")
}
2018-10-31 16:05:18 +00:00
if name == "source" || name == "sources" {
name = "Source"
2018-10-31 16:05:18 +00:00
}
if len(name) > 25 || len(value) > 50 || strings.HasSuffix(value, ".") {
return "", ""
}
2018-10-31 16:05:18 +00:00
fmt.Println(color.GreenString(name), color.YellowString(value))
return name, value
2018-10-31 16:05:18 +00:00
}