search.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "log"
  6. "net/http"
  7. "regexp"
  8. "time"
  9. )
  10. // Segment stores source and translated texts.
  11. type Segment struct {
  12. Source, Target string
  13. }
  14. // Clean cleans <seg> tags from source and translated texts.
  15. func (s *Segment) Clean() {
  16. re := regexp.MustCompile("</?seg>")
  17. s.Source = re.ReplaceAllString(s.Source, "")
  18. s.Target = re.ReplaceAllString(s.Target, "")
  19. }
  20. // CleanedResults stores processed results from given TM.
  21. type CleanedResults struct {
  22. TMName string
  23. Segments []Segment
  24. }
  25. // SearchResults stores processed results from all TMs.
  26. type SearchResults struct {
  27. SearchPhrase string
  28. Results []CleanedResults
  29. TotalResults int
  30. }
  31. // ResultsFromServer stores results as received from server.
  32. type ResultsFromServer struct {
  33. ConcResult []struct {
  34. ConcordanceTextRanges []struct {
  35. Length, Start int
  36. }
  37. ConcordanceTranslationRanges []string
  38. Length, StartPos int
  39. TMEntry struct {
  40. SourceSegment, TargetSegment string
  41. }
  42. }
  43. ConcTransResult, Errors []string
  44. TotalConcResult int
  45. }
  46. // PostQuery sends POST query to server and returns response.
  47. func PostQuery(requestURL string, searchJSON []byte) *http.Response {
  48. req, err := http.NewRequest("POST", requestURL, bytes.NewBuffer(searchJSON))
  49. req.Header.Set("Content-Type", "application/json")
  50. client := &http.Client{}
  51. resp, err := client.Do(req)
  52. if err != nil {
  53. log.Printf("Error posting query: %v", err)
  54. }
  55. return resp
  56. }
  57. func getCleanedResults(tempResults ResultsFromServer, TMFriendlyName string) CleanedResults {
  58. var tmResults CleanedResults
  59. //64 is maximum returned by server
  60. var numberOfSegments int
  61. if tempResults.TotalConcResult > 64 {
  62. numberOfSegments = 64
  63. } else {
  64. numberOfSegments = tempResults.TotalConcResult
  65. }
  66. //Allocating Segments array beforehand
  67. tmResults.Segments = make([]Segment, 0, numberOfSegments)
  68. tmResults.TMName = TMFriendlyName
  69. for _, result := range tempResults.ConcResult {
  70. segment := Segment{result.TMEntry.SourceSegment, result.TMEntry.TargetSegment}
  71. segment.Clean()
  72. tmResults.Segments = append(tmResults.Segments, segment)
  73. }
  74. return tmResults
  75. }
  76. type searchQuery struct {
  77. SearchExpression []string
  78. }
  79. func getSearchJSON(text string) []byte {
  80. query := searchQuery{}
  81. query.SearchExpression = append(query.SearchExpression, text)
  82. queryJSON, err := json.Marshal(query)
  83. if err != nil {
  84. log.Printf("Error marshalling query: %v", err)
  85. return []byte{}
  86. }
  87. return queryJSON
  88. }
  89. // Search for given phrase in given TMs.
  90. func (app *Application) Search(TMs []TM, text string) SearchResults {
  91. searchJSON := getSearchJSON(text)
  92. tmURL := app.BaseURL + "tms/"
  93. var finalResults SearchResults
  94. finalResults.SearchPhrase = text
  95. for _, tm := range TMs {
  96. getTM := tmURL + tm.TMGuid
  97. concordanceURL := getTM + "/concordance"
  98. requestURL := concordanceURL + app.AuthString
  99. resp := PostQuery(requestURL, searchJSON)
  100. defer resp.Body.Close()
  101. if resp.StatusCode == 401 {
  102. time.Sleep(app.Delay)
  103. app.Login()
  104. return app.Search(TMs, text)
  105. }
  106. var tempResults ResultsFromServer
  107. JSONDecoder(resp.Body, &tempResults)
  108. if tempResults.TotalConcResult > 0 {
  109. tmResults := getCleanedResults(tempResults, tm.FriendlyName)
  110. finalResults.Results = append(finalResults.Results, tmResults)
  111. finalResults.TotalResults += len(tmResults.Segments)
  112. }
  113. }
  114. return finalResults
  115. }