首页 > 解决方案 > 如何使用 python 对单词进行良好的标记化

问题描述

我在 python 中有一个函数,它使用分词器将一个句子分成单词。问题是当我运行这个函数时,返回的输出是一个没有空格的单词。

'是爱 Picture2Life.com !!!Y 所有有趣的应用程序都适用于 iPhone 而不是黑莓??!!'

'islovinpicturelifecomyallfunappsrforiphoneandnotblackberry'

结果一定是这样的: is love picture 2 life 。com....

代码:

ppt = '''...!@#$%^&*()....{}’‘ “”  “[]|._-`/?:;"'\,~12345678876543'''

#tekonize helper function
def text_process(raw_text):
    '''
    parameters:
    =========
    raw_text: text as input
    functions:
    ==========
    - remove all punctuation
    - remove all stop words
    - return a list of the cleaned text

    '''
    #check characters to see if they are in punctuation
    nopunc = [char for char in list(raw_text) if char not in ppt]

    
    
    # join the characters again to form the string
    nopunc = "".join(nopunc)
    
    #now just remove ant stopwords
     
    words = [word for word in nopunc.lower().split() if   word.lower() not in stopwords.words("english")]
    return words

ddt= data.text[2:3].apply(text_process)
print("example: {}".format(ddt))

标签: pythonpandasdataframetokenize

解决方案



推荐阅读