| 1 |
// Copyright (c) 2014 Couchbase, Inc. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 3 |
// except in compliance with the License. You may obtain a copy of the License at |
| 4 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 5 |
// Unless required by applicable law or agreed to in writing, software distributed under the |
| 6 |
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 7 |
// either express or implied. See the License for the specific language governing permissions |
| 8 |
// and limitations under the License. |
| 9 |
|
| 10 |
// +build ignore |
| 11 |
|
| 12 |
package main |
| 13 |
|
| 14 |
import ( |
| 15 |
"bufio" |
| 16 |
"bytes" |
| 17 |
"flag" |
| 18 |
"fmt" |
| 19 |
"io" |
| 20 |
"log" |
| 21 |
"net/http" |
| 22 |
"os" |
| 23 |
"os/exec" |
| 24 |
"strconv" |
| 25 |
"strings" |
| 26 |
) |
| 27 |
|
| 28 |
var url = flag.String("url", |
| 29 |
"http://www.unicode.org/Public/13.0.0/ucd/auxiliary/", |
| 30 |
"URL of Unicode database directory") |
| 31 |
var verbose = flag.Bool("verbose", |
| 32 |
false, |
| 33 |
"write data to stdout as it is parsed") |
| 34 |
var localFiles = flag.Bool("local", |
| 35 |
false, |
| 36 |
"data files have been copied to the current directory; for debugging only") |
| 37 |
|
| 38 |
var outputFile = flag.String("output", |
| 39 |
"", |
| 40 |
"output file for generated tables; default stdout") |
| 41 |
|
| 42 |
var output *bufio.Writer |
| 43 |
|
| 44 |
func main() { |
| 45 |
flag.Parse() |
| 46 |
setupOutput() |
| 47 |
|
| 48 |
graphemeTests := make([]test, 0) |
| 49 |
graphemeTests = loadUnicodeData("GraphemeBreakTest.txt", graphemeTests) |
| 50 |
wordTests := make([]test, 0) |
| 51 |
wordTests = loadUnicodeData("WordBreakTest.txt", wordTests) |
| 52 |
sentenceTests := make([]test, 0) |
| 53 |
sentenceTests = loadUnicodeData("SentenceBreakTest.txt", sentenceTests) |
| 54 |
|
| 55 |
fmt.Fprintf(output, fileHeader, *url) |
| 56 |
generateTestTables("Grapheme", graphemeTests) |
| 57 |
generateTestTables("Word", wordTests) |
| 58 |
generateTestTables("Sentence", sentenceTests) |
| 59 |
|
| 60 |
flushOutput() |
| 61 |
} |
| 62 |
|
| 63 |
// WordBreakProperty.txt has the form: |
| 64 |
// 05F0..05F2 ; Hebrew_Letter # Lo [3] HEBREW LIGATURE YIDDISH DOUBLE VAV..HEBREW LIGATURE YIDDISH DOUBLE YOD |
| 65 |
// FB1D ; Hebrew_Letter # Lo HEBREW LETTER YOD WITH HIRIQ |
| 66 |
func openReader(file string) (input io.ReadCloser) { |
| 67 |
if *localFiles { |
| 68 |
f, err := os.Open(file) |
| 69 |
if err != nil { |
| 70 |
log.Fatal(err) |
| 71 |
} |
| 72 |
input = f |
| 73 |
} else { |
| 74 |
path := *url + file |
| 75 |
resp, err := http.Get(path) |
| 76 |
if err != nil { |
| 77 |
log.Fatal(err) |
| 78 |
} |
| 79 |
if resp.StatusCode != 200 { |
| 80 |
log.Fatal("bad GET status for "+file, resp.Status) |
| 81 |
} |
| 82 |
input = resp.Body |
| 83 |
} |
| 84 |
return |
| 85 |
} |
| 86 |
|
| 87 |
func loadUnicodeData(filename string, tests []test) []test { |
| 88 |
f := openReader(filename) |
| 89 |
defer f.Close() |
| 90 |
bufioReader := bufio.NewReader(f) |
| 91 |
line, err := bufioReader.ReadString('\n') |
| 92 |
for err == nil { |
| 93 |
tests = parseLine(line, tests) |
| 94 |
line, err = bufioReader.ReadString('\n') |
| 95 |
} |
| 96 |
// if the err was EOF still need to process last value |
| 97 |
if err == io.EOF { |
| 98 |
tests = parseLine(line, tests) |
| 99 |
} |
| 100 |
return tests |
| 101 |
} |
| 102 |
|
| 103 |
const comment = "#" |
| 104 |
const brk = "÷" |
| 105 |
const nbrk = "×" |
| 106 |
|
| 107 |
type test [][]byte |
| 108 |
|
| 109 |
func parseLine(line string, tests []test) []test { |
| 110 |
if strings.HasPrefix(line, comment) { |
| 111 |
return tests |
| 112 |
} |
| 113 |
line = strings.TrimSpace(line) |
| 114 |
if len(line) == 0 { |
| 115 |
return tests |
| 116 |
} |
| 117 |
commentStart := strings.Index(line, comment) |
| 118 |
if commentStart > 0 { |
| 119 |
line = line[0:commentStart] |
| 120 |
} |
| 121 |
pieces := strings.Split(line, brk) |
| 122 |
t := make(test, 0) |
| 123 |
for _, piece := range pieces { |
| 124 |
piece = strings.TrimSpace(piece) |
| 125 |
if len(piece) > 0 { |
| 126 |
codePoints := strings.Split(piece, nbrk) |
| 127 |
word := "" |
| 128 |
for _, codePoint := range codePoints { |
| 129 |
codePoint = strings.TrimSpace(codePoint) |
| 130 |
r, err := strconv.ParseInt(codePoint, 16, 64) |
| 131 |
if err != nil { |
| 132 |
log.Printf("err: %v for '%s'", err, string(r)) |
| 133 |
return tests |
| 134 |
} |
| 135 |
|
| 136 |
word += string(r) |
| 137 |
} |
| 138 |
t = append(t, []byte(word)) |
| 139 |
} |
| 140 |
} |
| 141 |
tests = append(tests, t) |
| 142 |
return tests |
| 143 |
} |
| 144 |
|
| 145 |
func generateTestTables(prefix string, tests []test) { |
| 146 |
fmt.Fprintf(output, testHeader, prefix) |
| 147 |
for _, t := range tests { |
| 148 |
fmt.Fprintf(output, "\t\t{\n") |
| 149 |
fmt.Fprintf(output, "\t\t\tinput: %#v,\n", bytes.Join(t, []byte{})) |
| 150 |
fmt.Fprintf(output, "\t\t\toutput: %s,\n", generateTest(t)) |
| 151 |
fmt.Fprintf(output, "\t\t},\n") |
| 152 |
} |
| 153 |
fmt.Fprintf(output, "}\n") |
| 154 |
} |
| 155 |
|
| 156 |
func generateTest(t test) string { |
| 157 |
rv := "[][]byte{" |
| 158 |
for _, te := range t { |
| 159 |
rv += fmt.Sprintf("%#v,", te) |
| 160 |
} |
| 161 |
rv += "}" |
| 162 |
return rv |
| 163 |
} |
| 164 |
|
| 165 |
const fileHeader = `// Generated by running |
| 166 |
// maketesttables --url=%s |
| 167 |
// DO NOT EDIT |
| 168 |
|
| 169 |
package textseg |
| 170 |
` |
| 171 |
|
| 172 |
const testHeader = `var unicode%sTests = []struct { |
| 173 |
input []byte |
| 174 |
output [][]byte |
| 175 |
}{ |
| 176 |
` |
| 177 |
|
| 178 |
func setupOutput() { |
| 179 |
output = bufio.NewWriter(startGofmt()) |
| 180 |
} |
| 181 |
|
| 182 |
// startGofmt connects output to a gofmt process if -output is set. |
| 183 |
func startGofmt() io.Writer { |
| 184 |
if *outputFile == "" { |
| 185 |
return os.Stdout |
| 186 |
} |
| 187 |
stdout, err := os.Create(*outputFile) |
| 188 |
if err != nil { |
| 189 |
log.Fatal(err) |
| 190 |
} |
| 191 |
// Pipe output to gofmt. |
| 192 |
gofmt := exec.Command("gofmt") |
| 193 |
fd, err := gofmt.StdinPipe() |
| 194 |
if err != nil { |
| 195 |
log.Fatal(err) |
| 196 |
} |
| 197 |
gofmt.Stdout = stdout |
| 198 |
gofmt.Stderr = os.Stderr |
| 199 |
err = gofmt.Start() |
| 200 |
if err != nil { |
| 201 |
log.Fatal(err) |
| 202 |
} |
| 203 |
return fd |
| 204 |
} |
| 205 |
|
| 206 |
func flushOutput() { |
| 207 |
err := output.Flush() |
| 208 |
if err != nil { |
| 209 |
log.Fatal(err) |
| 210 |
} |
| 211 |
} |