-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
133 lines (107 loc) · 3.21 KB
/
search.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
package main
import (
"encoding/json"
"log"
"net/http"
"regexp"
"time"
)
// Segment stores source and translated texts.
type Segment struct {
Source, Target, DocumentName string
}
func (s *Segment) clean() {
re := regexp.MustCompile("</?seg>")
s.Source = re.ReplaceAllString(s.Source, "")
s.Target = re.ReplaceAllString(s.Target, "")
}
// CleanedResults stores processed results from given TM.
type CleanedResults struct {
TMName string
Segments []Segment
}
// SearchResults stores processed results from all TMs.
type SearchResults struct {
SearchPhrase string
Results []*CleanedResults
TotalResults int
}
func getCleanedResults(tempResults *ResultsFromServer, TMFriendlyName string) CleanedResults {
var tmResults CleanedResults
var numberOfSegments = len(tempResults.ConcResult)
tmResults.Segments = make([]Segment, numberOfSegments)
tmResults.TMName = TMFriendlyName
for index := 0; index < numberOfSegments; index++ {
result := tempResults.ConcResult[index]
segment := Segment{result.TMEntry.SourceSegment, result.TMEntry.TargetSegment, result.TMEntry.DocumentName}
segment.clean()
tmResults.Segments[index] = segment
}
return tmResults
}
func getSearchJSON(info *SearchInfo) []byte {
query := searchQuery{}
query.SearchExpression = append(query.SearchExpression, info.Phrase)
query.Options.CaseSensitive = false
query.Options.ReverseLookup = info.Reverse
query.Options.ResultsLimit = info.SearchLimit
queryJSON, err := json.Marshal(query)
if err != nil {
log.Printf("Error marshalling query: %s", err)
return []byte{}
}
return queryJSON
}
func (app *Application) getResultsFromTM(tmURL string, tm *TM, searchJSON []byte) (retry bool, result ResultsFromServer) {
getTM := tmURL + tm.TMGuid
concordanceURL := getTM + "/concordance"
requestURL := concordanceURL + app.AuthString
var tempResults ResultsFromServer
resp, err := postQuery(requestURL, searchJSON)
if err != nil {
log.Println(err)
return false, tempResults
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusUnauthorized {
time.Sleep(app.Delay)
status, err := app.login()
if !status || err != nil {
log.Printf("Couldn't log in: %s", err)
return false, tempResults
}
return true, tempResults
}
if resp.StatusCode != http.StatusOK {
log.Printf("Problem getting results (%s)!", resp.Status)
return false, tempResults
}
err = jsonDecoder(resp.Body, &tempResults)
if err != nil {
log.Printf("Error decoding results: %s", err)
}
return false, tempResults
}
func (app *Application) search(tms []TM, info *SearchInfo) SearchResults {
var finalResults SearchResults
finalResults.SearchPhrase = info.Phrase
searchJSON := getSearchJSON(info)
if len(searchJSON) == 0 {
return finalResults
}
tmURL := app.BaseURL + "tms/"
max := len(tms)
for i := 0; i < max; i++ {
retry, tempResults := app.getResultsFromTM(tmURL, &tms[i], searchJSON)
if retry {
_, tempResults = app.getResultsFromTM(tmURL, &tms[i], searchJSON)
}
if tempResults.TotalConcResult <= 0 {
continue
}
tmResults := getCleanedResults(&tempResults, tms[i].FriendlyName)
finalResults.Results = append(finalResults.Results, &tmResults)
finalResults.TotalResults += len(tmResults.Segments)
}
return finalResults
}