5

ランダムな 5 文字の組み合わせの発音可能性を判断できるアルゴリズムを見つける/作成するのに苦労しています。

これまでに見つけた最も近いものは、この 3 年前の StackOverflow スレッドからのものです。

単語の発音可能性を測定しますか?

<?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 以上)。

  • ズテダ
  • LLFDA
  • MMGDA
  • THHDA
  • RTHDA
  • XYHDA
  • VQIDA

私よりも賢い誰かが、おそらく次のようにこのアルゴリズムを微調整できますか?

  • 「MM」、「LL」、および「TH」は、母音が前後にある場合にのみ有効ですか?
  • 連続する 3 つ以上の子音は no-no です (最初または最後が 'R' または 'L' の場合を除く)。
  • あなたが考えることができる他の改良点...

(私はかなりの量の調査/グーグルを行いましたが、これは過去 3 年間、誰もが参照/使用している主要な発音可能関数のようです。私だけでなく、より広いコミュニティで!)。

4

3 に答える 3

8

「手紙にマルコフモデルを使用する」へのリンクされた質問の提案に基づいて

マルコフ モデルを使用します (もちろん、単語ではなく文字に対して)。単語の確率は、発音のしやすさを表すかなり良い指標です。

私はそれを試してみようと思い、いくつかの成功を収めました。

私の方法論

実際の 5 文字の単語のリストをファイルにコピーして、データセットとして使用しました (ここでは...うーん、実際にはここ)。

次に、隠れマルコフ モデル (1 グラム、2 グラム、3 グラムに基づく) を使用して、対象の単語がそのデータセットに出現する可能性を予測します。

(手順の 1 つとして、ある種の音声表記を使用すると、より良い結果が得られる可能性があります。)

まず、データセット内の文字シーケンスの確率を計算します。

たとえば、「A」が 50 回出現し、データセットに 250 文字しかない場合、「A」の確率は 50/250 または .2 です。

バイグラム「AB」、「AC」、...についても同じことを行います。

トリグラム 'ABC'、'ABD'、... についても同じことを行います。

基本的に、「ABCDE」という単語のスコアは次の要素で構成されています。

  • prob( 'A' )
  • prob( 'B' )
  • prob( 'C' )
  • prob( 'D' )
  • 調査' )
  • prob( 'AB' )
  • prob( 'BC' )
  • prob( 'CD' )
  • prob( 'DE' )
  • prob( 'ABC' )
  • prob( 'BCD' )
  • prob( 'CDE' )

これらすべてを掛け合わせて、ターゲット単語がデータセットに出現する確率を推定できます (ただし、これは非常に小さいです)。

代わりに、それぞれのログを取得して、それらを合計します。

これで、ターゲット単語がデータセットに出現する可能性を推定するスコアが得られました。

私のコード

私はこれを C# としてコーディングしましたが、マイナス 160 より大きいスコアはかなり良いことがわかりました。

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;
    }
}

}

私の例では、トライグラムの確率を 5 でスケーリングします。

また、すべてのカウントに 1 を加算するので、0 を掛けません。

最終的な注意事項

私は PHP プログラマーではありませんが、この手法は非常に簡単に実装できます。

いくつかの倍率を試したり、別のデータセットを試したり、上記で提案したような他のチェックを追加したりしてください。

于 2012-08-09T07:04:14.247 に答える
2

最初から合理的に発音可能な組み合わせを生成するのはどうですか? 私は、ランダムなSoundexコードを生成し、そこから (通常は) 発音可能なオリジナルに戻す作業を行いました。

于 2012-08-09T12:18:04.987 に答える
0

誰かがノードでこれを行う方法を探している場合、Xantixの回答が説明するものを実装しているように見えるpronouncableと呼ばれるモジュールを見つけました。

npm i pronounceable

RunKit に何もインストールせずにテストできます。

于 2022-02-26T16:04:58.397 に答える