首先幾個語法問題:
- 變化lexicon.alliteration到lexicon.alliteration 小號
- 線
console.log(nn)
錯誤,因爲nn是一個不存在的變量,所以刪除這一行。
的頭韻方法被調用與POS [I]作爲參數,而此時POS [I]具有「NN」的值,這是一個「部分的發聲」標籤由pos方法返回。它不能返回任何整體,因爲整體方法實際上需要一個單詞而不是一個詞性標籤。 所以你應該把它傳遞字[I],而不是POS [I]
但是它返回一個數組,所以你最終將有很多的話這樣的:
It was a dark and stormy abnormal,abnormally,abomination,acknowledge,acknowledged,acknowledgement ...
所以,如果你想代替你可以將其更改爲隨機頭韻:
var alliterations = lexicon.alliterations(words[i]);
output += alliterations[Math.floor(Math.random() * alliterations.length)];
所以完整的代碼變成:
var input;
var button;
var lexicon;
function setup() {
noCanvas();
lexicon = new RiLexicon();
input = createInput('It was a dark and stormy night.');
button = createButton('submit');
input.changed(processRita);
button.mousePressed(processRita);
input.size(200);
}
function processRita() {
var s = input.value();
var rs = new RiString(s);
var words = rs.words();
var pos = rs.pos();
console.log(words);
console.log(pos);
var output = '';
for (var i = 0; i < words.length; i++) {
if (/nn.*/.test(pos[i])) {
var alliterations = lexicon.alliterations(words[i]);
output += alliterations[Math.floor(Math.random() * alliterations.length)];
} else {
output += words[i];
}
output += " ";
}
createP(output);
}
不幸的是,RiTa的頭韻定義(單詞的第一個重音輔音匹配)並不是你所追求的。
假設你是不是有相同的字母,但單詞具有相同音素開始啓動的話後,簡單地說,我找不到 使用麗塔的API做的一個有效的方法,雖然我寫了下面的功能這似乎在做這項工作;但它遵循不好的做法,因爲它訪問了RiTa的一些不打算訪問的「私人」成員。爲了參考,我使用RiTa 1.1.40。
function firstPhoneAlliteration(searchWord) {
var phoneSplitRegex = new RegExp(' |-|' + RiTa.STRESSED); //space or hyphen or the stress character
function getFirstPhone(word) {
return lexicon._getRawPhones(word).split(phoneSplitRegex)[0];
}
var firstPhoneOfSearchWord = getFirstPhone(searchWord);
var wordsInLexicon = lexicon.keys;
var matchedWords = [];
for (let i = 0; i < wordsInLexicon.length; i++) {
if (firstPhoneOfSearchWord === getFirstPhone(wordsInLexicon[i])) {
matchedWords.push(wordsInLexicon[i]);
}
}
return matchedWords;
}
你可能只需要一個部分的語音匹配的標籤的話(從而例如「空中飛人」將與名詞來代替),所以你 可以過濾的話如下:
var alliterations = firstPhoneAlliteration(words[i])
.filter(function (word) {
return (RiTa.getPosTags(word)[0] === pos[i]);
});
來源
2016-11-15 21:44:22
Jon
快速瀏覽一下參考文獻[https://rednoise.org/rita/reference/index.php]可以看出,RiLexicon有一個方法alliteration ** s **而不是一致性,所以可能只需要更改方法名稱。 – Jon
我已經嘗試了兩種方法,以及「isAlliteration」標籤,但它們都不起作用。 –