差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン | |||
20220907_invertebrate_grouping_script_3 [2022/09/07 08:20] – 133.11.50.163 | 20220907_invertebrate_grouping_script_3 [Unknown date] (現在) – 削除 - 外部編集 (Unknown date) 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
- | indexof is not a good way | ||
- | ``` | ||
- | var fs = require(" | ||
- | var data = fs.readFileSync('/ | ||
- | var arr = data.toString().split(" | ||
- | // | ||
- | arr.sort(); | ||
- | var tmp = arr; | ||
- | |||
- | var wordcounter = []; | ||
- | var wordlist=[] | ||
- | var savelist=[] | ||
- | |||
- | |||
- | |||
- | //words counting | ||
- | var wordcountertmp=[] | ||
- | var wordlisttmp=[] | ||
- | for (var s = 0; s < tmp.length; s ++ ){ | ||
- | posiOf1stSemicolon=tmp[s].indexOf(";" | ||
- | var wordtmp = tmp[s].slice(0, | ||
- | //check if it is a new word. yes - add the new word. no - plus 1 | ||
- | var existchecker = wordlisttmp.indexOf(wordtmp) | ||
- | if(existchecker==-1){ | ||
- | wordlisttmp.push(wordtmp) | ||
- | wordcountertmp.push(1) | ||
- | } | ||
- | else{ | ||
- | var posi=existchecker; | ||
- | wordcountertmp[posi]=wordcountertmp[posi]+1 | ||
- | } | ||
- | //get the most counted word | ||
- | var maxTmp=0 | ||
- | for (i=0; | ||
- | if (maxTmp< | ||
- | maxTmp=wordcountertmp[i] | ||
- | indexOfMostCountedWordtmp=i | ||
- | } | ||
- | } | ||
- | var mostCountedWordtmp=wordlisttmp[indexOfMostCountedWordtmp] | ||
- | } | ||
- | //save the less counted species, and remove saved/ | ||
- | for(var s = 0; s < tmp.length; s ++){ | ||
- | if (tmp[s].indexOf(mostCountedWordtmp) == -1){ | ||
- | console.log(" | ||
- | console.log(tmp[s]) | ||
- | savelist.push(tmp[s]) | ||
- | tmp.splice(s, | ||
- | } | ||
- | else{ | ||
- | tmp[s]=tmp[s].slice(mostCountedWordtmp.length+1) | ||
- | } | ||
- | } | ||
- | wordcounter.push(wordcountertmp) | ||
- | wordlist.push(wordlisttmp) | ||
- | ``` | ||
- | so! with associate array | ||
- | ``` | ||
- | var fs = require(" | ||
- | var data = fs.readFileSync('/ | ||
- | var arr = data.toString().split(" | ||
- | arr.sort(); | ||
- | var speciesNumber=arr.length | ||
- | var speciesList={} | ||
- | var processlist=speciesList | ||
- | var savelist=[] | ||
- | |||
- | var wordList=[] | ||
- | var countList=[] | ||
- | |||
- | //save the data to array | ||
- | for (var s=0; s< | ||
- | var rowKey=s+1 | ||
- | processlist[" | ||
- | } | ||
- | |||
- | //counting words | ||
- | var layer=0 | ||
- | do{ | ||
- | var counttmp=[] | ||
- | var wordListtmp=[] | ||
- | for (var s=0; s< | ||
- | var rowKey=s+1 | ||
- | var existchecker=" | ||
- | for (i=0; | ||
- | console.log(" | ||
- | console.log(" | ||
- | console.log(" | ||
- | console.log(" | ||
- | console.log(" | ||
- | if (processlist[" | ||
- | existchecker=" | ||
- | var posilog=i | ||
- | } | ||
- | } | ||
- | if (existchecker==" | ||
- | counttmp[posilog]=counttmp[posilog]+1 | ||
- | } | ||
- | else{ | ||
- | wordListtmp.push(processlist[" | ||
- | counttmp.push(1) | ||
- | } | ||
- | } | ||
- | //get most counted word | ||
- | var countNumber=0 | ||
- | for (var j=0; | ||
- | if (countNumber< | ||
- | countNumber=counttmp[j] | ||
- | var indexOfCountNumber=j | ||
- | } | ||
- | var mostCountedWord=wordListtmp[indexOfCountNumber] | ||
- | } | ||
- | //keep the most counted species in processlist; | ||
- | //save other to savelist. | ||
- | savelisttmp=[] | ||
- | for (var s=0; s< | ||
- | var rowKey=s+1 | ||
- | if (mostCountedWord!==processlist[" | ||
- | savelisttmp.push(" | ||
- | delete processlist[" | ||
- | } | ||
- | } | ||
- | if (savelisttmp.length!==0){ | ||
- | savelist.push(savelisttmp) | ||
- | } | ||
- | |||
- | | ||
- | | ||
- | //save less counted words and numbers to list | ||
- | wordList.push(wordListtmp) | ||
- | countList.push(counttmp) | ||
- | layer=layer+1 | ||
- | speciesNumber=countNumber | ||
- | |||
- | |||
- | |||
- | console.log(" | ||
- | console.log(Object.keys(processlist).length) | ||
- | console.log(" | ||
- | console.log(wordList) | ||
- | console.log(" | ||
- | console.log(countList) | ||
- | console.log(" | ||
- | console.log(countNumber) | ||
- | console.log(" | ||
- | console.log(mostCountedWord) | ||
- | console.log(" | ||
- | console.log(" | ||
- | console.log(savelist) | ||
- | console.log(" | ||
- | } | ||
- | while (countNumber> | ||
- | ``` | ||
- | but the loop program is not going well | ||
- | ``` | ||
- | / | ||
- | wordListtmp.push(processlist[" | ||
- | ^ | ||
- | |||
- | TypeError: Cannot read property ' | ||
- | at Object.< | ||
- | at Module._compile (node: | ||
- | at Object.Module._extensions..js (node: | ||
- | at Module.load (node: | ||
- | at Function.Module._load (node: | ||
- | at Function.executeUserEntryPoint [as runMain] (node: | ||
- | at node: | ||
- | pch12005m: | ||
- | ``` | ||
- | |||
- | |||
- | |||
- | |||