Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1 | package datok |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "io" |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame^] | 6 | "strconv" |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 7 | ) |
| 8 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame^] | 9 | type TokenWriter struct { |
| 10 | SentenceEnd func(int) |
| 11 | TextEnd func(int) |
| 12 | Flush func() error |
| 13 | Token func(int, []rune) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 14 | } |
| 15 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame^] | 16 | func NewTokenWriter(w io.Writer) *TokenWriter { |
| 17 | writer := bufio.NewWriter(w) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 18 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame^] | 19 | return &TokenWriter{ |
| 20 | SentenceEnd: func(_ int) { |
| 21 | writer.WriteRune('\n') |
| 22 | }, |
| 23 | TextEnd: func(_ int) { |
| 24 | writer.WriteRune('\n') |
| 25 | writer.Flush() |
| 26 | }, |
| 27 | Token: func(offset int, buf []rune) { |
| 28 | writer.WriteString(string(buf[offset:])) |
| 29 | writer.WriteRune('\n') |
| 30 | }, |
| 31 | Flush: func() error { |
| 32 | return writer.Flush() |
| 33 | }, |
| 34 | } |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame^] | 37 | // Create a new token writer based on the options |
| 38 | func NewTokenWriterFromOptions(w io.Writer, positionFlag bool) *TokenWriter { |
| 39 | writer := bufio.NewWriter(w) |
| 40 | posC := 0 |
| 41 | pos := make([]int, 0, 200) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 42 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame^] | 43 | tw := &TokenWriter{} |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 44 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame^] | 45 | if positionFlag { |
| 46 | tw.Token = func(offset int, buf []rune) { |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 47 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame^] | 48 | // TODO: |
| 49 | // Store in []uint16 |
| 50 | // and write to string |
| 51 | posC += offset |
| 52 | pos = append(pos, posC) |
| 53 | posC += len(buf) - offset |
| 54 | pos = append(pos, posC) |
| 55 | // pos = append(pos, offset, len(buf)-offset) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 56 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame^] | 57 | writer.WriteString(string(buf[offset:])) |
| 58 | writer.WriteRune('\n') |
| 59 | } |
| 60 | } else { |
| 61 | tw.Token = func(offset int, buf []rune) { |
| 62 | writer.WriteString(string(buf[offset:])) |
| 63 | writer.WriteRune('\n') |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | tw.SentenceEnd = func(_ int) { |
| 68 | writer.WriteRune('\n') |
| 69 | } |
| 70 | |
| 71 | if positionFlag { |
| 72 | tw.TextEnd = func(offset int) { |
| 73 | writer.Flush() |
| 74 | |
| 75 | writer.WriteString(strconv.Itoa(pos[0])) |
| 76 | for _, x := range pos[1:] { |
| 77 | writer.WriteByte(' ') |
| 78 | writer.WriteString(strconv.Itoa(x)) |
| 79 | } |
| 80 | writer.WriteRune('\n') |
| 81 | |
| 82 | posC = 0 - offset |
| 83 | pos = pos[:0] |
| 84 | } |
| 85 | } else { |
| 86 | tw.TextEnd = func(_ int) { |
| 87 | writer.WriteRune('\n') |
| 88 | writer.Flush() |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | tw.Flush = func() error { |
| 94 | return writer.Flush() |
| 95 | } |
| 96 | |
| 97 | return tw |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 98 | } |