Beispiel #1
0
 def test_generate_d3_object(self):
     assert generate_d3_object(
         {'a': 1, 'b': 2, 'c': 3, 'd': 4},
         "object", "word", "count") == {
         'name': 'object', 'children': [{'word': 'a', 'count': 1},
                                        {'word': 'b', 'count': 2},
                                        {'word': 'c', 'count': 3},
                                        {'word': 'd', 'count': 4}]}
 def test_generate_d3_object(self):
     assert generate_d3_object(
         {'a': 1, 'b': 2, 'c': 3, 'd': 4},
         "object", "word", "count") == {
         'name': 'object', 'children': [{'word': 'a', 'count': 1},
                                        {'word': 'b', 'count': 2},
                                        {'word': 'c', 'count': 3},
                                        {'word': 'd', 'count': 4}]}
Beispiel #3
0
    def generate_d3_json_object(self, word_label: str,
                                count_label: str) -> object:
        """Generate a JSON object for d3 from the word counts of the file.

        :param word_label: label to use for identifying words in the
                           sub-objects.
        :param count_label: label to use for identifying counts in the
                            sub-objects.
        :return: the resultant JSON object, formatted for d3.
        """
        word_counts = self.get_word_counts()
        return general_functions.generate_d3_object(word_counts, self.label,
                                                    word_label, count_label)
Beispiel #4
0
    def generate_d3_json_object(self, word_label: str,
                                count_label: str) -> object:
        """ Generates a JSON object for d3 from the word counts of the file.

        :param word_label: label to use for identifying words in the
                           sub-objects.
        :param count_label: label to use for identifying counts in the
                            sub-objects.
        :return: the resultant JSON object, formatted for d3.
        """

        word_counts = self.get_word_counts()
        return general_functions.generate_d3_object(
            word_counts, self.label, word_label, count_label)
Beispiel #5
0
def generate_json_for_d3(file_manager: FileManager, merged_set):
    """
    Generates the data formatted nicely for the d3 visualization library.

    Args:
        merged_set: Boolean saying whether to merge all files into one data set
            or, if false, create a list of datasets.

    Returns:
        An object, formatted in the JSON that d3 needs, either a list or a
        dictionary.
    """

    active_files = []
    for l_file in list(file_manager.files.values()):
        if l_file.active:
            active_files.append(l_file)

    if merged_set:  # Create one JSON Object across all the chunks
        minimum_length = int(request.form['minlength']) \
            if 'minlength' in request.form else 0

        master_word_counts = {}
        for l_file in active_files:
            word_counts = l_file.get_word_counts()

            for key in word_counts:
                if len(key) <= minimum_length:
                    continue

                if key in master_word_counts:
                    master_word_counts[key] += word_counts[key]
                else:
                    master_word_counts[key] = word_counts[key]

        if 'maxwords' in request.form:
            # Make sure there is a number in the input form
            check_for_value = request.form['maxwords']
            if check_for_value == "":
                max_num_words = 100
            else:
                max_num_words = int(request.form['maxwords'])
            sorted_word_counts = sorted(master_word_counts,
                                        key=master_word_counts.__getitem__)
            j = len(sorted_word_counts) - max_num_words
            for i in range(len(sorted_word_counts) - 1, -1, -1):
                if i < j:
                    del master_word_counts[sorted_word_counts[i]]

        return_obj = general_functions.generate_d3_object(
            master_word_counts,
            object_label="tokens",
            word_label="name",
            count_label="size")

    else:  # Create a JSON object for each chunk
        return_obj = []
        for l_file in active_files:
            return_obj.append(
                l_file.generate_d3_json_object(word_label="text",
                                               count_label="size"))

    # NOTE: Objects in JSON are dictionaries in Python, but Lists are Arrays
    # are Objects as well.
    return return_obj