2017年5月24日水曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)の Part 1(Starting with the basics)、Chapter 11(Case Study: Data Structure Selection)の Word Frequency Analysis、Exercise: 1、2、3、4.を JavaScript で取り組んでみる。

Exercise: 1、2、3、4.

コード(Emacs)

HTML5

<pre id="output0"></pre>
<input id="file0" type="file">
<input id="file1" type="file">

<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample1.js"></script>

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    input_file0 = document.querySelector('#file0'),
    input_file1 = document.querySelector('#file1'),
    p = (x) => pre0.textContent += x + '\n',
    range = (start, end, step=1) => {
        let result = [];

        for (let i = start; i < end; i += 1) {
            result.push(i);
        }
        return result;
    };


let reader0 = new FileReader(),
    reader1 = new FileReader(),
    total,
    words = {},
    punctuation = /[-!"#$%&'()*+,./:;<=>?@\[\\\]^_`{|}~”“’]/g,
    wordsObj = {};

reader0.onload = () => {
    total = 0;
    reader0.result
        .split('\n')
        .forEach((line) => {
            line = line.replace(punctuation, '').trim();
            line.split(/\s+/)
                .filter((word) => word !== '')
                .forEach((word) => {
                    word = word.toLowerCase();
                    if (words[word]) {
                        words[word] += 1;
                    } else {
                        words[word] = 1;
                    }
                    total += 1;
                })
        });
    p('1, 2');
    p(Object.keys(words).length);
    p(total);
    p(Object.values(words).reduce((x, y) => x + y, 0));

    p('3.');
    let sorted = Object.keys(words)
        .sort((a, b) => words[b] - words[a]);
    sorted.slice(0, 20).forEach((word) => {
        p(`${word}: ${words[word]}`);
    });
    output();
};

reader1.onload = () => {
    reader1.result
        .split('\n')
        .map((word) => word.trim())
        .filter((word) => word !== '')
        .forEach((word) => {
            wordsObj[word] = 1;
        });
    output();
};

input_file0.onchange = () => {
    reader0.readAsText(input_file0.files[0]);
    // output();
};
input_file1.onchange = () => {
    reader1.readAsText(input_file1.files[0]);
    output();
};

let output = () => {
    if (Object.keys(words).length !== 0 &&
        Object.keys(wordsObj).length !== 0) {
        p('4.');
        Object.keys(words)
            .forEach((word) => {
                if (!wordsObj[word]) {
                    p(word);
                }
            });
    }
};

let clear = () => pre0.textContent = '';

btn0.onclick = output;
btn1.onclick = clear;

output();
















						

0 コメント:

コメントを投稿