search.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "regexp"
  7. "time"
  8. )
  9. // Segment stores source and translated texts.
  10. type Segment struct {
  11. Source, Target string
  12. }
  13. func (s *Segment) clean() {
  14. re := regexp.MustCompile("</?seg>")
  15. s.Source = re.ReplaceAllString(s.Source, "")
  16. s.Target = re.ReplaceAllString(s.Target, "")
  17. }
  18. // CleanedResults stores processed results from given TM.
  19. type CleanedResults struct {
  20. TMName string
  21. Segments []Segment
  22. }
  23. // SearchResults stores processed results from all TMs.
  24. type SearchResults struct {
  25. SearchPhrase string
  26. Results []CleanedResults
  27. TotalResults int
  28. }
  29. // ResultsFromServer stores results as received from server.
  30. type ResultsFromServer struct {
  31. ConcResult []struct {
  32. ConcordanceTextRanges []struct {
  33. Length, Start int
  34. }
  35. ConcordanceTranslationRanges []string
  36. Length, StartPos int
  37. TMEntry struct {
  38. SourceSegment, TargetSegment string
  39. }
  40. }
  41. ConcTransResult, Errors []string
  42. TotalConcResult int
  43. }
  44. func getCleanedResults(tempResults ResultsFromServer, TMFriendlyName string) CleanedResults {
  45. var tmResults CleanedResults
  46. maxReturnedBySever := 64
  47. numberOfSegments := tempResults.TotalConcResult
  48. if numberOfSegments > maxReturnedBySever {
  49. numberOfSegments = maxReturnedBySever
  50. }
  51. tmResults.Segments = make([]Segment, numberOfSegments)
  52. tmResults.TMName = TMFriendlyName
  53. for index := 0; index < numberOfSegments; index++ {
  54. result := tempResults.ConcResult[index]
  55. segment := Segment{result.TMEntry.SourceSegment, result.TMEntry.TargetSegment}
  56. segment.clean()
  57. tmResults.Segments[index] = segment
  58. }
  59. return tmResults
  60. }
  61. type searchQuery struct {
  62. SearchExpression []string
  63. }
  64. func getSearchJSON(text string) []byte {
  65. query := searchQuery{}
  66. query.SearchExpression = append(query.SearchExpression, text)
  67. queryJSON, err := json.Marshal(query)
  68. if err != nil {
  69. log.Printf("Error marshalling query: %s", err)
  70. return []byte{}
  71. }
  72. return queryJSON
  73. }
  74. func (app Application) getResultsFromTM(tmURL string, tm TM, searchJSON []byte) (retry bool, result ResultsFromServer) {
  75. getTM := tmURL + tm.TMGuid
  76. concordanceURL := getTM + "/concordance"
  77. requestURL := concordanceURL + app.AuthString
  78. var tempResults ResultsFromServer
  79. resp, err := postQuery(requestURL, searchJSON)
  80. if err != nil {
  81. log.Println(err)
  82. return false, tempResults
  83. }
  84. defer resp.Body.Close()
  85. if resp.StatusCode == http.StatusUnauthorized {
  86. time.Sleep(app.Delay)
  87. status, err := app.login()
  88. if !status || err != nil {
  89. log.Printf("Couldn't log in: %s", err)
  90. return false, tempResults
  91. }
  92. return true, tempResults
  93. }
  94. err = jsonDecoder(resp.Body, &tempResults)
  95. if err != nil {
  96. log.Printf("Error decoding results: %s", err)
  97. }
  98. return false, tempResults
  99. }
  100. func (app Application) search(TMs []TM, text string) SearchResults {
  101. var finalResults SearchResults
  102. finalResults.SearchPhrase = text
  103. searchJSON := getSearchJSON(text)
  104. if len(searchJSON) == 0 {
  105. return finalResults
  106. }
  107. tmURL := app.BaseURL + "tms/"
  108. for _, tm := range TMs {
  109. retry, tempResults := app.getResultsFromTM(tmURL, tm, searchJSON)
  110. if retry {
  111. _, tempResults = app.getResultsFromTM(tmURL, tm, searchJSON)
  112. }
  113. if tempResults.TotalConcResult <= 0 {
  114. continue
  115. }
  116. tmResults := getCleanedResults(tempResults, tm.FriendlyName)
  117. finalResults.Results = append(finalResults.Results, tmResults)
  118. finalResults.TotalResults += len(tmResults.Segments)
  119. }
  120. return finalResults
  121. }