代码之家  ›  专栏  ›  技术社区  ›  Christopher

显示柠檬化进程

  •  0
  • Christopher  · 技术社区  · 7 年前

    下面的脚本用于将给定的输入列与文本进行二元化:

    %%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)
    

    问题:

    如何打印柠檬化进程的进度?

    1 回复  |  直到 7 年前
        1
  •  1
  •   N.Clarke    7 年前

    您可以将一个变量传递到lemmatization函数中,以跟踪它被调用的次数,然后每隔1000次左右打印一次。我将它包装在下面的列表中,这样int可以通过引用而不是通过值传递。

    %%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)