-
Notifications
You must be signed in to change notification settings - Fork 1
/
mesh2solrsyn.go
88 lines (71 loc) · 1.77 KB
/
mesh2solrsyn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* See LICENSE file for copyright and license details. */
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/Shugyousha/mesh"
)
type prefices []string
func (p prefices) Len() int {
return len(p)
}
func (p prefices) Less(i, j int) bool {
return len(p[i]) > len(p[j])
}
func (p prefices) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
var (
meshrecs mesh.MeSHRecordsMap
)
func gethyposyns(path string, mn *mesh.MeSHNode) []string {
var res []string
suffs := mn.GetSamePrefix(path)
for _, suf := range suffs {
rec, ok := meshrecs[suf]
if !ok {
fmt.Fprintf(os.Stderr, "Suf: %q does not have a record! Please check.\n", suf)
}
for s, _ := range rec.Entries {
res = append(res, s)
}
res = append(res, rec.MH)
}
return res
}
func main() {
mn := mesh.NewNode(make(map[string]*mesh.MeSHNode, 5))
meshrecs = make(mesh.MeSHRecordsMap, 30000)
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Need a file to read the MeSH terms from as an argument. Exiting.\n")
os.Exit(1)
}
r := bufio.NewReader(os.Stdin)
mpt := mesh.NewMeSHTreeParser(*r)
mpt.ParseMeSHTree(*mn)
f, err := os.Open(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open file: %s\n", err)
}
fr := bufio.NewReader(f)
mp := mesh.NewMeSHParser(*fr)
_, meshrecs = mp.ParseToSliceAndMap()
//suff := mn.GetSamePrefix("G11.561.600.810.964.186.624"})
for r, _ := range meshrecs {
syns := gethyposyns(r, mn)
rec, ok := meshrecs[r]
if !ok {
fmt.Fprintf(os.Stderr, "Record: %q does not exist! Please check.\n", r)
os.Exit(1)
}
for s, _ := range rec.Entries {
syns = append(syns, s)
}
if len(syns) > 0 {
//fmt.Fprintf(os.Stderr, "Rec: %q %q, syns: %q\n", r, rec.MH, strings.Join(syns, ","))
fmt.Printf("%s => %s\n", rec.MH, strings.Join(syns, ", "))
}
}
}