Normalmente este tipo de problemas se resuelve usando Tries. Voy a basar mi implementación de un Trie en How to create a trie in c# (pero tenga en cuenta que lo he reescrito).
var trie = new Trie(new[] { "un", "que", "stio", "na", "ble", "qu", "es", "ti", "onable", "o", "nable" });
//var trie = new Trie(new[] { "u", "n", "q", "u", "e", "s", "t", "i", "o", "n", "a", "b", "l", "e", "un", "qu", "es", "ti", "on", "ab", "le", "nq", "ue", "st", "io", "na", "bl", "unq", "ues", "tio", "nab", "nqu", "est", "ion", "abl", "que", "stio", "nab" });
var word = "unquestionable";
var parts = new List<List<string>>();
Split(word, 0, trie, trie.Root, new List<string>(), parts);
//
public static void Split(string word, int index, Trie trie, TrieNode node, List<string> currentParts, List<List<string>> parts)
{
// Found a syllable. We have to split: one way we take that syllable and continue from it (and it's done in this if).
// Another way we ignore this possible syllable and we continue searching for a longer word (done after the if)
if (node.IsTerminal)
{
// Add the syllable to the current list of syllables
currentParts.Add(node.Word);
// "covered" the word with syllables
if (index == word.Length)
{
// Here we make a copy of the parts of the word. This because the currentParts list is a "working" list and is modified every time.
parts.Add(new List<string>(currentParts));
}
else
{
// There are remaining letters in the word. We restart the scan for more syllables, restarting from the root.
Split(word, index, trie, trie.Root, currentParts, parts);
}
// Remove the syllable from the current list of syllables
currentParts.RemoveAt(currentParts.Count - 1);
}
// We have covered all the word with letters. No more work to do in this subiteration
if (index == word.Length)
{
return;
}
// Here we try to find the edge corresponding to the current character
TrieNode nextNode;
if (!node.Edges.TryGetValue(word[index], out nextNode))
{
return;
}
Split(word, index + 1, trie, nextNode, currentParts, parts);
}
public class Trie
{
public readonly TrieNode Root = new TrieNode();
public Trie()
{
}
public Trie(IEnumerable<string> words)
{
this.AddRange(words);
}
public void Add(string word)
{
var currentNode = this.Root;
foreach (char ch in word)
{
TrieNode nextNode;
if (!currentNode.Edges.TryGetValue(ch, out nextNode))
{
nextNode = new TrieNode();
currentNode.Edges[ch] = nextNode;
}
currentNode = nextNode;
}
currentNode.Word = word;
}
public void AddRange(IEnumerable<string> words)
{
foreach (var word in words)
{
this.Add(word);
}
}
}
public class TrieNode
{
public readonly Dictionary<char, TrieNode> Edges = new Dictionary<char, TrieNode>();
public string Word { get; set; }
public bool IsTerminal
{
get
{
return this.Word != null;
}
}
}
word
es la cadena le interesa, parts
contendrá la lista de listas de posibles sílabas (que probablemente sería más correcto para que sea un List<string[]>
, pero es bastante fácil de hacerlo. En lugar de parts.Add(new List<string>(currentParts));
parts.Add(currentParts.ToArray());
escribir y cambiar todo el List<List<string>>
-List<string[]>
.
voy a añadir una variante de la respuesta Enigmativity thas es más rápido que su theretically porque descarta sílabas equivocadas inmediatamente en lugar de post-filtrado de ellos más tarde. Si te gusta, usted debe dale un +1, porque sin su idea, este varian No sería posible. Pero tenga en cuenta que todavía es un truco. La solución "correcta" es en el uso de Trie (s) :-)
Func<string, bool> isSyllable = t => Regex.IsMatch(t, "^(un|que|stio|na|ble|qu|es|ti|onable|o|nable)$");
Func<string, IEnumerable<string[]>> splitter = null;
splitter =
t =>
(
from n in Enumerable.Range(1, t.Length - 1)
let s = t.Substring(0, n)
where isSyllable(s)
let e = t.Substring(n)
let f = splitter(e)
from g in f
select (new[] { s }).Concat(g).ToArray()
)
.Concat(isSyllable(t) ? new[] { new string[] { t } } : new string[0][]);
var parts = splitter(word).ToList();
Una explicación:
from n in Enumerable.Range(1, t.Length - 1)
let s = t.Substring(0, n)
where isSyllable(s)
Calculamos todas las posibles sílabas de una palabra, de longitud 1 a la longitud de la palabra - 1 y verifica si es una sílaba. Eliminamos directamente las no sílabas. La palabra completa como sílaba se verificará más tarde.
let e = t.Substring(n)
let f = splitter(e)
Buscamos las sílabas de la parte restante de la cadena
from g in f
select (new[] { s }).Concat(g).ToArray()
Y encadenar los sílabas que se encuentran con la sílaba "actual". Tenga en cuenta que estamos creando muchas matrices inútiles. Si aceptamos tener un IEnumerable<IEnumerable<string>>
como resultado, podemos llevar este ToArray
.
(podríamos reescribir muchas filas juntas, eliminando muchos let
, como
from g in splitter(t.Substring(n))
select (new[] { s }).Concat(g).ToArray()
pero no lo hará por claridad)
Y concatenar la sílaba "actual" con las sílabas que se encuentran .
.Concat(isSyllable(t) ? new[] { new string[] { t } } : new string[0][]);
Aquí podríamos reconstruir la consulta un poco para que no utilice este Concat
y crear matrices vacías, pero sería un poco más complejo (podríamos reescribir toda la función lambda como isSyllable(t) ? new[] { new string[] { t } }.Concat(oldLambdaFunction) : oldLambdaFunction
)
En al final, si la palabra completa es una sílaba, agregamos la palabra completa como sílaba.De lo contrario, Concat
una matriz vacía (lo que no Concat
)
Deberías usar Trie para catalogar tus sílabas. O puede usar las soluciones naiver :-) – xanatos