RegEx: Compare two strings to find Alliteration and Assonance

后端 未结 2 711
我寻月下人不归
我寻月下人不归 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:12

    To find alliterations in a text you simply iterate over all words, omitting too short and too common words, and collect them as long as their initial letters match.

    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]
        }
    })
    

    Results:

    ["deep", "ditches", "dark", "dreadful"]
    ["fair", "field", "full", "folk", "found"]
    ["working", "wandering", "world"]
    

    For more advanced parsing and also to find assonances and rhymes you first have to translate a text into phonetic spelling. You didn't say which language you're targeting, for English there are some phonetic dictionaries available online, for example from Carnegie Mellon: ftp://ftp.cs.cmu.edu/project/fgdata/dict

提交回复
热议问题