<commit_before>import csv


class OutputText(object):
    """OutputText the clustering result to various types of file such as txt, csv, and html.
    """
    @staticmethod
    def txt_perline(perline_file, perline_analysis, max_cluster_id, cluster_labels, original_logs):
        """Write clustering result per line to txt file. This clustering result is for all members in a node.

        Parameters
        ----------
        perline_file            : str
            Filename of output file per line.
        perline_analysis        : dict
            Dictionary of cluster label per log line.
        cluster_labels          : iterable
            List of cluster labels in auth.log.
        max_cluster_id          : int
            Maximum value of cluster identifier.
        original_logs           : iterable
            List of original event logs.
        """
        fopen = open(perline_file, 'w')
        for rowid, cluster_id in perline_analysis.iteritems():
            cluster_label = 'undefined' if cluster_id > max_cluster_id else cluster_labels[cluster_id]
            fopen.write(str(cluster_id) + '; ' + cluster_label + '; ' + original_logs[rowid])
        fopen.close()

    @staticmethod
    def txt_percluster(percluster_file, clusters, mode, graph, original_logs):
        """Write clustering result to txt file.

        Parameters
        ----------
        percluster_file         : str
            Filename of output file per cluster.
        clusters                : dict
            Dictionary of a list containing node identifier per cluster.
        mode                    : str
            Mode of clustering method, i.e., graph or text.
        graph                   : graph
            The graph which its clustering result to be written to a file.
        original_logs           : iterable
            List of original event logs.
        """
        f = open(percluster_file, 'w')
        for cluster_id, nodes in clusters.iteritems():
            f.write('Cluster #' + str(cluster_id) + '\n')
            for node in nodes:
                if mode == 'graph':
                    members = graph.node[node]['member']
                    for member in members:
                        f.write(original_logs[member])
                elif mode == 'text' or mode == 'text-h5' or mode == 'text-cpu' or mode == 'text-graph':
                    f.write(original_logs[node] + '\n')
            f.write('\n')

        f.close()

    @staticmethod
    def csv_cluster_property(report_file, cluster_property, cluster_abstraction, anomaly_score, quadratic_score,
                             normalized_score, sentiment_score, anomaly_decision, evaluation_metrics):
        """Write cluster property to a csv file.

        Parameters<commit_msg>Edit mode in output per cluster<commit_after>import csv


class OutputText(object):
    """OutputText the clustering result to various types of file such as txt, csv, and html.
    """
    @staticmethod
    def txt_perline(perline_file, perline_analysis, max_cluster_id, cluster_labels, original_logs):
        """Write clustering result per line to txt file. This clustering result is for all members in a node.

        Parameters
        ----------
        perline_file            : str
            Filename of output file per line.
        perline_analysis        : dict
            Dictionary of cluster label per log line.
        cluster_labels          : iterable
            List of cluster labels in auth.log.
        max_cluster_id          : int
            Maximum value of cluster identifier.
        original_logs           : iterable
            List of original event logs.
        """
        fopen = open(perline_file, 'w')
        for rowid, cluster_id in perline_analysis.iteritems():
            cluster_label = 'undefined' if cluster_id > max_cluster_id else cluster_labels[cluster_id]
            fopen.write(str(cluster_id) + '; ' + cluster_label + '; ' + original_logs[rowid])
        fopen.close()

    @staticmethod
    def txt_percluster(percluster_file, clusters, mode, graph, original_logs):
        """Write clustering result to txt file.

        Parameters
        ----------
        percluster_file         : str
            Filename of output file per cluster.
        clusters                : dict
            Dictionary of a list containing node identifier per cluster.
        mode                    : str
            Mode of clustering method, i.e., graph or text.
        graph                   : graph
            The graph which its clustering result to be written to a file.
        original_logs           : iterable
            List of original event logs.
        """
        f = open(percluster_file, 'w')
        for cluster_id, nodes in clusters.iteritems():
            f.write('Cluster #' + str(cluster_id) + '\n')
            for node in nodes:
                if mode == 'graph':
                    members = graph.node[node]['member']
                    for member in members:
                        f.write(original_logs[member])
                elif mode == 'text' or mode == 'text-csv':
                    f.write(original_logs[node] + '\n')
            f.write('\n')

        f.close()

    @staticmethod
    def csv_cluster_property(report_file, cluster_property, cluster_abstraction, anomaly_score, quadratic_score,
                             normalized_score, sentiment_score, anomaly_decision, evaluation_metrics):
        """Write cluster property to a csv file.

        Parameters
{'fraction_non_alphanumeric': 0.07982662091385227, 'fraction_numerical': 0.00018060321473722233, 'mean_word_length': 1.7579681274900398, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2242356', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1340, 'n_words': 452}