通常这种类型的问题是使用Tries解决。我将基于我的实施Trie How to create a trie in c#(但请注意,我已将其重写)。
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响应的变体全髋关节置换是theretically快于他的,因为它摒弃错误的音节立即代替滤波后后他们。如果你喜欢它,你应该给他+1,因为没有他的想法,这种差异t是不可能的。但请注意,它仍然是一个黑客。 “正确”的解决方案是使用特里(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
)
您应该使用Trie为您的音节编目。或者你可以使用naiver解决方案:-) – xanatos