#creator of dataset for weka, count occurence in set of one element
import string
import os
import csv 

#open file csv with all malware, save these in a string
with open("C:\Users\Lorenzo\Desktop\drebin\sha256_family.csv", "rb") as csvfile:
	spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
	in_line=""
	for row in spamreader:
		in_line=in_line+ ","+''.join(row)

#open directory where there are the training set files save all the file inside in a list and create a dictionary from this list		
direct= "C:\Users\Lorenzo\Desktop\MachineLearning\datasetMalware2\\"
listM = os.listdir('C:\Users\Lorenzo\Desktop\MachineLearning\datasetMalware2')
dictM=dict.fromkeys(listM) 

#open file arff where write the datasheet for weka
fweka=open("C:\Users\Lorenzo\Desktop\lollo.arff","w")
fweka.write("@relation malware\n")

momstr = ""
i=0
j=0
dictMalFeat={}
dictNonFeat={}

#now for all the malwere saved in svc file say if they are inside or not of the two directory and labeled each malwere with "M"
for indexj in range(0,len(in_line)):
		if in_line[indexj].isalnum():
			g=in_line[indexj]
			momstr=momstr+g
		else:
			if not(momstr==""):
				if dictM.has_key(momstr):
					dict2={momstr: "M"}
					dictM.update(dict2)
					i=i+1
				momstr=""
				
				
print "\nLEARNING PHASE CONCLUSED, FOUNDED ",i, " DANGEROUS FILE ON ",
print len(dictM), "FILE ESAMINATED IN TRAINING SET"

s1=[]	
r=0
#create 8 list not ordered where put all the line that contain one of the key words written over
for indexj in range(0,len(listM)):
	fileName=listM[indexj]
	fileNameTot=direct+fileName
	in_file=open(fileNameTot, "r")

	while True:
		in_line=in_file.readline()
		if in_line=="":
			break
		else:
			if(s1.count(in_line)==0):
				s1.append(in_line)
				fweka.write("\n@attribute s")
				fweka.write(str(r))
				fweka.write("{0,1}")
				r=r+1
			
#order the list whit .sort()
s1.sort()

fweka.write("\n@attribute malware { M , U }\n")
fweka.write("\n@data\n")

#print s1
#using these lists i associate 8 numbers to 1 file, these numbers are composed by the sum of 2^index of the features inside the 8 lists

for indexj in range(0,len(listM)):
	fileName=listM[indexj]
	fileNameTot=direct+fileName
	in_file=open(fileNameTot, "r")
	c="U"
	s2=[]
	if dictM.get(fileName)=="M":
		c="M"

	while True:
		in_line=in_file.readline()
		if in_line=="":
			break
		else:
			if(s1.count(in_line)==1):
				s2.append(s1.index(in_line))
				
			
	for index in range(0,len(s1)):
		if(s2.count(index)==1):
			fweka.write("1,")
		else:
			fweka.write("0,")
	fweka.write(str(c))
	fweka.write("\n")
	
	#print  l
	
	
fweka.close()

#cell of the list if now u print the new list u have a sort of combination of 0101... 
#transate this number in decimal number 