NLTK 令牌 - 从熊猫系列中创建单个单词列表



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

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

将单词标记化为 pandas 数据帧中的新列

将熊猫数据帧列传递给 NLTK 标记器 等。。。


这是我所拥有的:一个数据帧,其中包含有关我们的学生在我们的网站上搜索信息时寻找的内容(这是校园的网站(的大量信息。

它看起来有点像这样:

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

我想要的,是一个大列表,看起来像["查询"、"考试"、"会话"、"六月"、"2020"、"何时"、"是"、"考试"、"考试"、"什么"、"我的"、"教师"、"电子邮件"、"地址]===>一个字符串,所有单词(无句子(,无标点符号。

我试过:

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)

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

你可以这样做:

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']

最新更新