# Path: simpleai/machine_learning/classifiers.py
# def tree_to_str(root):
#     """
#     Returns a string representation of a decision tree with
#     root node `root`.
#     """
# 
#     xs = []
#     for value, node, depth in iter_tree(root):
#         template = "{indent}"
#         if node is not root:
#             template += "case={value}\t"
#         if node.attribute is None:
#             template += "result={result} -- P={prob:.2}"
#         else:
#             template += "split by {split}:\t" +\
#                         "(partial result={result} -- P={prob:.2})"
#         line = template.format(indent="    " * depth,
#                                value=value,
#                                result=node.result[0],
#                                prob=node.result[1],
#                                split=str(node.attribute))
#         xs.append(line)
#     return "\n".join(xs)


import random
from simpleai.machine_learning import DecisionTreeLearner_LargeData, \
                                      ClassificationProblem, Attribute, \
                                      precision, NaiveBayes
from simpleai.machine_learning.classifiers import tree_to_str


print("Counting examples")
# line count
N = 0
for _, filename in input_files:
    for _ in open(filename):
        N += 1
print("Corpus has {} examples".format(N))

# Choose test set, either 10% or 10000 examples, whatever is less
M = min(N / 10, 10000)
testindexes = set(random.sample(range(N), M))
print("Keeping {} examples for testing".format(M))

problem = LanguageClassificationProblem()
train = OnlineCorpusReader(input_files, lambda i: i not in testindexes)
test = OnlineCorpusReader(input_files, lambda i: i in testindexes)


print("Training Naive Bayes...")
classifier = NaiveBayes(train, problem)
print("Testing...")
p = precision(classifier, test)
print("Precision Naive Bayes = {}".format(p))


print("Training Decision Tree (large data)...")
classifier = DecisionTreeLearner_LargeData(train, problem, minsample=500)
print("Final tree:")

# Path: simpleai/machine_learning/classifiers.py
# def tree_to_str(root):
#     """
#     Returns a string representation of a decision tree with
#     root node `root`.
#     """
# 
#     xs = []
#     for value, node, depth in iter_tree(root):
#         template = "{indent}"
#         if node is not root:
#             template += "case={value}\t"
#         if node.attribute is None:
#             template += "result={result} -- P={prob:.2}"
#         else:
#             template += "split by {split}:\t" +\
#                         "(partial result={result} -- P={prob:.2})"
#         line = template.format(indent="    " * depth,
#                                value=value,
#                                result=node.result[0],
#                                prob=node.result[1],
#                                split=str(node.attribute))
#         xs.append(line)
#     return "\n".join(xs)


# Path: samples/machine_learning/language_classification.py
import random
from simpleai.machine_learning import DecisionTreeLearner_LargeData, \
                                      ClassificationProblem, Attribute, \
                                      precision, NaiveBayes
from simpleai.machine_learning.classifiers import tree_to_str



print("Counting examples")
# line count
N = 0
for _, filename in input_files:
    for _ in open(filename):
        N += 1
print("Corpus has {} examples".format(N))

# Choose test set, either 10% or 10000 examples, whatever is less
M = min(N / 10, 10000)
testindexes = set(random.sample(range(N), M))
print("Keeping {} examples for testing".format(M))

problem = LanguageClassificationProblem()
train = OnlineCorpusReader(input_files, lambda i: i not in testindexes)
test = OnlineCorpusReader(input_files, lambda i: i in testindexes)


print("Training Naive Bayes...")
classifier = NaiveBayes(train, problem)
print("Testing...")
p = precision(classifier, test)
print("Precision Naive Bayes = {}".format(p))


print("Training Decision Tree (large data)...")
classifier = DecisionTreeLearner_LargeData(train, problem, minsample=500)
print("Final tree:")

print(tree_to_str(classifier.root))