The problem
A Noun Phrase is a phrase that may be changed by a pronoun [he/she/it].
For instance, within the sentence:
a lady ate the cookie
“A lady” is a Noun Phrase, so we will say “she ate the cookie.” “The cookie” can be a Noun Phrase, so we will even say “she ate it.”
Each of those Noun Phrases are made up of an auxialliary (aux) [the/a] adopted by a Noun (N) [girl/cookie].
Extra difficult Noun Phrases may embody an arbitrary variety of adjectives (adj). For instance:
an enormous tall scary lady ate the cookie
the place “large”, “tall”, and “scary” are all adjectives.
For our simplified mannequin, we’ll outline a Noun Phrase as an auxiliary, adopted by 0 or extra adjectives, adopted by a Noun.
Write a operate that takes a sentence (as a string of phrases separated by areas), a listing of pronouns, and a dictionary (maping phrases to the grammatical classes aux, adj, and N). Return the sentcnce with all Noun Phrases changed by pronouns, within the order given.
Assume:
- sentences will comprise solely lower-case letters and areas (no puctuation)
- any phrases not inlcuded within the dictionary aren’t auxilliaries, adjectives, or nouns
- the variety of pronouns offered can be equal to the variety of Noun Phrases within the sentence
The answer in Golang
Possibility 1:
package deal answer
import "strings"
func ReplaceNounPhrases(str string, professional []string, dict map[string]string) string{
phrases, phrase := strings.Fields(str), ""
state, noun := 0, 0
if len(professional) < 1 {return str}
for _, phrase := vary phrases {
if _, okay := dict[word]; !okay {state = 0; proceed}
if state == 0 && dict[word] != "aux" {proceed}
if state == 1 {
if dict[word] == "adj" {phrase += " " + phrase}
if dict[word] == "N" {
phrase += " " + phrase
str = strings.Substitute(str, phrase, professional[noun], 1)
if noun < len(professional) - 1 {noun++}
state = 0
}
}
if dict[word] == "aux" {state, phrase = 1, phrase}
}
return str
}
Possibility 2:
package deal answer
import "strings"
func ReplaceNounPhrases(sentence string, pronouns []string, dictionary map[string]string) string {
phrases, grams, res := strings.Cut up(sentence, " "), []string{}, []string{}
for _, phrase := vary phrases { grams = append(grams, dictionary[word]) }
n, i, j, ok := len(phrases), 0, 0, 0
for i<n {
phrase:=phrases[i]
if i<n-1 && grams[i]=="aux" {
j = i+1
for j<n && grams[j]=="adj" { j++ }
if j<n && grams[j]=="N" { phrase = pronouns[k]; ok++; i = j+1 } else { i++ }
} else { i++ }
res = append(res, phrase)
}
return strings.Be part of(res, " ")
}
Possibility 3:
package deal answer
import "strings"
func ReplaceNounPhrases(sentence string, pronouns []string, dictionary map[string]string) string {
phrases := strings.Cut up(sentence, " ")
aux := -1
var pronoun int
for i := 0; i < len(phrases); i++ {
if _, okay := dictionary[words[i]]; !okay {
aux = -1
} else if dictionary[words[i]] == "aux" {
aux = i
} else if dictionary[words[i]] == "N" && aux != -1 {
left := append(phrases[:aux], pronouns[pronoun])
proper := phrases[aux+i-aux+1:]
phrases = append(left, proper...)
pronoun++
aux = -1
i = aux
}
}
return strings.Be part of(phrases, " ")
}
Take a look at instances to validate our answer
package deal solution_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var dictionary = map[string]string{
"a": "aux",
"the": "aux",
"lady" : "N",
"cookie" : "N",
"large" : "adj",
"tall" : "adj",
"scary" : "adj",
}
var _ = Describe("Take a look at Instance", func() {
It("#1", func() {
Count on(ReplaceNounPhrases("a lady", []string{"she"}, dictionary)).To(Equal("she"))
})
It("#2", func() {
Count on(ReplaceNounPhrases("a lady ate the cookie", []string{"she", "it"}, dictionary)).To(Equal("she ate it"))
})
It("#3", func() {
Count on(ReplaceNounPhrases("a a an enormous large a cookie", []string{"it"}, dictionary)).To(Equal("a a an enormous large it"))
})
It("#4", func() {
Count on(ReplaceNounPhrases("a scary lady", []string{"she"}, dictionary)).To(Equal("she"))
})
It("#5", func() {
Count on(ReplaceNounPhrases("an enormous tall scary lady ate the large cookie", []string{"she", "it"}, dictionary)).To(Equal("she ate it"))
})
})