Advertisement
Ramaraunt1

Untitled

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