This extracts one bag-of-words from a list of strings. The documents are mapped to parallel processes.

    Inputs: - corpus: A list of strings.
            - lemmatizing: A string containing one of the following: "porter", "snowball" or "wordnet".

    Output: - bag_of_words: This is a bag-of-words in python dictionary format.
            - lemma_to_keywordbag_total: Aggregated python dictionary that maps stems/lemmas to original topic keywords.

def extract_bag_of_words_from_corpus_parallel(corpus, lemmatizing="wordnet"):
    """
    This extracts one bag-of-words from a list of strings. The documents are mapped to parallel processes.

    Inputs: - corpus: A list of strings.
            - lemmatizing: A string containing one of the following: "porter", "snowball" or "wordnet".

    Output: - bag_of_words: This is a bag-of-words in python dictionary format.
            - lemma_to_keywordbag_total: Aggregated python dictionary that maps stems/lemmas to original topic keywords.
    """
    ####################################################################################################################
    # Map and reduce document cleaning.
    ####################################################################################################################
    # Build a pool of processes.
    pool = Pool(processes=get_threads_number()*2,)

    # Partition the tweets to chunks.
    partitioned_corpus = chunks(corpus, len(corpus) / get_threads_number())

    # Map the cleaning of the tweet corpus to a pool of processes.
    list_of_bags_of_words, list_of_lemma_to_keywordset_maps = pool.map(partial(clean_corpus_serial, lemmatizing=lemmatizing), partitioned_corpus)

    # Reduce dictionaries to a single dictionary serially.
    bag_of_words = reduce_list_of_bags_of_words(list_of_bags_of_words)

    # Reduce lemma to keyword maps to a single dictionary.
    lemma_to_keywordbag_total = defaultdict(lambda: defaultdict(int))
    for lemma_to_keywordbag in list_of_lemma_to_keywordset_maps:
        for lemma, keywordbag in lemma_to_keywordbag.items():
            for keyword, multiplicity in keywordbag.items():
                lemma_to_keywordbag_total[lemma][keyword] += multiplicity

    return bag_of_words, lemma_to_keywordbag_total