I am currently working on a Ruby problem in which I am essentially creating my own language. Below (nouns, verbs, articles) can be thought as words. However, in order to create a valid sentence, I must have a verb, a noun, or at least 2 articles.
Nouns: "abcd", "c", "def", "h", "ij, "cde"
Verbs: "bc", "fg", "g", "hij", "bcd"
Articles: "a", "ac", "e"
So what I am trying to do is basically write a method that takes a string and returns all possible valid sentences (while keeping characters in the same order and inserting a space between the words)
ex. input = "abcdefg"
returns the list
[ "a bc def g", "a bcd e fg", "abcd e fg"]
So I tried breaking down the problem and this is what I have so far
alpha = "abcdefg"
nouns = ["abcd", "c", "def", "h", "ij", "cde"]
verbs = ["bc", "fg", "g", "hij", "bcd"]
articles = ["a", "ac", "e"]
verbArray = [];
nounArray = [];
articleArray = [];
nouns.each do |item|
if alpha.include?(item)
nounArray << item
end
end
verbs.each do |item|
if alpha.include?(item)
verbArray << item
end
end
articles.each do |item|
if alpha.include?(item)
articleArray << item
end
end
puts nounArray.inspect => ["abcd", "c", "def", "cde"]
puts verbArray.inspect => ["bc", "fg", "g", "bcd"]
puts articleArray.inspect => ["a", "e"]
My thought process was that I first wanted to get all possible combinations for each of the words (nouns, verb, article). I am not sure if this is the most efficient way to approach this problem but beyond this step I was trying without much success to form ordered sentences.
I've been searching stacks and other websites for types of combinations/sorting techniques plus I am trying to avoid using regex at the moment. I would honestly appreciate any direction/feedback as how to continue my journey to solving this problem.Thank you for your time!
verb/noun/articleorder?I must have a verb, a noun, or at least 2 articles.is ambiguous.