Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Output() {
- this.init = function() {
- $('body').append('<div id="result-div"><table id="result-table"></table></div>');
- $('#result-table').html('');
- $('#result-div').css('position','fixed').css('left','0').css('top','0').css('background','white').css('overflow','scroll').css('max-height','100%');
- }
- this.add = function(ja,en) {
- $('#result-table').append('<tr><td>'+ja+'</td><td>'+en+'</td></tr>');
- }
- }
- function Dictionary() {
- this.dict = [];
- this.ranges = {hiragana:[0x3040,0x309f],katakana:[0x30a0,0x30ff],kanji:[0x4e00,0x9faf]};
- this.statKnown = function(text) {
- var known = 0;
- var unknown = 0;
- for (var i=0;i<text.length;i++) {
- var c = text.charCodeAt(i);
- // count kanji characters only
- if (c >= this.ranges['kanji'][0] && c <= this.ranges['kanji'][1]) {
- if (c in this.dict) {
- known++;
- } else {
- unknown++;
- }
- }
- }
- return [known,unknown];
- }
- this._textToDict = function(text) {
- var dict = [];
- for (var i=0;i<text.length;i++) {
- dict[text.charCodeAt(i)] = 1;
- }
- return dict;
- }
- this.init = function(kanji) {
- this.dict = this._textToDict(kanji);
- }
- }
- function Parser() {
- this.sentences = function(data,dictionary,output) {
- var ja = $(data).find('td.japanese');
- var en = $(data).find('td.english');
- for (var i=0;i<ja.length;i++) {
- var ja_ = $(ja[i]).text();
- var en_ = $(en[i]).text();
- var stat = dictionary.statKnown(ja_);
- //console.log(stat);
- if (stat[1] < 3) {
- output.add(ja_,en_);
- }
- }
- }
- this.pages = function(data,pageLimit) {
- var pages=[];
- $(data).find('a').each(function(i,item){
- //console.log($(item).attr('class'))
- if ($(item).attr('href')) {
- var href = $(item).attr('href');
- var matched = href.match(/page=([0-9]*)$/);
- if (matched) {
- pages[matched[1]] = true;
- }
- }
- });
- var pages_ = [];
- var maxPage = Math.min(pages.length,pageLimit+1);
- for (var i=2;i<maxPage;i++) {
- pages_.push(i);
- }
- return pages_;
- }
- }
- function Jobs(dictionary,words,parser,output,pageLimit) {
- this.dictionary = dictionary;
- this.words = words;
- this.pages = [];
- this.word = '';
- this.pageLimit = pageLimit;
- this.parser = parser;
- this.output = output;
- this._fetch = function(word,page) {
- var url = '';
- console.log('fetching word ' + word + '; page ' + page);
- if (page == 1) {
- url = '/sentences?jap=' + word;
- } else {
- url = '/sentences?jap=' + word + '&page=' + page;
- }
- var this_ = this;
- $.ajax({
- url: url,
- success: function(data) {
- parser.sentences(data,dictionary,output);
- if (page == 1) {
- this_.pages = parser.pages(data,pageLimit);
- }
- }
- });
- }
- this.run = function() {
- if (this.words.length == 0 && this.pages.length == 0) {
- console.log('all job is done');
- return ;
- }
- if (this.pages.length > 0) {
- var page = this.pages.shift();
- //console.log('schedule page ' + page);
- this._fetch(this.word,page);
- } else if (this.words.length>0) {
- this.word = this.words.shift();
- //console.log('schedule word ' + this.word);
- this._fetch(this.word,1);
- }
- var this_ = this;
- setTimeout(function(){this_.run()},2000);
- }
- }
- var kanji = '一二三四五六七八九十百千上下左右中大小月日年早木林山川土空田天生花草虫犬人名女男子目耳口手足見音力気円入出立休先夕本文字学校村町森正水火玉王石竹糸貝車金雨赤青白数多少万半形太細広長点丸交光角計直線矢弱強高同親母父姉兄弟妹自友体毛頭顔首心時曜朝昼夜分週春夏秋冬今新古間方北南東西遠近前後内外場地国園谷野原里市京風雪雲池海岩星室戸家寺通門道話言答声聞語読書記紙画絵図工教晴思考知才理算作元食肉馬牛魚鳥羽鳴麦米茶色黄黒来行帰歩走止活店買売午汽弓回会組船明社切電毎合当台楽公引科歌刀番用何丁世両主乗予事仕他代住使係倍全具写列助勉動勝化区医去反取受号向君味命和品員商問坂央始委守安定実客宮宿寒対局屋岸島州帳平幸度庫庭式役待急息悪悲想意感所打投拾持指放整旅族昔昭暑暗曲有服期板柱根植業様横橋次歯死氷決油波注泳洋流消深温港湖湯漢炭物球由申界畑病発登皮皿相県真着短研礼神祭福秒究章童笛第筆等箱級終緑練羊美習者育苦荷落葉薬血表詩調談豆負起路身転軽農返追送速進遊運部都配酒重鉄銀開院陽階集面題飲館駅鼻';
- var dictionary = new Dictionary();
- dictionary.init(kanji);
- var words = ['人間','彼女'];
- var parser = new Parser();
- var output = new Output();
- output.init();
- var pageLimit = 3;
- var jobs = new Jobs(dictionary,words,parser,output,pageLimit);
- jobs.run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement