|
| 1 | +package cases |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "log/slog" |
| 9 | + "math/rand/v2" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "unicode" |
| 13 | + |
| 14 | + "github.com/ndabAP/entitydebs" |
| 15 | + "github.com/ndabAP/entitydebs/tokenize" |
| 16 | + "github.com/ndabAP/entitydebs/tokenize/nlp" |
| 17 | + "github.com/ndabAP/entityscrape/sbd" |
| 18 | + "github.com/ndabAP/entityscrape/translator" |
| 19 | + "golang.org/x/text/language" |
| 20 | +) |
| 21 | + |
| 22 | +func (study study[samples, aggregated]) Conduct(ctx context.Context) error { |
| 23 | + slog.Debug("processing subjects", "n", len(study.Subjects)) |
| 24 | + |
| 25 | + translator := translator.NewGoogle(ctx, GoogleCloudSvcAccountKey) |
| 26 | + for subject, analyses := range study.Subjects { |
| 27 | + select { |
| 28 | + case <-ctx.Done(): |
| 29 | + return ctx.Err() |
| 30 | + default: |
| 31 | + } |
| 32 | + |
| 33 | + slog.Debug("processing analyses", "subject", subject) |
| 34 | + var ( |
| 35 | + entity = analyses.Entity |
| 36 | + ext = analyses.Ext |
| 37 | + feats = analyses.Feats |
| 38 | + filenames = analyses.Filenames |
| 39 | + fuzzyMatching = analyses.FuzzyMatching |
| 40 | + lang = analyses.Language |
| 41 | + parser = analyses.Parser |
| 42 | + ) |
| 43 | + tokenizer := nlp.New(GoogleCloudSvcAccountKey, lang.String()) |
| 44 | + frames, err := study.frames( |
| 45 | + ctx, |
| 46 | + entity, |
| 47 | + filenames, |
| 48 | + parser, |
| 49 | + fuzzyMatching, |
| 50 | + tokenizer, |
| 51 | + feats, |
| 52 | + lang, |
| 53 | + ) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + slog.Debug("analysis done") |
| 58 | + |
| 59 | + slog.Debug("collecting samples") |
| 60 | + samples := study.collect(frames) |
| 61 | + slog.Debug("sample collection done") |
| 62 | + |
| 63 | + slog.Debug("aggregating samples") |
| 64 | + aggregated := study.aggregate(samples) |
| 65 | + slog.Debug("aggregation done") |
| 66 | + |
| 67 | + slog.Debug("reporting aggregation") |
| 68 | + translator := func(w []string) ([]string, error) { |
| 69 | + switch lang { |
| 70 | + case language.English: |
| 71 | + slog.Debug("skipping translation for English") |
| 72 | + return w, nil |
| 73 | + default: |
| 74 | + } |
| 75 | + |
| 76 | + return translator.Translate(w, lang, language.English) |
| 77 | + } |
| 78 | + if err := func() error { |
| 79 | + pref := strings.Map(func(r rune) rune { |
| 80 | + switch { |
| 81 | + case r >= 'a' && r <= 'z': |
| 82 | + return r |
| 83 | + case r >= 'A' && r <= 'Z': |
| 84 | + return unicode.ToLower(r) |
| 85 | + case r == ' ', r == '-': |
| 86 | + return '_' |
| 87 | + default: |
| 88 | + return -1 |
| 89 | + } |
| 90 | + }, subject) |
| 91 | + writer, err := study.store.NewWriter(pref, ext) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + //nolint:errcheck |
| 96 | + defer writer.Close() |
| 97 | + |
| 98 | + if err := study.report(aggregated, translator, writer); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | + }(); err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + slog.Debug("reporting done", "subject", subject) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func (study study[samples, aggregated]) frames( |
| 113 | + ctx context.Context, |
| 114 | + entity, |
| 115 | + filenames []string, |
| 116 | + parser Parser, |
| 117 | + fuzzyMatching bool, |
| 118 | + tokenizer tokenize.Tokenizer, |
| 119 | + feats tokenize.Features, |
| 120 | + lang language.Tag, |
| 121 | +) ( |
| 122 | + entitydebs.Frames, |
| 123 | + error, |
| 124 | +) { |
| 125 | + slog.Debug("parsing files", "n", len(filenames)) |
| 126 | + if fuzzyMatching { |
| 127 | + slog.Debug("fuzzy matching enabled") |
| 128 | + } |
| 129 | + |
| 130 | + var ( |
| 131 | + texts = make([]string, 0, len(filenames)) |
| 132 | + |
| 133 | + textChan = make(chan []byte, 50) |
| 134 | + errChan = make(chan error, 1) |
| 135 | + ) |
| 136 | + |
| 137 | + // Consumer |
| 138 | + go func() { |
| 139 | + defer close(errChan) |
| 140 | + |
| 141 | + for text := range textChan { |
| 142 | + n := rand.Uint64N(100) |
| 143 | + if n >= SampleRate { |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + t := string(text) |
| 148 | + |
| 149 | + if fuzzyMatching { |
| 150 | + var ( |
| 151 | + buf = new(bytes.Buffer) |
| 152 | + |
| 153 | + c = make(chan string, 50) |
| 154 | + done = make(chan struct{}, 1) |
| 155 | + ) |
| 156 | + |
| 157 | + // Consumer |
| 158 | + study.fuzzyMatch(c, entity, buf, done) |
| 159 | + // Producer |
| 160 | + sbd.Tokenize(lang, t, c) |
| 161 | + close(c) |
| 162 | + |
| 163 | + <-done |
| 164 | + if buf.Len() > 0 { |
| 165 | + texts = append(texts, strings.TrimSpace(buf.String())) |
| 166 | + } |
| 167 | + |
| 168 | + continue |
| 169 | + } |
| 170 | + |
| 171 | + // No fuzzy matching. |
| 172 | + texts = append(texts, strings.TrimSpace(t)) |
| 173 | + } |
| 174 | + }() |
| 175 | + // Producer |
| 176 | + go func() { |
| 177 | + defer close(textChan) |
| 178 | + |
| 179 | + for _, filename := range filenames { |
| 180 | + file, err := os.Open(filename) |
| 181 | + if err != nil { |
| 182 | + errChan <- err |
| 183 | + return |
| 184 | + } |
| 185 | + for err := range parser(file, textChan) { |
| 186 | + if errors.Is(err, bufio.ErrTooLong) { |
| 187 | + continue |
| 188 | + } |
| 189 | + errChan <- err |
| 190 | + } |
| 191 | + //nolint:errcheck |
| 192 | + _ = file.Close() |
| 193 | + } |
| 194 | + }() |
| 195 | + |
| 196 | + select { |
| 197 | + case <-ctx.Done(): |
| 198 | + return entitydebs.Frames{}, ctx.Err() |
| 199 | + |
| 200 | + case err := <-errChan: |
| 201 | + if err != nil { |
| 202 | + return entitydebs.Frames{}, err |
| 203 | + } |
| 204 | + } |
| 205 | + slog.Debug("texts sampled and parsed", "n", len(texts)) |
| 206 | + |
| 207 | + slog.Debug("generating frames") |
| 208 | + src := entitydebs.NewSource(entity, texts) |
| 209 | + return src.Frames(ctx, tokenizer, feats, entitydebs.NFKC) |
| 210 | +} |
0 commit comments