Files
sanderling/cmd/internal-tools/confusion-matrix/table.go
T
pj 56a002d87a feat(confusion-matrix): score the checker against a blind reviewer
Cross-tabulates the properties that fired against the human verdict, one
cell per implementation, over a sweep whose implementations all passed
their own generated tests. An implementation that failed to build, has no
usable run, or carries no filed verdict is listed as missing data rather
than counted as a clean cell.

Landing the package in one commit because the intermediate splits would
not link.
2026-08-17 23:27:47 +05:30

80 lines
1.7 KiB
Go

package main
import (
"regexp"
"strings"
)
// tableRow is one pipe-delimited markdown row with its 1-based line number, so
// a malformed cell can name the line the author has to go and fix.
type tableRow struct {
Line int
Cells []string
}
var separatorCell = regexp.MustCompile(`^:?-{2,}:?$`)
func parseTableRows(body string) []tableRow {
var rows []tableRow
for index, raw := range strings.Split(body, "\n") {
line := strings.TrimSpace(raw)
if !strings.HasPrefix(line, "|") {
continue
}
cells := splitCells(line)
if len(cells) == 0 || isSeparatorRow(cells) {
continue
}
rows = append(rows, tableRow{Line: index + 1, Cells: cells})
}
return rows
}
func splitCells(line string) []string {
trimmed := strings.Trim(line, "|")
parts := strings.Split(trimmed, "|")
cells := make([]string, 0, len(parts))
for _, part := range parts {
cells = append(cells, normalizeCell(part))
}
return cells
}
func isSeparatorRow(cells []string) bool {
for _, cell := range cells {
if !separatorCell.MatchString(cell) {
return false
}
}
return true
}
func normalizeCell(value string) string {
cleaned := strings.ReplaceAll(value, "**", "")
cleaned = strings.ReplaceAll(cleaned, "`", "")
return strings.Join(strings.Fields(cleaned), " ")
}
func (row tableRow) cell(index int) string {
if index >= len(row.Cells) {
return ""
}
return row.Cells[index]
}
var listSeparators = regexp.MustCompile(`[,\s]+`)
func splitList(value string) []string {
trimmed := strings.TrimSpace(value)
if trimmed == "" {
return nil
}
var items []string
for _, item := range listSeparators.Split(trimmed, -1) {
if item != "" {
items = append(items, item)
}
}
return items
}