首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可宣告性算法

可宣告性算法
EN

Stack Overflow用户
提问于 2012-08-08 22:42:22
回答 3查看 3.2K关注 0票数 6

我正在努力寻找/创建一个算法,可以确定随机5个字母组合的可读性。

到目前为止,我发现的最接近的东西是来自这个3岁的StackOverflow线程:

衡量一个单词的可宣布性?

代码语言:javascript
复制
<?php
// Score: 1
echo pronounceability('namelet') . "\n";

// Score: 0.71428571428571
echo pronounceability('nameoic') . "\n";

function pronounceability($word) {
    static $vowels = array
        (
        'a',
        'e',
        'i',
        'o',
        'u',
        'y'
        );

    static $composites = array
        (
        'mm',
        'll',
        'th',
        'ing'
        );

    if (!is_string($word)) return false;

    // Remove non letters and put in lowercase
    $word = preg_replace('/[^a-z]/i', '', $word);
    $word = strtolower($word);

    // Special case
    if ($word == 'a') return 1;

    $len = strlen($word);

    // Let's not parse an empty string
    if ($len == 0) return 0;

    $score = 0;
    $pos = 0;

    while ($pos < $len) {
        // Check if is allowed composites
        foreach ($composites as $comp) {
                $complen = strlen($comp);

                if (($pos + $complen) < $len) {
                        $check = substr($word, $pos, $complen);

                        if ($check == $comp) {
                                $score += $complen;
                                $pos += $complen;
                                continue 2;
                        }
                }
        }

        // Is it a vowel? If so, check if previous wasn't a vowel too.
        if (in_array($word[$pos], $vowels)) {
                if (($pos - 1) >= 0 && !in_array($word[$pos - 1], $vowels)) {
                        $score += 1;
                        $pos += 1;
                        continue;
                }
        } else { // Not a vowel, check if next one is, or if is end of word
                if (($pos + 1) < $len && in_array($word[$pos + 1], $vowels)) {
                        $score += 2;
                        $pos += 2;
                        continue;
                } elseif (($pos + 1) == $len) {
                        $score += 1;
                        break;
                }
        }

        $pos += 1;
    }

    return $score / $len;
}
?>

..。但这远远不是完美无缺的,给出了一些相当奇怪的假阳性:

使用此函数,下列所有可声明的比率,(7/10以上)

  • ZTEDA
  • LLFDA
  • MMGDA
  • THHDA
  • RTHDA
  • XYHDA
  • VQIDA

