S1 filet (backend + mobile) : corbeille 30j (deleted_at réutilisé, trash/restore récursifs, purge auto serveur), versions N=3 à chaque overwrite, ops outbox trash/restore/purge (delete legacy mappé), Room v19 (trashed_at), Deleters -> trash, écran Corbeille, walk guards. S2 extract (backend) : package extract (règles FR stdlib, corpus testé), resource_facts 000019 (origin auto/user), hook worker OCR + backfill boot, GET /files/:id/facts (viewer+). Contrat docs/api-v1.md étendu, V3.md (vision + plan).
109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
package service
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/kazier/backend/extract"
|
|
"github.com/kazier/backend/repository"
|
|
)
|
|
|
|
// Extract derives generic facts from OCR text (V3 S2) — dates, amounts,
|
|
// issuers, references, suggested doc type. Rules-based (package extract),
|
|
// no ML, no network : it runs inside the OCR worker right after Tesseract
|
|
// and stores origin='auto' rows (user corrections, origin='user', survive
|
|
// every re-run — see UpsertAutoFacts).
|
|
type Extract struct {
|
|
Repository *repository.Repository
|
|
}
|
|
|
|
func NewExtract(repo *repository.Repository) *Extract {
|
|
return &Extract{Repository: repo}
|
|
}
|
|
|
|
// FactDTO serializes one row of GET /files/:id/facts.
|
|
type FactDTO struct {
|
|
FactType string `json:"factType"`
|
|
ValueText string `json:"valueText,omitempty"`
|
|
ValueNum *float64 `json:"valueNum,omitempty"`
|
|
ValueDate string `json:"valueDate,omitempty"`
|
|
Confidence float64 `json:"confidence"`
|
|
Origin string `json:"origin"`
|
|
}
|
|
|
|
// ProcessText extracts facts from text and replaces the resource's auto
|
|
// facts. Empty text clears stale auto facts. Never touches user corrections.
|
|
func (e *Extract) ProcessText(resourceID, text string) error {
|
|
raw := extract.FromText(text)
|
|
params := make([]repository.FactParam, 0, len(raw))
|
|
for _, f := range raw {
|
|
var num *float64
|
|
if f.HasNum {
|
|
n := f.ValueNum
|
|
num = &n
|
|
}
|
|
var dat *string
|
|
if f.ValueDate != "" {
|
|
d := f.ValueDate
|
|
dat = &d
|
|
}
|
|
text := f.ValueText
|
|
// Amounts carry a display string ("1234.56 €") — keep the numeric
|
|
// column as the machine value, the text as the human one.
|
|
params = append(params, repository.FactParam{
|
|
Type: string(f.Type),
|
|
ValueText: text,
|
|
ValueNum: num,
|
|
ValueDate: dat,
|
|
Confidence: f.Confidence,
|
|
})
|
|
}
|
|
return e.Repository.Resources.UpsertAutoFacts(resourceID, params)
|
|
}
|
|
|
|
// Backfill processes files with OCR text but no auto facts yet (one boot
|
|
// pass, bounded batches). Returns the number of resources processed.
|
|
func (e *Extract) Backfill(batch int) (int, error) {
|
|
total := 0
|
|
for {
|
|
pending, err := e.Repository.Resources.ListNeedingExtract(batch)
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
if len(pending) == 0 {
|
|
return total, nil
|
|
}
|
|
for _, p := range pending {
|
|
if err := e.ProcessText(p.ResourceID, p.Text); err != nil {
|
|
log.Printf("extract: backfill %s: %v", p.ResourceID, err)
|
|
continue
|
|
}
|
|
total++
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetFacts returns the facts of a visible resource (viewer+, own or shared —
|
|
// same scope as GetFileOcr, facts derive from the OCR text). Trashed/absent/
|
|
// foreign → repository.ErrNotFound.
|
|
func (s *Resources) GetFacts(userID, resourceID string) ([]FactDTO, error) {
|
|
if _, err := s.Repo.GetFileVisible(userID, resourceID); err != nil {
|
|
return nil, err
|
|
}
|
|
rows, err := s.Repository.Resources.ListFacts(resourceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]FactDTO, 0, len(rows))
|
|
for _, row := range rows {
|
|
out = append(out, FactDTO{
|
|
FactType: row.Type,
|
|
ValueText: row.ValueText,
|
|
ValueNum: row.ValueNum,
|
|
ValueDate: derefString(row.ValueDate),
|
|
Confidence: row.Confidence,
|
|
Origin: row.Origin,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|