衡量一个单词的发音能力? [英] Measure the pronounceability of a word?

查看:92
本文介绍了衡量一个单词的发音能力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修补域名查找器,并希望使用那些易于发音的单词.

I'm tinkering with a domain name finder and want to favour those words which are easy to pronounce.

例如:nameoic.com(不好)与namelet.com(很好).

Example: nameoic.com (bad) versus namelet.com (good).

认为与soundex有关可能比较合适,但是我似乎无法使用它们来产生某种比较得分.

Was thinking something to do with soundex may be appropriate but it doesn't look like I can use them to produce some sort of comparative score.

获胜的PHP代码.

推荐答案

这是一个应使用最常用的单词的函数...它应该给您一个介于1之间的良好结果(根据规则具有完美的可发音性)到0.

Here is a function which should work with the most common of words... It should give you a nice result between 1 (perfect pronounceability according to the rules) to 0.

以下功能远非完美(它不太像海啸[0.857]这样的词).但是,根据您的需求进行调整应该相当容易.

The following function far from perfect (it doesn't quite like words like Tsunami [0.857]). But it should be fairly easy to tweak for your needs.

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

这篇关于衡量一个单词的发音能力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