Spintax Spinner
Spin content là một kỹ thuật dùng để tạo ra một nội dung mới từ nội dung gốc dựa trên thuật toán và cú pháp xáo trộn(spin). Mục đích của spin content là tạo ra nhiều nội dung bản sao từ một nội dung gốc, khác nhau ở mặt chữ và thứ tự xuất hiện câu, đoạn văn.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Spintax Spinner</title>
</head>
<body>
<script>
var SPINTAX_PATTERN = /\{[^"\r\n\}]*\}/;
var spin = function (spun) {
var match;
while (match = spun.match(SPINTAX_PATTERN)) {
match = match[0];
var candidates = match.substring(1, match.length - 1).split("|");
spun = spun.replace(match, candidates[Math.floor(Math.random() * candidates.length)])
}
return spun;
}
var sample_str = "{Hey|Hello} {there|mate}, {how are you|what's up}?";
spin(sample_str)
// // var text = "{{Hello|Hi|Hola}, How {have you been|are you doing}? " +
// // "Take care. {{Thanks and|Best} Regards|Cheers|Thanks}";
// var matches, options, random;
// var regEx = new RegExp(/{([^{}]+?)}/);
// while ((matches = regEx.exec(text)) !== null) {
// options = matches[1].split("|");
// random = Math.floor(Math.random() * options.length);
// text = text.replace(matches[0], options[random]);
// }
// console.log(text);
</script>
</body>
</html>