search.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // Search searches for given phrase in given TMs.
  57. func (app *Application) Search(TMs []TM, text string) SearchResults {
  58. searchString := "{ \"SearchExpression\": [ \"" + text + "\" ]}"
  59. searchJSON := []byte(searchString)
  60. tmURL := app.BaseURL + "tms/"
  61. var finalResults SearchResults
  62. finalResults.SearchPhrase = text
  63. var results []CleanedResults
  64. for _, tm := range TMs {
  65. getTM := tmURL + tm.TMGuid
  66. concordanceURL := getTM + "/concordance"
  67. requestURL := concordanceURL + app.AuthString
  68. resp := PostQuery(requestURL, searchJSON)
  69. defer resp.Body.Close()
  70. if resp.StatusCode == 401 {
  71. time.Sleep(app.Delay)
  72. app.Login()
  73. return app.Search(TMs, text)
  74. }
  75. var tempResults ResultsFromServer
  76. JSONDecoder(resp.Body, &tempResults)
  77. if tempResults.TotalConcResult > 0 {
  78. var tmResults CleanedResults
  79. //Allocating Segments array beforehand
  80. tmResults.Segments = make([]Segment, 0, tempResults.TotalConcResult)
  81. tmResults.TMName = tm.FriendlyName
  82. for _, result := range tempResults.ConcResult {
  83. segment := Segment{result.TMEntry.SourceSegment, result.TMEntry.TargetSegment}
  84. segment.Clean()
  85. tmResults.Segments = append(tmResults.Segments, segment)
  86. }
  87. results = append(results, tmResults)
  88. finalResults.TotalResults += len(tmResults.Segments)
  89. }
  90. }
  91. finalResults.Results = results
  92. return finalResults
  93. }