-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (133 loc) · 4.02 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bufio"
"github.com/gin-gonic/gin"
"github.com/hanglics/gocheck/pkg/checker"
"github.com/hanglics/gocheck/pkg/loader"
"github.com/hscells/cqr"
"github.com/hscells/groove/analysis"
"github.com/hscells/transmute"
tpipeline "github.com/hscells/transmute/pipeline"
"github.com/ielab/searchrefiner"
"net/http"
"path/filepath"
"regexp"
"strings"
)
type AutoDocPlugin struct {
}
func handleQueryValidation(s searchrefiner.Server, c *gin.Context) {
rawQuery := c.PostForm("query")
lang := c.PostForm("lang")
absPathFields, _ := filepath.Abs("../searchrefiner/dictionary/fields.txt")
fieldsDictionary := loader.LoadDictionary(absPathFields)
absPath, _ := filepath.Abs("../searchrefiner/dictionary/words.txt")
keywordDictionary := loader.LoadDictionary(absPath)
lang = strings.ToLower(lang)
var fieldsError []string
if strings.ToLower(lang) == "medline" {
scanner := bufio.NewScanner(strings.NewReader(rawQuery))
var extractedFields []string
for scanner.Scan() {
temp := scanner.Text()
line := temp[3:]
reg := regexp.MustCompile(`\.([^.]+)\.`)
rawFields := reg.FindAllStringSubmatch(line, -1)
if len(rawFields) > 0 {
extractedFields = append(extractedFields, rawFields[0][1])
}
}
for _, i := range extractedFields {
flag := checker.CheckWord(fieldsDictionary, strings.ToLower(i), 0)
if !flag {
fieldsError = append(fieldsError, i)
}
}
} else if strings.ToLower(lang) == "pubmed" {
reg := regexp.MustCompile(`\[([^]]+)\]`)
rawFields := reg.FindAllStringSubmatch(rawQuery, -1)
for _, i := range rawFields {
flag := checker.CheckWord(fieldsDictionary, strings.ToLower(i[1]), 0)
if !flag {
fieldsError = append(fieldsError, i[1])
}
}
}
p := make(map[string]tpipeline.TransmutePipeline)
p["medline"] = transmute.Medline2Cqr
p["pubmed"] = transmute.Pubmed2Cqr
compiler := p["medline"]
if v, ok := p[lang]; ok {
compiler = v
} else {
lang = "medline"
}
cq, err := compiler.Execute(rawQuery)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
repr, err := cq.Representation()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
}
commonRepr := repr.(cqr.CommonQueryRepresentation)
keywords := analysis.QueryKeywords(commonRepr)
var spellErrors []string
resp := make(map[string][]string)
for i := 0; i < len(keywords); i++ {
keyword := keywords[i].QueryString
keyword = strings.ToLower(keyword)
var slices []string
if strings.Contains(keyword, " ") {
slices = strings.Split(keyword, " ")
for _, s := range slices {
if strings.Contains(s, "*") {
s = s[:len(s)-1]
}
flag := checker.CheckWord(keywordDictionary, s, 0)
if !flag {
spellErrors = append(spellErrors, s)
}
}
} else {
if strings.Contains(keyword, "*") {
keyword = keyword[:len(keyword)-1]
}
flag := checker.CheckWord(keywordDictionary, keyword, 0)
if !flag {
spellErrors = append(spellErrors, keyword)
}
}
}
resp["keyword"] = spellErrors
resp["fields"] = fieldsError
c.JSON(http.StatusOK, resp)
}
func (AutoDocPlugin) Serve(s searchrefiner.Server, c *gin.Context) {
if c.Request.Method == "POST" && c.Query("validate") == "y" {
handleQueryValidation(s, c)
return
}
rawQuery := c.PostForm("query")
lang := c.PostForm("lang")
c.Render(http.StatusOK, searchrefiner.RenderPlugin(searchrefiner.TemplatePlugin("plugin/autodoc/index.html"), struct {
searchrefiner.Query
View string
}{searchrefiner.Query{QueryString: rawQuery, Language: lang, PluginTitle: "AutoDoc", Plugins: s.Plugins}, c.Query("view")}))
return
}
func (AutoDocPlugin) PermissionType() searchrefiner.PluginPermission {
return searchrefiner.PluginUser
}
func (AutoDocPlugin) Details() searchrefiner.PluginDetails {
return searchrefiner.PluginDetails{
Title: "AutoDoc",
Description: "Automatically generate search reports based on the information provided.",
Author: "ielab",
Version: "26.Aug.2020",
ProjectURL: "ielab.io/searchrefiner",
AcceptsQueryPosts: true,
}
}
var Autodoc = AutoDocPlugin{}