var fs = require("fs");
var data = fs.readFileSync('/System/Volumes/Data/home/2544842260/Public/100again.txt');
var arr = data.toString().split("\n");
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<speciesNumber; s++) {
var rowKey=s+1
processlist["species"+rowKey]=arr[s].split(";")
}
//counting words
var layer=0
do{
var counttmp=[]
var wordListtmp=[]
for (var s=0; s<speciesNumber; s++){
var rowKey=s+1
var existchecker="nonexist"
for (i=0;i<wordListtmp.length;i++){
console.log("s "+s)
console.log("rowkey "+rowKey)
console.log("remain "+countNumber)
console.log("species "+rowKey)
console.log("layer "+layer)
if (processlist["species"+rowKey][layer]===wordListtmp[i]){
existchecker="exist"
var posilog=i
}
}
if (existchecker=="exist"){
counttmp[posilog]=counttmp[posilog]+1
}
else{
wordListtmp.push(processlist["species"+rowKey][layer])
counttmp.push(1)
}
}
//get most counted word
var countNumber=0
for (var j=0;j<wordListtmp.length;j++){
if (countNumber<counttmp[j]){
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<speciesNumber; s++){
var rowKey=s+1
if (mostCountedWord!==processlist["species"+rowKey][layer]){
savelisttmp.push("species"+rowKey)
delete processlist["species"+rowKey]
}
}
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("processlist length:")
console.log(Object.keys(processlist).length)
console.log("wordlist:")
console.log(wordList)
console.log("countlist:")
console.log(countList)
console.log("times:")
console.log(countNumber)
console.log("most counted word:")
console.log(mostCountedWord)
console.log(" ")
console.log("savelist:")
console.log(savelist)
console.log(" ")
}
while (countNumber>(speciesNumber/8))