【问题标题】:how to perform a good tokenization for words using python如何使用 python 对单词进行良好的标记化
【发布时间】:2020-11-03 18:07:12
【问题描述】:

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

  • 实际句子:

'喜欢 Picture2Life.com !!! Y 所有有趣的应用程序 iphone而不是黑莓??!! '

  • 结果:

'islovinpicturelifecomyallfunappsrforiphoneandnotblackberry'

结果必须是这样的: 是热爱图片 2 的生活。 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))

【问题讨论】:

标签: python pandas dataframe tokenize


【解决方案1】:

好吧,在你的第一行

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

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

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

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 2012-05-25
    相关资源
    最近更新 更多