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

NLTK tokens - creating a single list of words from a pandas series

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

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

等...


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

看起来有点像这样:

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', 'my', 'teacher', '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)

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

你可以这样做:

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