Search code examples
gensimlemmatization

Show progress in lemmatization


following script is used to lemmatize a given input column with text:

%%time
import pandas as pd
from gensim.utils import lemmatize
from gensim.parsing.preprocessing import STOPWORDS
STOPWORDS = list(STOPWORDS)

data = pd.read_csv('https://pastebin.com/raw/0SEv1RMf')

def lemmatization(s):
    result = []
    # lowercase, tokenize, remove stopwords, len>3, lemmatize
    for token in lemmatize(s, stopwords=STOPWORDS, min_length=3):
        result.append(token.decode('utf-8').split('/')[0])
    # print(len(result)) <- This didn't work.
    return result

X_train = data.apply(lambda r: lemmatization(r['text']), axis=1)
print(X_train)

Question:

How can I print the progress of the lemmatization progress?


Solution

  • You could pass a variable into the lemmatization function to keep track of the number of times it was called - and then print it every 1000 iterations or so. I have wrapped it in a list below so the int can be passed by reference rather than by value.

    %%time
    import pandas as pd
    from gensim.utils import lemmatize
    from gensim.parsing.preprocessing import STOPWORDS
    STOPWORDS = list(STOPWORDS)
    
    data = pd.read_csv('https://pastebin.com/raw/0SEv1RMf')
    
    iteration_count = [0]
    
    def lemmatization(s, iteration_count):
        result = []
        # lowercase, tokenize, remove stopwords, len>3, lemmatize
        for token in lemmatize(s, stopwords=STOPWORDS, min_length=3):
            result.append(token.decode('utf-8').split('/')[0])
        # print(len(result)) <- This didn't work.
    
        iteration_count[0] += 1
    
        if iteration_count[0] % 1000 == 0:
            print(iteration_count[0])
    
        return result
    
    X_train = data.apply(lambda r: lemmatization(r['text'], iteration_count), axis=1)
    print(X_train)