如何使用python对单词执行良好的标记化

2024-04-24 14:58:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  • 实际判刑:

是爱的图片2生活网!!!Y所有有趣的应用程序r iphone而不是黑莓??”

  • 结果:

“IsLovinPictureLifeComyallfunsr适用于iPhone而非黑莓”

结果必须如下所示: 是一幅充满爱的人生画卷。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))

Tags: the函数textinrawifallprocess
1条回答
网友
1楼 · 发布于 2024-04-24 14:58:13

嗯,在你的第一行

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

‘ “” “序列中包含空格字符,因此它在运行列表时删除了所有空格(因此也删除了空格):

nopunc = [char for char in list(raw_text) if char not in ppt]

相关问题 更多 >