repochecker/grammalecte: Refactor grammar passage extraction

This commit is contained in:
nemunaire 2022-11-24 19:38:15 +01:00
parent ccc2c5d1d7
commit 2381dfe4f5
2 changed files with 25 additions and 10 deletions

View File

@ -183,19 +183,11 @@ func grammalecte(name string, text string, paragraph int, exceptions *sync.Check
}
for _, gerror := range data.GrammarErrors {
if data.Text[0] == '>' && gerror.RuleId == "poncfin_règle1" {
continue
}
if gerror.RuleId == "mc_mot_composé" && ((gerror.End+1 < len(data.Text) && gerror.Start+1 < len(data.Text) && exceptions.HasException(fmt.Sprintf(":spelling:%s", data.Text[gerror.Start+1:gerror.End+1]))) || exceptions.HasException(fmt.Sprintf(":spelling:%s", data.Text[gerror.Start:gerror.End]))) {
continue
}
if (paragraph == 0 && exceptions.HasException(fmt.Sprintf(":%d:%s", data.Paragraph, gerror.RuleId))) || (paragraph < 0 && exceptions.HasException(fmt.Sprintf(":%s", gerror.RuleId))) || (paragraph > 0 && exceptions.HasException(fmt.Sprintf(":%d:%s", paragraph, gerror.RuleId))) {
continue
}
errs = append(errs, lib.GrammarError{
err := lib.GrammarError{
Prefix: name,
Source: data.Text,
NSource: data.Paragraph,
@ -206,7 +198,13 @@ func grammalecte(name string, text string, paragraph int, exceptions *sync.Check
Message: gerror.Message,
Suggestions: gerror.Suggestions,
URL: gerror.URL,
})
}
if err.RuleId == "mc_mot_composé" && exceptions.HasException(fmt.Sprintf(":spelling:%s", err.GetPassage())) {
continue
}
errs = append(errs, err)
}
}

View File

@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"strings"
"unicode/utf8"
)
const LOG_PREFIX_LEN = 20
@ -72,6 +73,22 @@ func (e GrammarError) Error() string {
)
}
func (e GrammarError) GetPassage() string {
nb := 0
var ret []byte
for _, r := range e.Source {
if nb >= e.End {
break
}
if nb >= e.Start {
ret = utf8.AppendRune(ret, r)
}
nb += 1
}
return string(ret)
}
func underline(prefix, start, end int) string {
var b bytes.Buffer