Advertisement
Guest User

Untitled

a guest
May 31st, 2017
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package sfp
  2.  
  3. import "fmt"
  4.  
  5. //////////////////////////////////////////////////////////////////////////////////////////
  6.  
  7. const (
  8. VERB string = "KIND_verb"
  9. ADVERB string = "KIND_adv"
  10. NOUN string = "KIND_noun"
  11. ADJECTIVE string = "KIND_adj"
  12.  
  13. NONE string = "none"
  14. )
  15.  
  16. //this is the main function where input from the player is interpreted. Each line calls another function which does the dirty work, which are down bellow.
  17. func (g game) calculateInput(input string) {
  18. wordCount := countWords(input)
  19. local := g.determineLocalObjectsOfActor(g.player)
  20. Slice := simpleSlicer(input)
  21. ArticleSlice := getArticlePositions(Slice)
  22. PermutatedData := getPermutations(Slice, ArticleSlice, g.verbs, local, wordCount)
  23. fmt.Println(PermutatedData)
  24. final := putPuzzleTogether(PermutatedData, ArticleSlice, wordCount)
  25.  
  26. fmt.Println(final)
  27.  
  28. }
  29.  
  30. const (
  31. tverb = iota
  32. tadjective
  33. tnoun
  34. tadverb
  35. )
  36.  
  37. func putPuzzleTogether(PermutatedData []samp, ArticleSlice []article, wordCount int) []samp {
  38. position := 0
  39. curTarget := 0
  40. targets := [][]int{{tverb}, {tadjective, tnoun}, {tadverb}, {tadjective, tnoun}}
  41. solution := make([]samp, 0, 0)
  42. prevPositions := make([]int, 0, 0)
  43. used := make([]samp, 0, 0)
  44. for position < wordCount {
  45. for x := 0; x < len(PermutatedData); x++ {
  46. if PermutatedData[x].pos == position && !sliceOfSampContains(used, PermutatedData[x]) && matchesOneOfIntSlice(targets[curTarget], PermutatedData[x].target) {
  47. solution = append(solution, PermutatedData[x])
  48. prevPositions = append(prevPositions, position)
  49. used = append(used, PermutatedData[x])
  50. position += countWords(solution[len(solution)-1].term)
  51. if matchesOneArticlePosition(ArticleSlice, position) {
  52. position++
  53. }
  54. if PermutatedData[x].target != tadjective {
  55. if curTarget != 4 {
  56. curTarget++
  57. } else if position != wordCount-1 {
  58. position = prevPositions[len(prevPositions)-1]
  59. prevPositions = prevPositions[:len(prevPositions)-1]
  60. }
  61. }
  62. if position == wordCount {
  63. return solution
  64. }
  65. x = 0
  66. }
  67. }
  68. if len(prevPositions) > 0 {
  69. position = prevPositions[len(prevPositions)-1]
  70. prevPositions = prevPositions[:len(prevPositions)-1]
  71. solution = solution[:len(solution)-1]
  72. } else {
  73. return nil
  74. }
  75. }
  76. return solution
  77. }
  78.  
  79. func getPermutations(Slice []string, ArticleSlice []article, verbs []verb, local []*thing, wordCount int) []samp {
  80. sampList := make([]samp, 0, 0)
  81. verbPointers := make([]*verb, 0, 0)
  82. for x := 0; x < len(verbs); x++ {
  83. verbPointers = append(verbPointers, &verbs[x])
  84. }
  85. for x := -1; x < len(ArticleSlice); x++ { //for each space between the articles
  86. var rangeTop int //first lets the determine the top and bottom positions between these two articles
  87. var rangeBottom int
  88. if x == -1 { //we are before the first article
  89. rangeBottom = 0
  90. if len(ArticleSlice) == 0 {
  91. rangeTop = wordCount
  92. } else {
  93. rangeTop = ArticleSlice[0].position
  94. }
  95. } else if x >= 0 && x < len(ArticleSlice)-1 { //we are in between the first and the last article
  96. rangeBottom = ArticleSlice[x].position + 1
  97. rangeTop = ArticleSlice[x+1].position
  98. } else if x == len(ArticleSlice)-1 { //we are after the last article
  99. rangeBottom = ArticleSlice[x].position + 1
  100. rangeTop = wordCount
  101. }
  102. for y := rangeBottom; y < rangeTop; y++ { //for each index in the range between articles (between rangeBottom and rangeTop)
  103. for z := 0; z < rangeTop; z++ { //for every possible size of a term within this space
  104. if y+z < rangeTop { //if the selection fits between the articles
  105. testString := "" //lets make a test string to use
  106. for a := y; a < y+z+1; a++ { //for each word of the string
  107. testString += Slice[a] //add the word
  108. if a < y+z { //if we are not at the last word
  109. testString += " " //add a space
  110. }
  111. }
  112. interpretedWord := interpretWord(verbPointers, local, testString, y, x) //now that we built our test string, lets see if it matches anything.
  113. for a := 0; a < len(interpretedWord); a++ { //for every interpretation of the word that passed
  114. sampList = append(sampList, interpretedWord[a]) //add it to the samplist
  115. }
  116. }
  117. }
  118. }
  119. }
  120. return sampList
  121. }
  122.  
  123. func getArticlePositions(Slice []string) []article {
  124. returnSlice := make([]article, 0, 0)
  125. for x := 0; x < len(Slice); x++ {
  126. if isInteger(Slice[x]) {
  127. test := returnInteger(Slice[x])
  128. if test == 1 {
  129. returnSlice = append(returnSlice, article{test, false, x})
  130. } else {
  131. returnSlice = append(returnSlice, article{test, true, x})
  132. }
  133. } else if Slice[x] == "the" || Slice[x] == "this" || Slice[x] == "that" {
  134. returnSlice = append(returnSlice, article{1, false, x})
  135. } else if Slice[x] == "some" || Slice[x] == "those" || Slice[x] == "these" {
  136. returnSlice = append(returnSlice, article{1, true, x})
  137. } else if Slice[x] == "a" || Slice[x] == "an" {
  138. returnSlice = append(returnSlice, article{1, true, x})
  139. }
  140. }
  141. return returnSlice
  142. }
  143.  
  144. func simpleSlicer(phrase string) []string {
  145. slice := ""
  146. returns := make([]string, 0, 0)
  147. for x := 0; x < len(phrase); x++ {
  148. if string(phrase[x]) == " " && slice != "" {
  149. returns = append(returns, slice)
  150. slice = ""
  151. } else if x < len(phrase)-1 && string(phrase[x]) != "," {
  152. slice += string(phrase[x])
  153. } else if x == len(phrase)-1 && !isPunctuation(string(phrase[x])) {
  154. slice += string(phrase[x])
  155. }
  156. }
  157.  
  158. if slice != "" {
  159. returns = append(returns, slice)
  160. }
  161.  
  162. return returns
  163. }
  164.  
  165. func interpretWord(verbs []*verb, nouns []*thing, phrase string, pos int, art int) []samp {
  166.  
  167. samps := make([]samp, 0, 0)
  168. for _, v := range verbs {
  169. for _, vi := range v.verbs {
  170. if phrase == vi {
  171. // isVerb = true
  172. samps = append(samps, samp{tverb, v, nil, vi, pos, art})
  173. }
  174. }
  175.  
  176. for _, avi := range v.adverbs {
  177. if phrase == avi {
  178. // isAdverb = true
  179. samps = append(samps, samp{tadverb, v, nil, avi, pos, art})
  180. }
  181. }
  182. }
  183.  
  184. for _, n := range nouns {
  185. for _, ni := range n.terms {
  186. if ni == phrase {
  187. // isNoun = true
  188. samps = append(samps, samp{tnoun, nil, n, ni, pos, art})
  189. }
  190. }
  191.  
  192. for _, aji := range n.adjectives {
  193. if phrase == aji {
  194. // isAdjective = true
  195. samps = append(samps, samp{tadjective, nil, n, aji, pos, art})
  196. }
  197. }
  198. }
  199.  
  200. return samps
  201.  
  202. }
  203.  
  204. type samp struct {
  205. target int
  206. ver *verb
  207. nou *thing
  208. term string
  209. pos int
  210. article int
  211. }
  212.  
  213. type article struct {
  214. amount int
  215. plural bool
  216. position int
  217. }
Advertisement
RAW Paste Data Copied
Advertisement