from meta import Classifier
from sklearn.svm import SVC
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif

class SVMClassifier(Classifier):

    def train(self, training_set):
        """
        create vectors with all words of email and label
        Exctract all the words used in the emails
        select features
        train
        """
        all_features = self.all_words(training_set)
        features = []
        labels = []
        n_laps = 200
        i = 0
        for name, email_data in training_set.items():
            # Find all used words
            f_dict = {}
            for word in email_data["body"].split():
                f_dict[word] = True

            f_vec = []
            for key in all_features:
                if key in f_dict:
                    f_vec.append(1)
                else:
                    f_vec.append(0)

            labels.append(email_data["label"])
            features.append(f_vec)
            print(i, name)
            # Abort earlier so we can limit the nr of features
            if i == n_laps:
                break
            else:
                i = i + 1

        important_features = SelectKBest(f_classif, k=100).fit_transform(features, labels)

        self.classifier = SVC()


    def classify(self, email):
        return 1

    def all_words(self, emails):
        res = set()
        for _, email in emails.items():
            for word in email["body"].split():
                res.add(word)
        return list(res)

@@ -20,16 +20,67 @@
 assifier
+, d_print%0Afrom timeit import default_timer as timer
 %0Afrom sk
@@ -535,28 +535,136 @@
  %5B%5D%0A
-        n_laps = 200
+%0A%0A        %0A        # The number of emails to use for training%0A        n_laps = len(training_set)%0A        start = timer()        
 %0A   
@@ -1150,35 +1150,8 @@
 ec)%0A
-            print(i, name)%0A
     
@@ -1316,124 +1316,610 @@
     
-important_features = SelectKBest(f_classif, k=100).fit_transform(features, labels)%0A%0A        self.classifier = SVC()%0A
+end = timer()%0A        d_print(%22Pre-processing done, t = %22 + str(end - start), source=%22SVM%22)%0A%0A        # start = timer()%0A        # self.feature_selection = SelectKBest(f_classif, k=100)%0A        # important_features = self.feature_selection.fit_transform(features, labels)%0A        # end = timer()%0A        # d_print(%22Feature selection done, t = %22 + str(end - start), source=%22SVM%22)%0A        %0A%0A        start = timer()%0A        self.classifier = SVC()%0A        self.classifier.fit(features, labels)%0A        end = timer()%0A        d_print(%22Classifier training done, t = %22 + str(end - start), source=%22SVM%22)%0A        
 %0A%0A  

classify af