Skip to content

Commit

Permalink
Merge pull request #214 from lunakv/regex_update
Browse files Browse the repository at this point in the history
Fix roll regex, update a bunch of others
  • Loading branch information
Fryyyyy authored Nov 13, 2023
2 parents f523298 + 2f637d9 commit 4bc2b75
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ config.json*
*.exe
[fF]ryatog
vendor/*
.idea/
13 changes: 0 additions & 13 deletions card.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,6 @@ func fetchScryfallCardByFuzzyName(input string, isLang bool) (Card, error) {
}
if !isLang && card.Lang != "en" {
log.Debug("Got back a foreign card when it wasn't requested, let's try again")
coercedName, err := HandleForeignCardOverlapCases(input)
if err == nil && coercedName != "" {
card.Name = coercedName
}
return fetchScryfallCardByFuzzyName(card.Name, false)
}
if IsDumbCard(card) {
Expand All @@ -454,15 +450,6 @@ func fetchScryfallCardByFuzzyName(input string, isLang bool) (Card, error) {
return card, fmt.Errorf("Card not found by Scryfall")
}

func HandleForeignCardOverlapCases(input string) (string, error) {
log.Debug("Handling foreign card overlap", "Card", input)
if uroRegex.MatchString(input) {
log.Debug("\tThey probably wanted Uro, Titan of Nature's Wrath")
return "Uro, Titan of Nature's Wrath", nil
}
return "", nil
}

func IsDumbCard(card Card) bool {
releaseTime, err := time.Parse("2006-01-02", card.ReleasedAt)
if err != nil {
Expand Down
20 changes: 0 additions & 20 deletions card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,26 +523,6 @@ func TestSearchResultHandling(t *testing.T) {
}
}

func TestCoerceRealNamesFromForeignHiccups(t *testing.T) {
tables := []struct {
input string
output string
}{
{"Uro,", "Uro, Titan of Nature's Wrath"},
{"Uro", "Uro, Titan of Nature's Wrath"},
{"uro", "Uro, Titan of Nature's Wrath"},
}
for _, table := range tables {
got, err := HandleForeignCardOverlapCases(table.input)
if err != nil {
t.Errorf("Something broke: %s", err)
}
if got != table.output {
t.Errorf("Incorrect output -- got %s -- want %s", got, table.output)
}
}
}

func TestIsDumbCard(t *testing.T) {
tables := []struct {
jsonFile string
Expand Down
21 changes: 9 additions & 12 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ var (

//Stuff pared from main.go
botCommandRegex = regexp.MustCompile(`[!&]([^=!&?[)][^!&?[)]+)\.\s|[!&]([^=!&?[)][^!&?[)]+)|\[\[(.*?)\]\]`)
singleQuotedWord = regexp.MustCompile(`^(?:\"|\')\w+(?:\"|\')$`)
nonTextRegex = regexp.MustCompile(`^[^\w]+$`)
wordEndingInBang = regexp.MustCompile(`!(?:"|') |(?:\n)+`)
wordStartingWithBang = regexp.MustCompile(`\s+!(?: *)\S+`)
singleQuotedWord = regexp.MustCompile(`^(?:"\w+"|'\w+')$`)
nonTextRegex = regexp.MustCompile(`^\W+$`)
wordEndingInBang = regexp.MustCompile(`!["'] |\n+`)
wordStartingWithBang = regexp.MustCompile(`\s+! *\S+`)

diceRegex = regexp.MustCompile(`(?:roll)?(?:\s*)(.*?)(?:d(\d+)([+-]\d+)?)`)
diceRegex = regexp.MustCompile(`^(?:roll )?\s*(.*?)d(\d+)([+-]\d+)?`)

cardMetadataRegex = regexp.MustCompile(`(?i)^(?:ruling(?:s?)|reminder|flavo(?:u?)r)(?: )`)
cardMetadataRegex = regexp.MustCompile(`(?i)^(?:rulings?|reminder|flavou?r) `)

gathererRulingRegex = regexp.MustCompile(`^(?:(?P<start_number>\d+) ?(?P<name>.+)|(?P<name2>.*?) ?(?P<end_number>\d+).*?|(?P<name3>.+))`)

seeRuleRegexp = regexp.MustCompile(`rule (\d+\.{0,1}\d*\w?)`)
seeRuleRegexp = regexp.MustCompile(`rule (\d+\.?\d*\w?)`)

noPunctuationRegex = regexp.MustCompile(`\W$`)

Expand All @@ -42,19 +42,16 @@ var (
ruleRegexp = regexp.MustCompile(ruleRegexpLiteral)
ruleExampleRegexp = regexp.MustCompile(`(\d+) ` + ruleRegexpLiteral + `|` + ruleRegexpLiteral + ` (\d+)` + `|` + ruleRegexpLiteral)

greetingRegexp = regexp.MustCompile(`(?i)^h(ello|i)( *)(\!|\.|\?)*( *)$`)
greetingRegexp = regexp.MustCompile(`(?i)^h(ello|i) *[!.?]* *$`)

//Stuff pared from card.go
reminderRegexp = regexp.MustCompile(`\((.*?)\)`)
nonAlphaRegex = regexp.MustCompile(`\W+`)
emojiRegex = regexp.MustCompile(`{\d+}|{[A-Z]}|{\d\/[A-Z]}|{[A-Z]\/[A-Z]}`)
emojiRegex = regexp.MustCompile(`{\d+}|{[A-Z]}|{\d/[A-Z]}|{[A-Z]/[A-Z]}`)

// Super rudimentary policy regex to parse e.g '4.8' into 4-8 for link generation
policyRegex = regexp.MustCompile(`[^0-9]+`)

// We PROBABLY want Uro, not Aurochs
uroRegex = regexp.MustCompile(`^[Uu]ro,?$`)

// Metrics
totalLines = expvar.NewInt("bot_totalLines")
ircLines = expvar.NewInt("bot_ircLines")
Expand Down

0 comments on commit 4bc2b75

Please sign in to comment.