将 csv 文件中的每个唯一单词标记化
Get each unique word in a csv file tokenized
Here is the CSV tableCSV table 中有两列。一个是摘要,一个是正文。在我将它们组合在一起、转换为数据框并保存为 CSV 文件之前,两列都是 typeOfList。顺便说一句,table 中的文本已经被清理(删除所有标记并转换为小写):
我想遍历 table 中的每个单元格,将摘要和文本拆分为单词并标记每个单词。我该怎么做?
我尝试使用 python CSV reader 和 df.apply(word_tokenize)。我也尝试了 newList=set(summaries+texts),但后来我无法标记它们。
解决问题的任何解决方案,无论是使用 CSV 文件、数据框还是列表。提前感谢您的帮助!
注:真正的table有5万多行
===一些更新==
这是我试过的代码。
import pandas as pd
data= pd.read_csv('test.csv')
data.head()
newTry=data.apply(lambda x: " ".join(x), axis=1)
type(newTry)
print (newTry)
import nltk
for sentence in newTry:
new=sentence.split()
print(new)
print(set(new))
enter image description here
请参考截图中的输出。列表中有重复的单词,还有一些方括号。我应该如何删除它们?我试过set,但它只给出了一个句子值。
您可以使用内置的csv包来读取csv文件。和 nltk 来标记单词:
from nltk.tokenize import word_tokenize
import csv
words = []
def get_data():
with open("sample_csv.csv", "r") as records:
for record in csv.reader(records):
yield record
data = get_data()
next(data) # skip header
for row in data:
for sent in row:
for word in word_tokenize(sent):
if word not in words:
words.append(word)
print(words)
Here is the CSV tableCSV table 中有两列。一个是摘要,一个是正文。在我将它们组合在一起、转换为数据框并保存为 CSV 文件之前,两列都是 typeOfList。顺便说一句,table 中的文本已经被清理(删除所有标记并转换为小写):
我想遍历 table 中的每个单元格,将摘要和文本拆分为单词并标记每个单词。我该怎么做?
我尝试使用 python CSV reader 和 df.apply(word_tokenize)。我也尝试了 newList=set(summaries+texts),但后来我无法标记它们。 解决问题的任何解决方案,无论是使用 CSV 文件、数据框还是列表。提前感谢您的帮助!
注:真正的table有5万多行
===一些更新==
这是我试过的代码。
import pandas as pd
data= pd.read_csv('test.csv')
data.head()
newTry=data.apply(lambda x: " ".join(x), axis=1)
type(newTry)
print (newTry)
import nltk
for sentence in newTry:
new=sentence.split()
print(new)
print(set(new))
enter image description here
请参考截图中的输出。列表中有重复的单词,还有一些方括号。我应该如何删除它们?我试过set,但它只给出了一个句子值。
您可以使用内置的csv包来读取csv文件。和 nltk 来标记单词:
from nltk.tokenize import word_tokenize
import csv
words = []
def get_data():
with open("sample_csv.csv", "r") as records:
for record in csv.reader(records):
yield record
data = get_data()
next(data) # skip header
for row in data:
for sent in row:
for word in word_tokenize(sent):
if word not in words:
words.append(word)
print(words)