How to verify if a word has valid syllables or not?
algorithm
Solution
Whenever we have collected a full pattern-string, we can either discard it and begin collecting to a new pattern-string, or keep it and try to get a longer pattern-string. We don't know in advance (without examining the rest of the input-string), whether we should keep or discard the current string, so we need to keep both possibilities in mind.
We can build a state machine that can keep track of this for us. The base-states are identified by the sequence of characters we have examined so far:
State C V
"" {"C"} {"V",""}
"C" {"CC"} {"CV",""}
"CC" {"CCC"} {""}
"CCC" {} {""}
"CV" {"CVC",""} {}
"CVC" {""} {}
"V" {""} {}
Since we don't always know which action to take, we can be in several possible states at once. Those sets of possible states form super-states:
Index Super-state C V
0 {} 0 0 Fail
1 {""} 2 9 Accept
2 {"C"} 3 8
3 {"CC"} 4 1
4 {"CCC"} 0 1
5 {"","C"} 6 13 Accept
6 {"C","CC"} 7 8
7 {"CC","CCC"} 4 1
8 {"","CV"} 12 9 Accept
9 {"","V"} 5 9 Accept
10 {"","C","CC"} 11 13 Accept
11 {"C","CC","CCC"} 7 8
12 {"","C","CVC"} 10 13 Accept
13 {"","CV","V"} 12 9 Accept
The transitions are between super-states. Each member of the super-state is advanced with the same symbol. All members without such transition are discarded. If a member has two possible destinations, both are added to the new super-state.
You might notice that some rows are very similar. Super-state 3 and 7 have the same transitions. As are 6 and 11, and 8 and 13. You could collapse those into one state each, and update the indices. I'm not going to demonstrate that here.
This could easily be encoded into a programming language:
// index = 0 1 2 3 4 5 6 7 8 9 10 11 12 13
int[] consonant = new int[] { 0, 2, 3, 4, 0, 6, 7, 4, 12, 5, 11, 7, 10, 12 };
int[] vocal = new int[] { 0, 9, 8, 1, 1, 13, 8, 1, 9, 9, 13, 8, 13, 9 };
int[] accept = new int[] { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1 };
int startState = 1;
int failState = 0;
bool CheckWord(string word)
{
int state = startState;
foreach (char c in word)
{
if (IsVocal(c))
{
state = vocal[state];
}
else if (IsConsonant(c))
{
state = consonant[state];
}
if (state == failState) return false;
}
return accept[state] != 0;
}
Example:
> CheckWord("pronunciation")
true
> CheckWord("pronunciationn")
false
Problem
I need to be able to process one word or words and verify that it has valid syllables. There are some syllabification rules that could be used: ``` V CV VC CVC CCV CCCV CVCC ``` where V is a vowel and C is a consonant. e.g., ``` pronunciation (5 Pro-nun-ci-a-tion; CCV-CVC-CV-V-CVC) ``` Or is there a simple code that can be used, or a library in c++? In class we're talking about binary search trees, hash tables, etc, but i can't really see the relation. Any help would appreciated, thanks.