search.go 3.0 KB

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