2016-08-07 01:17:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Alex Yatskov <alex@foosoft.net>
|
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-08-23 03:51:30 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2016-08-07 01:17:02 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-08-23 03:51:30 +00:00
|
|
|
const (
|
2016-08-24 16:02:26 +00:00
|
|
|
BANK_STRIDE = 50000
|
|
|
|
DB_VERSION = 0
|
2016-08-23 03:51:30 +00:00
|
|
|
)
|
2016-08-22 05:06:35 +00:00
|
|
|
|
2016-08-24 16:02:26 +00:00
|
|
|
type termIndex struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
Banks int `json:"banks"`
|
2016-08-23 03:51:30 +00:00
|
|
|
Entities [][]string `json:"ents"`
|
|
|
|
defs [][]string
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 02:51:43 +00:00
|
|
|
type termSource struct {
|
2016-08-07 01:17:02 +00:00
|
|
|
Expression string
|
|
|
|
Reading string
|
|
|
|
Tags []string
|
|
|
|
Glossary []string
|
|
|
|
}
|
|
|
|
|
2016-08-22 02:51:43 +00:00
|
|
|
func (s *termSource) addTags(tags ...string) {
|
2016-08-07 01:17:02 +00:00
|
|
|
for _, tag := range tags {
|
2016-08-08 02:55:46 +00:00
|
|
|
if !hasString(tag, s.Tags) {
|
|
|
|
s.Tags = append(s.Tags, tag)
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 02:51:43 +00:00
|
|
|
func (s *termSource) addTagsPri(tags ...string) {
|
2016-08-07 20:24:56 +00:00
|
|
|
for _, tag := range tags {
|
|
|
|
switch tag {
|
|
|
|
case "news1", "ichi1", "spec1", "gai1":
|
2016-08-08 02:55:46 +00:00
|
|
|
s.addTags("P")
|
2016-08-07 20:24:56 +00:00
|
|
|
fallthrough
|
|
|
|
case "news2", "ichi2", "spec2", "gai2":
|
2016-08-08 02:55:46 +00:00
|
|
|
s.addTags(tag[:len(tag)-1])
|
2016-08-07 20:24:56 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 16:02:26 +00:00
|
|
|
func buildTermIndex(entries []termSource, entities map[string]string) termIndex {
|
|
|
|
dict := termIndex{
|
|
|
|
Version: DB_VERSION,
|
|
|
|
Banks: bankCount(len(entries)),
|
2016-08-22 02:51:43 +00:00
|
|
|
}
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2016-08-22 02:51:43 +00:00
|
|
|
for _, e := range entries {
|
2016-08-23 03:51:30 +00:00
|
|
|
def := []string{e.Expression, e.Reading, strings.Join(e.Tags, " ")}
|
|
|
|
def = append(def, e.Glossary...)
|
|
|
|
dict.defs = append(dict.defs, def)
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 16:02:26 +00:00
|
|
|
for name, value := range entities {
|
|
|
|
ent := []string{name, value}
|
|
|
|
dict.Entities = append(dict.Entities, ent)
|
2016-08-23 03:51:30 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 16:02:26 +00:00
|
|
|
return dict
|
2016-08-23 03:51:30 +00:00
|
|
|
}
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2016-08-24 16:02:26 +00:00
|
|
|
func outputTermIndex(outputDir string, entries []termSource, entities map[string]string, pretty bool) error {
|
2016-08-23 03:51:30 +00:00
|
|
|
if err := os.MkdirAll(outputDir, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2016-08-23 03:51:30 +00:00
|
|
|
outputIndex, err := os.Create(path.Join(outputDir, "index.json"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|
2016-08-23 03:51:30 +00:00
|
|
|
defer outputIndex.Close()
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2016-08-24 16:02:26 +00:00
|
|
|
dict := buildTermIndex(entries, entities)
|
2016-08-23 03:51:30 +00:00
|
|
|
indexBytes, err := marshalJson(dict, pretty)
|
2016-08-07 01:17:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:51:30 +00:00
|
|
|
if _, err = outputIndex.Write(indexBytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defCnt := len(dict.defs)
|
2016-08-24 16:02:26 +00:00
|
|
|
for i := 0; i < defCnt; i += BANK_STRIDE {
|
|
|
|
outputRef, err := os.Create(path.Join(outputDir, fmt.Sprintf("bank_%d.json", i/BANK_STRIDE+1)))
|
2016-08-23 03:51:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outputRef.Close()
|
|
|
|
|
|
|
|
indexSrc := i
|
2016-08-24 16:02:26 +00:00
|
|
|
indexDst := i + BANK_STRIDE
|
2016-08-23 03:51:30 +00:00
|
|
|
if indexDst > defCnt {
|
|
|
|
indexDst = defCnt
|
|
|
|
}
|
|
|
|
|
|
|
|
refBytes, err := marshalJson(dict.defs[indexSrc:indexDst], pretty)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = outputRef.Write(refBytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 16:02:26 +00:00
|
|
|
func marshalJson(obj interface{}, pretty bool) ([]byte, error) {
|
|
|
|
if pretty {
|
|
|
|
return json.MarshalIndent(obj, "", " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func bankCount(defCount int) int {
|
|
|
|
count := defCount / BANK_STRIDE
|
|
|
|
if defCount%BANK_STRIDE > 0 {
|
|
|
|
count += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-07 01:17:02 +00:00
|
|
|
func hasString(needle string, haystack []string) bool {
|
|
|
|
for _, value := range haystack {
|
|
|
|
if needle == value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|