RegEx: Compare two strings to find Alliteration and Assonance

后端 未结 2 685
我寻月下人不归
我寻月下人不归 2021-01-12 14:15

would be possible to Compare two strings to find Alliteration and Assonance?

i use mainly javascript or php

2条回答
  •  执笔经年
    2021-01-12 15:14

    I'm not sure that a regex would be the best way of building a robust comparison tool. A simple regex might be part of a larger solution that used more sophisticated algorithms for non-exact matching.

    There are a variety of readily-available options for English, some of which could be extended fairly simply to languages that use the Latin alphabet. Most of these algorithms have been around for years or even decades and are well-documented, though they all have limits.

    I imagine that there are similar algorithms for non-Latin alphabets but I can't comment on their availability firsthand.

    Phonetic Algorithms

    The Soundex algorithm is nearly 100 years old and has been implemented in multiple programming languages. It is used to determine a numeric value based on the pronunciation of a string. It is not precise but it may be useful for identifying similar sounding words/syllables. I've experimented with it in MS SQL Server and it is available in PHP.

    http://php.net/manual/en/function.soundex.php

    General consensus (including the PHP docs) is that Metaphone is much more accurate than Soundex when dealing with the English language. There are numerous implementations available (Wikipedia has a long list at the end of the article) and it is included in PHP.

    http://www.php.net/manual/en/function.metaphone.php

    Double Metahpone supports a second encoding of a word corresponding to an alternate pronunciation of the word.

    As with Metaphone, Double Metaphone has been implemented in many programming languages (example).

    Word Deconstruction

    Levenshtein can be used to suggest alternate spellings (for example, to normalize user input) and might be useful as part of a more granular algorithm for alliteration and assonance.

    http://www.php.net/manual/en/function.levenshtein.php

    Logically, it would help to understand the syllabication of the words in the string so that each word could be deconstructed. The syllable break could resolve ambiguity as to how two adjacent letters should be pronounced. This thread has a few links:

    PHP Syllable Detection

提交回复
热议问题