teach-matsuuratomoya-com/content/docs/2023/mediaart-programming2/5/text_unicode_shift_grad.js

29 lines
1001 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//ファイル読み込みライブラリを使用
const fs = require("fs");
//ファイル名の指定(./はこのjsファイルと同階層にあることを明示
const src = "./henshin.txt";
//ファイルを読み込み
const bytes = fs.readFileSync(src);
//バイト列から文字列に
const txt = bytes.toString();
//文字の配列にする
const arr = Array.from(txt);
//文字コードをずらす確率
let probability = 0.0;
const shifted = arr.map(c => {
//配列の最初0%-最後:100%になるように確率を変更していく
probability += 1 / arr.length;
//randomは0~1の乱数
if (Math.random() < probability) {
//配列の文字コードを1つ隣へずらす
return String.fromCodePoint(c.codePointAt(0) + 1)
} else {
return c
}
});
//ソートした配列を(区切り文字なしで)結合
const shifted_str = shifted.join("");
//ファイルに保存
fs.writeFileSync("henshin_shifted_grad.txt", shifted_str);