比我聪明的人能不能用这个算法,以便:

  • ‘'MM','LL’和'TH‘只有在后面或前面加上元音时才有效?
  • 一行中的三个或多个辅音是不-否(除非第一个或最后一个是'R‘或'L')。
  • 你能想到的任何其他细节..。

(我做了相当多的研究/谷歌搜索,这似乎是过去3年里每个人都在引用/使用的主要声明功能,所以我相信一个更新的、更精致的版本会受到更广泛的社区的欢迎,而不仅仅是我!)

EN

回答 3

Stack Overflow用户

发布于 2012-08-09 07:04:14

基于关于“使用字母马尔可夫模型”的链接问题的建议

使用马尔可夫模型(当然是字母,而不是单词)。一个单词的概率可以很好地反映发音的易用性。

我想我应该试一试,并取得了一些成功。

我的方法

我将一个真实的5个字母单词的列表复制到一个文件中,作为我的数据集(这里...um,实际上是这里)。

然后,我使用隐马尔可夫模型(基于1克、双克和三克)来预测目标词在该数据集中出现的可能性。

(以某种语音转录作为步骤之一可以获得更好的效果。)

首先,计算数据集中字符序列的概率。

例如,如果'A‘出现50次,而且数据集中只有250个字符,那么'A’有50/250个或.2概率。

对字母'AB','AC‘做同样的处理,.

对“ABC”,“ABD”,.

基本上,我对"ABCDE“这个词的评分包括:

  • 序言( 'A‘)
  • prob( 'B‘)
  • prob( 'C‘)
  • prob( 'D‘)
  • prob( 'E‘)
  • prob( 'AB‘)
  • prob(“BC”)
  • prob(“CD”)
  • prob(“DE”)
  • prob( 'ABC‘)
  • prob( 'BCD‘)
  • prob( 'CDE‘)

您可以将所有这些数据相乘,以获得目标单词出现在数据集中的估计概率(但这非常小)。

因此,我们取每一个日志,并将它们相加在一起。

现在我们有了一个分数,它估计我们的目标单词在数据集中出现的可能性。

我的代码

我编码了这是C#,发现大于负160的分数是很好的。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Pronouncability
{

class Program
{
    public static char[] alphabet = new char[]{ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };

    public static List<string> wordList = loadWordList(); //Dataset of 5-letter words

    public static Random rand = new Random();

    public const double SCORE_LIMIT = -160.00;

    /// <summary>
    /// Generates random words, until 100 of them are better than
    /// the SCORE_LIMIT based on a statistical score. 
    /// </summary>
    public static void Main(string[] args)
    {
        Dictionary<Tuple<char, char, char>, int> trigramCounts = new Dictionary<Tuple<char, char, char>, int>();

        Dictionary<Tuple<char, char>, int> bigramCounts = new Dictionary<Tuple<char, char>, int>();

        Dictionary<char, int> onegramCounts = new Dictionary<char, int>();

        calculateProbabilities(onegramCounts, bigramCounts, trigramCounts);

        double totalTrigrams = (double)trigramCounts.Values.Sum();
        double totalBigrams = (double)bigramCounts.Values.Sum();
        double totalOnegrams = (double)onegramCounts.Values.Sum();

        SortedList<double, string> randomWordsScores = new SortedList<double, string>();

        while( randomWordsScores.Count < 100 )
        {
            string randStr = getRandomWord();

            if (!randomWordsScores.ContainsValue(randStr))
            {
                double score = getLikelyhood(randStr,trigramCounts, bigramCounts, onegramCounts, totalTrigrams, totalBigrams, totalOnegrams);

                if (score > SCORE_LIMIT)
                {
                    randomWordsScores.Add(score, randStr);
                }
            }
        }


        //Right now randomWordsScores contains 100 random words which have 
        //a better score than the SCORE_LIMIT, sorted from worst to best.
    }


    /// <summary>
    /// Generates a random 5-letter word
    /// </summary>
    public static string getRandomWord()
    {
        char c0 = (char)rand.Next(65, 90);
        char c1 = (char)rand.Next(65, 90);
        char c2 = (char)rand.Next(65, 90);
        char c3 = (char)rand.Next(65, 90);
        char c4 = (char)rand.Next(65, 90);

        return "" + c0 + c1 + c2 + c3 + c4;
    }

    /// <summary>
    /// Returns a score for how likely a given word is, based on given trigrams, bigrams, and one-grams
    /// </summary>
    public static double getLikelyhood(string wordToScore, Dictionary<Tuple<char, char,char>, int> trigramCounts, Dictionary<Tuple<char, char>, int> bigramCounts, Dictionary<char, int> onegramCounts, double totalTrigrams, double totalBigrams, double totalOnegrams)
    {
        wordToScore = wordToScore.ToUpper();

        char[] letters = wordToScore.ToCharArray();

        Tuple<char, char>[] bigrams = new Tuple<char, char>[]{ 

            new Tuple<char,char>( wordToScore[0], wordToScore[1] ),
            new Tuple<char,char>( wordToScore[1], wordToScore[2] ),
            new Tuple<char,char>( wordToScore[2], wordToScore[3] ),
            new Tuple<char,char>( wordToScore[3], wordToScore[4] )

        };

        Tuple<char, char, char>[] trigrams = new Tuple<char, char, char>[]{ 

            new Tuple<char,char,char>( wordToScore[0], wordToScore[1], wordToScore[2] ),
            new Tuple<char,char,char>( wordToScore[1], wordToScore[2], wordToScore[3] ),
            new Tuple<char,char,char>( wordToScore[2], wordToScore[3], wordToScore[4] ),


        };

        double score = 0;

        foreach (char c in letters)
        {
            score += Math.Log((((double)onegramCounts[c]) / totalOnegrams));
        }

        foreach (Tuple<char, char> pair in bigrams)
        {
            score += Math.Log((((double)bigramCounts[pair]) / totalBigrams));
        }

        foreach (Tuple<char, char, char> trio in trigrams)
        {
            score += 5.0*Math.Log((((double)trigramCounts[trio]) / totalTrigrams));
        }


        return score;
    }

    /// <summary>
    /// Build the probability tables based on the dataset (WordList)
    /// </summary>
    public static void calculateProbabilities(Dictionary<char, int> onegramCounts, Dictionary<Tuple<char, char>, int> bigramCounts, Dictionary<Tuple<char, char, char>, int> trigramCounts)
    {
        foreach (char c1 in alphabet)
        {
            foreach (char c2 in alphabet)
            {
                foreach( char c3 in alphabet)
                {
                    trigramCounts[new Tuple<char, char, char>(c1, c2, c3)] = 1;
                }
            }
        }

        foreach( char c1 in alphabet)
        {
            foreach( char c2 in alphabet)
            {
                bigramCounts[ new Tuple<char,char>(c1,c2) ] = 1;
            }
        }

        foreach (char c1 in alphabet)
        {
            onegramCounts[c1] = 1;
        }


        foreach (string word in wordList)
        {
            for (int pos = 0; pos < 3; pos++)
            {
                trigramCounts[new Tuple<char, char, char>(word[pos], word[pos + 1], word[pos + 2])]++;
            }

            for (int pos = 0; pos < 4; pos++)
            {
                bigramCounts[new Tuple<char, char>(word[pos], word[pos + 1])]++;
            }

            for (int pos = 0; pos < 5; pos++)
            {
                onegramCounts[word[pos]]++;
            }
        }
    }

    /// <summary>
    /// Get the dataset (WordList) from file.
    /// </summary>
    public static List<string> loadWordList()
    {
        string filePath = "WordList.txt";

        string text = File.ReadAllText(filePath);

        List<string> result = text.Split(' ').ToList();

        return result;
    }
}

}

在我的示例中,我将trigram概率标度为5。

我也把1加到所有的计数上,这样我们就不会乘以0。

最后注记

我不是一个php程序员,但这种技术很容易实现。

使用一些缩放因素,尝试不同的数据集,或者添加一些其他检查,如上面所建议的。

票数 9
EN

Stack Overflow用户

发布于 2012-08-09 12:18:04

从一开始就产生一个合理的组合如何?我已经完成了一些工作,我生成了一个随机的Soundex代码,并从它返回到一个(通常)可发音的原始代码。

票数 2
EN

Stack Overflow用户

发布于 2022-02-26 16:04:58

如果有人在寻找使用Node.js的方法,我发现了一个名为可宣布的模块,它似乎实现了Xantix的答案所描述的功能。

代码语言:javascript
复制
npm i pronounceable

你可以在没有在RunKit上安装任何东西的情况下测试

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11874274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档