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 |
Akron | fceddb6 | 2021-10-27 19:27:54 +0200 | [diff] [blame^] | 38 | func NewTokenWriterFromOptions(w io.Writer, positionFlag bool, tokenFlag bool, sentenceFlag bool, sentencePositionFlag bool, newlineAfterEot bool) *TokenWriter { |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 39 | writer := bufio.NewWriter(w) |
| 40 | posC := 0 |
Akron | fceddb6 | 2021-10-27 19:27:54 +0200 | [diff] [blame^] | 41 | pos := make([]int, 0, 1024) |
| 42 | sentB := true |
| 43 | sent := make([]int, 0, 1024) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 44 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 45 | tw := &TokenWriter{} |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 46 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 47 | if positionFlag { |
| 48 | tw.Token = func(offset int, buf []rune) { |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 49 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 50 | // TODO: |
| 51 | // Store in []uint16 |
| 52 | // and write to string |
Akron | 8cc2dd9 | 2021-10-25 19:49:41 +0200 | [diff] [blame] | 53 | |
Akron | e9431ec | 2021-10-25 21:35:33 +0200 | [diff] [blame] | 54 | // Accept newline after EOT |
| 55 | if newlineAfterEot && posC == 0 && buf[0] == '\n' && writer.Buffered() != 0 { |
| 56 | posC-- |
| 57 | } |
| 58 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 59 | posC += offset |
| 60 | pos = append(pos, posC) |
Akron | fceddb6 | 2021-10-27 19:27:54 +0200 | [diff] [blame^] | 61 | |
| 62 | // Token is the start of a sentence |
| 63 | if sentB { |
| 64 | sentB = false |
| 65 | sent = append(sent, posC) |
| 66 | } |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 67 | posC += len(buf) - offset |
| 68 | pos = append(pos, posC) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 69 | |
Akron | a9e0c42 | 2021-10-27 19:01:17 +0200 | [diff] [blame] | 70 | if tokenFlag { |
| 71 | writer.WriteString(string(buf[offset:])) |
| 72 | writer.WriteRune('\n') |
| 73 | } |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 74 | } |
Akron | fceddb6 | 2021-10-27 19:27:54 +0200 | [diff] [blame^] | 75 | |
| 76 | // Only print one token per line |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 77 | } else { |
| 78 | tw.Token = func(offset int, buf []rune) { |
| 79 | writer.WriteString(string(buf[offset:])) |
| 80 | writer.WriteRune('\n') |
| 81 | } |
| 82 | } |
| 83 | |
Akron | fceddb6 | 2021-10-27 19:27:54 +0200 | [diff] [blame^] | 84 | // Print sentence boundaries |
| 85 | if sentenceFlag || sentencePositionFlag { |
| 86 | tw.SentenceEnd = func(offset int) { |
| 87 | |
| 88 | // Add end position of last token to sentence boundary |
| 89 | sent = append(sent, pos[len(pos)-1]) |
| 90 | sentB = true |
| 91 | |
| 92 | if sentenceFlag { |
| 93 | writer.WriteRune('\n') |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Print sentence boundaries as newlines |
| 98 | } else if sentenceFlag { |
| 99 | tw.SentenceEnd = func(_ int) { |
| 100 | writer.WriteRune('\n') |
| 101 | } |
| 102 | |
| 103 | // Ignore sentence boundaries |
| 104 | } else { |
| 105 | tw.SentenceEnd = func(_ int) {} |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Akron | fceddb6 | 2021-10-27 19:27:54 +0200 | [diff] [blame^] | 108 | if positionFlag || sentencePositionFlag { |
Akron | e9431ec | 2021-10-25 21:35:33 +0200 | [diff] [blame] | 109 | tw.TextEnd = func(_ int) { |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 110 | writer.Flush() |
| 111 | |
Akron | fceddb6 | 2021-10-27 19:27:54 +0200 | [diff] [blame^] | 112 | if positionFlag { |
| 113 | writer.WriteString(strconv.Itoa(pos[0])) |
| 114 | for _, x := range pos[1:] { |
| 115 | writer.WriteByte(' ') |
| 116 | writer.WriteString(strconv.Itoa(x)) |
| 117 | } |
| 118 | writer.WriteRune('\n') |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 119 | } |
Akron | fceddb6 | 2021-10-27 19:27:54 +0200 | [diff] [blame^] | 120 | |
| 121 | if sentencePositionFlag { |
| 122 | writer.WriteString(strconv.Itoa(sent[0])) |
| 123 | for _, x := range sent[1:] { |
| 124 | writer.WriteByte(' ') |
| 125 | writer.WriteString(strconv.Itoa(x)) |
| 126 | } |
| 127 | writer.WriteRune('\n') |
| 128 | sent = sent[:0] |
| 129 | sentB = true |
| 130 | } |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 131 | |
Akron | 8cc2dd9 | 2021-10-25 19:49:41 +0200 | [diff] [blame] | 132 | posC = 0 |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 133 | pos = pos[:0] |
| 134 | } |
| 135 | } else { |
| 136 | tw.TextEnd = func(_ int) { |
| 137 | writer.WriteRune('\n') |
| 138 | writer.Flush() |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | tw.Flush = func() error { |
| 144 | return writer.Flush() |
| 145 | } |
| 146 | |
| 147 | return tw |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 148 | } |