Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sfp
- import "fmt"
- //////////////////////////////////////////////////////////////////////////////////////////
- const (
- VERB string = "KIND_verb"
- ADVERB string = "KIND_adv"
- NOUN string = "KIND_noun"
- ADJECTIVE string = "KIND_adj"
- NONE string = "none"
- )
- //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.
- func (g game) calculateInput(input string) {
- wordCount := countWords(input)
- local := g.determineLocalObjectsOfActor(g.player)
- Slice := simpleSlicer(input)
- ArticleSlice := getArticlePositions(Slice)
- PermutatedData := getPermutations(Slice, ArticleSlice, g.verbs, local, wordCount)
- fmt.Println(PermutatedData)
- final := putPuzzleTogether(PermutatedData, ArticleSlice, wordCount)
- fmt.Println(final)
- }
- const (
- tverb = iota
- tadjective
- tnoun
- tadverb
- )
- func putPuzzleTogether(PermutatedData []samp, ArticleSlice []article, wordCount int) []samp {
- position := 0
- curTarget := 0
- targets := [][]int{{tverb}, {tadjective, tnoun}, {tadverb}, {tadjective, tnoun}}
- solution := make([]samp, 0, 0)
- prevPositions := make([]int, 0, 0)
- used := make([]samp, 0, 0)
- for position < wordCount {
- for x := 0; x < len(PermutatedData); x++ {
- if PermutatedData[x].pos == position && !sliceOfSampContains(used, PermutatedData[x]) && matchesOneOfIntSlice(targets[curTarget], PermutatedData[x].target) {
- solution = append(solution, PermutatedData[x])
- prevPositions = append(prevPositions, position)
- used = append(used, PermutatedData[x])
- position += countWords(solution[len(solution)-1].term)
- if matchesOneArticlePosition(ArticleSlice, position) {
- position++
- }
- if PermutatedData[x].target != tadjective {
- if curTarget != 4 {
- curTarget++
- } else if position != wordCount-1 {
- position = prevPositions[len(prevPositions)-1]
- prevPositions = prevPositions[:len(prevPositions)-1]
- }
- }
- if position == wordCount {
- return solution
- }
- x = 0
- }
- }
- if len(prevPositions) > 0 {
- position = prevPositions[len(prevPositions)-1]
- prevPositions = prevPositions[:len(prevPositions)-1]
- solution = solution[:len(solution)-1]
- } else {
- return nil
- }
- }
- return solution
- }
- func getPermutations(Slice []string, ArticleSlice []article, verbs []verb, local []*thing, wordCount int) []samp {
- sampList := make([]samp, 0, 0)
- verbPointers := make([]*verb, 0, 0)
- for x := 0; x < len(verbs); x++ {
- verbPointers = append(verbPointers, &verbs[x])
- }
- for x := -1; x < len(ArticleSlice); x++ { //for each space between the articles
- var rangeTop int //first lets the determine the top and bottom positions between these two articles
- var rangeBottom int
- if x == -1 { //we are before the first article
- rangeBottom = 0
- if len(ArticleSlice) == 0 {
- rangeTop = wordCount
- } else {
- rangeTop = ArticleSlice[0].position
- }
- } else if x >= 0 && x < len(ArticleSlice)-1 { //we are in between the first and the last article
- rangeBottom = ArticleSlice[x].position + 1
- rangeTop = ArticleSlice[x+1].position
- } else if x == len(ArticleSlice)-1 { //we are after the last article
- rangeBottom = ArticleSlice[x].position + 1
- rangeTop = wordCount
- }
- for y := rangeBottom; y < rangeTop; y++ { //for each index in the range between articles (between rangeBottom and rangeTop)
- for z := 0; z < rangeTop; z++ { //for every possible size of a term within this space
- if y+z < rangeTop { //if the selection fits between the articles
- testString := "" //lets make a test string to use
- for a := y; a < y+z+1; a++ { //for each word of the string
- testString += Slice[a] //add the word
- if a < y+z { //if we are not at the last word
- testString += " " //add a space
- }
- }
- interpretedWord := interpretWord(verbPointers, local, testString, y, x) //now that we built our test string, lets see if it matches anything.
- for a := 0; a < len(interpretedWord); a++ { //for every interpretation of the word that passed
- sampList = append(sampList, interpretedWord[a]) //add it to the samplist
- }
- }
- }
- }
- }
- return sampList
- }
- func getArticlePositions(Slice []string) []article {
- returnSlice := make([]article, 0, 0)
- for x := 0; x < len(Slice); x++ {
- if isInteger(Slice[x]) {
- test := returnInteger(Slice[x])
- if test == 1 {
- returnSlice = append(returnSlice, article{test, false, x})
- } else {
- returnSlice = append(returnSlice, article{test, true, x})
- }
- } else if Slice[x] == "the" || Slice[x] == "this" || Slice[x] == "that" {
- returnSlice = append(returnSlice, article{1, false, x})
- } else if Slice[x] == "some" || Slice[x] == "those" || Slice[x] == "these" {
- returnSlice = append(returnSlice, article{1, true, x})
- } else if Slice[x] == "a" || Slice[x] == "an" {
- returnSlice = append(returnSlice, article{1, true, x})
- }
- }
- return returnSlice
- }
- func simpleSlicer(phrase string) []string {
- slice := ""
- returns := make([]string, 0, 0)
- for x := 0; x < len(phrase); x++ {
- if string(phrase[x]) == " " && slice != "" {
- returns = append(returns, slice)
- slice = ""
- } else if x < len(phrase)-1 && string(phrase[x]) != "," {
- slice += string(phrase[x])
- } else if x == len(phrase)-1 && !isPunctuation(string(phrase[x])) {
- slice += string(phrase[x])
- }
- }
- if slice != "" {
- returns = append(returns, slice)
- }
- return returns
- }
- func interpretWord(verbs []*verb, nouns []*thing, phrase string, pos int, art int) []samp {
- samps := make([]samp, 0, 0)
- for _, v := range verbs {
- for _, vi := range v.verbs {
- if phrase == vi {
- // isVerb = true
- samps = append(samps, samp{tverb, v, nil, vi, pos, art})
- }
- }
- for _, avi := range v.adverbs {
- if phrase == avi {
- // isAdverb = true
- samps = append(samps, samp{tadverb, v, nil, avi, pos, art})
- }
- }
- }
- for _, n := range nouns {
- for _, ni := range n.terms {
- if ni == phrase {
- // isNoun = true
- samps = append(samps, samp{tnoun, nil, n, ni, pos, art})
- }
- }
- for _, aji := range n.adjectives {
- if phrase == aji {
- // isAdjective = true
- samps = append(samps, samp{tadjective, nil, n, aji, pos, art})
- }
- }
- }
- return samps
- }
- type samp struct {
- target int
- ver *verb
- nou *thing
- term string
- pos int
- article int
- }
- type article struct {
- amount int
- plural bool
- position int
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement