# !/usr/bin/python
# coding: latin-1

#------------------------------------------------------------------------------#
# Artificial Intelligence - Bayes Classification Algorithms                    #
# ============================================================================ #
# Organization: HE-Arc Engineering                                             #
# Developer(s): Etienne Frank                                                  #
#               Johan Chavaillaz                                               #
#                                                                              #
# Filename:     BayesClassification.py                                         #
# Version:      1.0                                                            #
#------------------------------------------------------------------------------#

#------------------------------------------------------------------------------#
#                                                                              #
#                               LIBRARIES IMPORT                               #
#                                                                              #
#------------------------------------------------------------------------------#

import sys
from os import walk
import random

#------------------------------------------------------------------------------#
#                                                                              #
#                                   CLASSES                                    #
#                                                                              #
#------------------------------------------------------------------------------#


class DataFile:
    """ Contains file analysis information """

    def __init__(self, fileLine, isPositive):
        """
        Create a new DataFile
        :param fileLine: Data file content (=message) in one line
        :type fileLine: str
        :param isPositive: True if positive message, False otherwise
        :type isPositive: bool
        :rtype: str
        """
        self.isPositive = isPositive
        self.fileLine = fileLine
        self.wordsCount = {}

        self.words = fileLine.split()

        for word in self.words:
            try:
                self.wordsCount[word] += 1
            except KeyError:
                self.wordsCount[word] = 1

        self.sumWords = sum(self.wordsCount.values())

    def __repr__(self):
        information = "Input file : " + self.fileLine + "\n"
        information += "============" + "\n"

        for key, value in self.wordsCount.items():
            information += str(key) + " " + str(value) + "\n"

        information += "============" + "\n"
        information += "Word count : " + str(self.sumWords) + "\n"

        return information

class DataSet:
    """ Contains all DataFile """

    def __init__(self, dataSetPath):
        """
        :param dataSetPath: Path to data set folder that contains positive and negative folder messages
        :type dataSetPath: str
        :return: object
        """
        self.dataPositive = []
        self.dataNegative = []
        self.dataPath = dataSetPath
        self.positivePath = self.dataPath + "/positive"
        self.negativePath = self.dataPath + "/negative"

        self.load(self.dataPath)

    def load(self, path):

        # print("Directory" + path)
        for (directoryPath, subDirectoryList, fileNameList) in walk(path):

            # Debug
            # print(fileNameList)
            # print(directoryPath)
            # print(subDirectoryList)

            if directoryPath in [self.positivePath, self.negativePath]:
                isPositive = True if directoryPath == self.positivePath else False

                random.shuffle(fileNameList)

                for index, fileName in enumerate(fileNameList):
                    # print(directoryPath + '/' + fileName)
                    fileContent = ''.join(open(directoryPath + '/' + fileName, 'r', encoding="utf-8").readlines())

                    self.dataPositive.append(DataFile(fileContent, isPositive)) if isPositive \
                        else self.dataNegative.append(DataFile(fileContent, isPositive))

            for subDirectory in subDirectoryList:
                self.load(path + "/" + subDirectory)

            break

    def classify(self):

        maxIndexPositive = 0.8 * self.dataPositive.count()
        maxIndexNegative = 0.8 * self.dataNegative.count()

        for data in (self.dataPositive[:maxIndexPositive] + self.dataNegative[:maxIndexNegative]):

            pass



#------------------------------------------------------------------------------#
#                                                                              #
#                             UTILITIES FUNCTIONS                              #
#                                                                              #
#------------------------------------------------------------------------------#


#------------------------------------------------------------------------------#
#                                                                              #
#                               "MAIN" FUNCTION                                #
#                                                                              #
#------------------------------------------------------------------------------#

# If this is the main module, run this
if __name__ == '__main__':
    argsCount = len(sys.argv)
    argsIndex = 1

    random.seed()

    dataSet = DataSet("./data")
    print(dataSet.dataPositive[500])

@@ -4368,24 +4368,463 @@
 def 
-classify(self):%0A
+train(self):%0A        %22%22%22%0A        Training for Bayes algorithm%0A%0A        :return: null%0A        %22%22%22%0A        maxIndexPositive = int(0.8 * self.dataPositive.count())%0A        maxIndexNegative = int(0.8 * self.dataNegative.count())%0A%0A        for data in (self.dataPositive%5B:maxIndexPositive%5D + self.dataNegative%5B:maxIndexNegative%5D):%0A%0A            pass%0A%0A    def test(self):%0A        %22%22%22%0A        Testing results from bayes algorithm%0A%0A        :return: null%0A        %22%22%22
 %0A   
@@ -4843,24 +4843,28 @@
 xPositive = 
+int(
 0.8 * self.d
@@ -4882,16 +4882,17 @@
 .count()
+)
 %0A       
@@ -4911,16 +4911,20 @@
 ative = 
+int(
 0.8 * se
@@ -4946,16 +4946,17 @@
 .count()
+)
 %0A%0A      
@@ -4980,33 +4980,32 @@
 lf.dataPositive%5B
-:
 maxIndexPositive
@@ -4996,32 +4996,33 @@
 maxIndexPositive
+:
 %5D + self.dataNeg
@@ -5019,33 +5019,32 @@
 lf.dataNegative%5B
-:
 maxIndexNegative
@@ -5035,32 +5035,33 @@
 maxIndexNegative
+:
 %5D):%0A%0A           
@@ -5067,16 +5067,237 @@
   pass%0A%0A
+    def classify(self, dataFile):%0A        %22%22%22%0A        Classify a dataFile to positive or negative%0A%0A        :param dataFile: DataFile to classify%0A        :return: True if positive, False otherwise%0A        %22%22%22%0A%0A        pass
 %0A%0A#-----

Add testing and training method.