通常使用Tries 解决此类问题。我将基于How to create a trie in c# 实现 Trie(但请注意我已经重写了它)。
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 是您感兴趣的字符串,parts 将包含可能音节的列表(将其设为List<string[]> 可能更正确,但它很容易做到。相反的parts.Add(new List<string>(currentParts)); 写入parts.Add(currentParts.ToArray()); 并将所有List<List<string>> 更改为List<string[]>。
我将添加一个比他更快的 Enigmativity 响应变体,因为它会立即丢弃错误的音节,而不是稍后对其进行后过滤。如果你喜欢它,你应该给他+1,因为没有他的想法,这个变种是不可能的。但请注意,它仍然是一个 hack。 “正确”的解决方案是使用 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();
解释:
from n in Enumerable.Range(1, t.Length - 1)
let s = t.Substring(0, n)
where isSyllable(s)
我们计算一个单词的所有可能音节,从长度 1 到单词的长度 - 1 并检查它是否是一个音节。我们直接剔除非音节。后面会检查作为音节的完整单词。
let e = t.Substring(n)
let f = splitter(e)
我们搜索字符串剩余部分的音节
from g in f
select (new[] { s }).Concat(g).ToArray()
我们将找到的音节与“当前”音节链接起来。请注意,我们正在创建许多无用的数组。如果我们接受 IEnumerable<IEnumerable<string>> 作为结果,我们可以删除这个 ToArray。
(我们可以一起重写很多行,删除很多let,比如
from g in splitter(t.Substring(n))
select (new[] { s }).Concat(g).ToArray()
但为了清楚起见,我们不会这样做)
我们将“当前”音节与找到的音节连接起来。
.Concat(isSyllable(t) ? new[] { new string[] { t } } : new string[0][]);
在这里,我们可以稍微重建查询,以便不使用这个 Concat 并创建空数组,但这会有点复杂(我们可以将整个 lambda 函数重写为 isSyllable(t) ? new[] { new string[] { t } }.Concat(oldLambdaFunction) : oldLambdaFunction)
最后,如果整个单词是一个音节,我们将整个单词添加为一个音节。否则我们Concat一个空数组(所以没有Concat)