【问题标题】:NLTK tokens - creating a single list of words from a pandas seriesNLTK 令牌 - 从熊猫系列中创建单个单词列表
【发布时间】:2020-05-30 16:52:26
【问题描述】:

我正在寻找有关 NLTK 的帮助,或者任何其他可以帮助我解决我面临的问题的库。

我不是 Python 专家(实际上我是 4 个月前才开始学习 Python),但在寻求帮助之前我已经做了一些研究:

Tokenizing words into a new column in a pandas dataframe

Passing a pandas dataframe column to an NLTK tokenizer 等等……


这就是我所拥有的:一个数据框,其中包含很多关于我们的学生在我们的网站上搜索信息时所寻找的信息(它是校园网站)。

看起来有点像这样:

session             | student_query
2020-05-15 09:34:21 | exams session june 2020
2020-05-15 09:41:12 | when are the exams?
2020-05-15 09:59:51 | exams.
2020-05-15 10:02:18 | what's my teacher's email address

我想要的是一个看起来像的大列表: ['query', 'exams', 'session', 'june', '2020', 'when', 'are', 'the', Exams', 'exams', 'what', 's' , '我的', '老师', 's', 'email', '地址] ===> 一串,所有单词(无句子),无标点符号。

我试过了:

tokens = df['query'].apply(word_tokenize)
text = nltk.Text(tokens)

===> 为每行提供一个单独的字符串

sentences = pd.Series(df.Name)
sentences = sentences.str.replace('[^A-z ]','').str.replace(' +',' ').str.strip()
splitwords = [ nltk.word_tokenize( str(sentence) ) for sentence in sentences ]
print(splitwords)

===>好一点,但也不是我想要的

【问题讨论】:

    标签: python pandas nltk


    【解决方案1】:

    你可以这样做:

    df['student_query'] = df['student_query'].str.replace(r'\?|\.|\'', ' ')
    list_of_words = ' '.join(df['student_query']).split()
    print(list_of_words)
    
    ['exams', 'session', 'june', '2020', 'when', 'are', 'the', 'exams', 'exams', 'what', 's', 'my', 'teacher', 's', 'email', 'address']
    

    【讨论】:

    • 就是这样!非常感谢哥们! :) 我已经做了一个 for 循环的事情,但是你写的更有意义。谢谢,真的!
    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2017-01-25
    • 2020-09-21
    • 2021-09-20
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多