是否可以通过比较两个字符串来找到头韵和谐音?
我主要使用javascript或php。
发布于 2012-01-26 12:49:33
我不确定正则表达式是否是构建健壮的比较工具的最佳方式。一个简单的正则表达式可能是一个较大的解决方案的一部分,该解决方案使用更复杂的算法进行非精确匹配。
有各种现成的英语选项,其中一些可以相当简单地扩展到使用Latin alphabet的语言。这些算法中的大多数已经存在了几年甚至几十年,而且都有很好的文档记录,尽管它们都有局限性。
我想对于非拉丁字母也有类似的算法,但我不能直接评论它们的可用性。
语音算法
Soundex算法已有近100年的历史,并已在多种编程语言中实现。它用于根据字符串的发音来确定数值。它不是精确的,但它可能有助于识别发音相似的单词/音节。我已经在MS SQL Server中进行了实验,它在PHP中可用。
http://php.net/manual/en/function.soundex.php
普遍的共识(包括PHP文档)是,在处理英语时,Metaphone比Soundex准确得多。有许多可用的实现(Wikipedia在本文末尾有一个很长的列表),它包含在PHP中。
http://www.php.net/manual/en/function.metaphone.php
Double Metahpone支持对应于单词的另一种发音的单词的第二种编码。
与变音位一样,双变音位已经在许多编程语言(example)中实现。
Word解构
Levenshtein可以用来建议替代拼写(例如,规范化用户输入),并且可以作为更细粒度的头韵和同音算法的一部分。
http://www.php.net/manual/en/function.levenshtein.php
从逻辑上讲,这将有助于理解字符串中单词的音节,以便每个单词都可以解构。音节中断可以解决两个相邻字母应该如何发音的歧义。这个线程有几个链接:
发布于 2012-01-26 16:46:18
要查找文本中的所有单词,只需迭代所有单词,省略太短和太常见的单词,并在它们的首字母匹配的情况下收集它们。
text = ''
+'\nAs I looked to the east right into the sun,'
+'\nI saw a tower on a toft worthily built;'
+'\nA deep dale beneath a dungeon therein,'
+'\nWith deep ditches and dark and dreadful of sight'
+'\nA fair field full of folk found I in between,'
+'\nOf all manner of men the rich and the poor,'
+'\nWorking and wandering as the world asketh.'
skipWords = ['the', 'and']
curr = []
text.toLowerCase().replace(/\b\w{3,}\b/g, function(word) {
if (skipWords.indexOf(word) >= 0)
return;
var len = curr.length
if (!len || curr[len - 1].charAt(0) == word.charAt(0))
curr.push(word)
else {
if (len > 2)
console.log(curr)
curr = [word]
}
})结果:
["deep", "ditches", "dark", "dreadful"]
["fair", "field", "full", "folk", "found"]
["working", "wandering", "world"]要进行更高级的解析,也要找到谐音和押韵,您首先必须将文本转换为语音拼写。你没有说你的目标是哪种语言,英语有一些在线语音词典,例如来自Carnegie Mellon的:ftp://ftp.cs.cmu.edu/project/fgdata/dict
https://stackoverflow.com/questions/9014045
复制相似问题