search.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "regexp"
  6. "time"
  7. )
  8. // Segment stores source and translated texts.
  9. type Segment struct {
  10. Source, Target string
  11. }
  12. // Clean cleans <seg> tags from source and translated texts.
  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. //64 is maximum returned by server
  47. var numberOfSegments int
  48. if tempResults.TotalConcResult > 64 {
  49. numberOfSegments = 64
  50. } else {
  51. numberOfSegments = tempResults.TotalConcResult
  52. }
  53. //Allocating Segments array beforehand
  54. tmResults.Segments = make([]Segment, 0, numberOfSegments)
  55. tmResults.TMName = TMFriendlyName
  56. for _, result := range tempResults.ConcResult {
  57. segment := Segment{result.TMEntry.SourceSegment, result.TMEntry.TargetSegment}
  58. segment.Clean()
  59. tmResults.Segments = append(tmResults.Segments, segment)
  60. }
  61. return tmResults
  62. }
  63. type searchQuery struct {
  64. SearchExpression []string
  65. }
  66. func getSearchJSON(text string) []byte {
  67. query := searchQuery{}
  68. query.SearchExpression = append(query.SearchExpression, text)
  69. queryJSON, err := json.Marshal(query)
  70. if err != nil {
  71. log.Printf("Error marshalling query: %v", err)
  72. return []byte{}
  73. }
  74. return queryJSON
  75. }
  76. // Search for given phrase in given TMs.
  77. func (app *Application) Search(TMs []TM, text string) SearchResults {
  78. searchJSON := getSearchJSON(text)
  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, err := postQuery(requestURL, searchJSON)
  87. if err != nil {
  88. log.Println(err)
  89. return finalResults
  90. }
  91. defer resp.Body.Close()
  92. if resp.StatusCode == 401 {
  93. time.Sleep(app.Delay)
  94. app.Login()
  95. return app.Search(TMs, text)
  96. }
  97. var tempResults ResultsFromServer
  98. JSONDecoder(resp.Body, &tempResults)
  99. if tempResults.TotalConcResult > 0 {
  100. tmResults := getCleanedResults(tempResults, tm.FriendlyName)
  101. finalResults.Results = append(finalResults.Results, tmResults)
  102. finalResults.TotalResults += len(tmResults.Segments)
  103. }
  104. }
  105. return finalResults
  106. }