作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个由两列组成的数据框:ID 和 TEXT。假设数据如下:
ID TEXT
265 The farmer plants grain. The fisher catches tuna.
456 The sky is blue.
434 The sun is bright.
921 I own a phone. I own a book.
df.TEXT.apply(nltk.sent_tokenize)
TEXT
The farmer plants grain.
The fisher catches tuna.
The sky is blue.
The sun is bright.
I own a phone.
I own a book.
ID
数字联系起来(在进一步的文本清理之后):
ID TEXT
265 'farmer', 'plants', 'grain'
265 'fisher', 'catches', 'tuna'
456 'sky', 'blue'
434 'sun', 'bright'
921 'I', 'own', 'phone'
921 'I', 'own', 'book'
最佳答案
编辑 :由于@alexis 有保证的刺激,这里是一个更好的回应
句子标记化
这应该为您提供每个 ID 和句子一行的 DataFrame:
sentences = []
for row in df.itertuples():
for sentence in row[2].split('.'):
if sentence != '':
sentences.append((row[1], sentence))
new_df = pandas.DataFrame(sentences, columns=['ID', 'SENTENCE'])
sentences = []
for row in df.itertuples():
for sentence in sent_tokenize(row[2]):
sentences.append((row[1], sentence))
new_df = pandas.DataFrame(sentences, columns=['ID', 'SENTENCE'])
new_df['SENTENCE_noperiods'] = new_df.SENTENCE.apply(lambda x: x.strip('.'))
df = df.join(df.TEXT.apply(sent_tokenize).rename('SENTENCES'))
sentences = df.SENTENCES.apply(pandas.Series)
sentences.columns = ['sentence {}'.format(n + 1) for n in sentences.columns]
df = df.join(sentences)
df['sent_1_words'] = df['sentence 1'].apply(word_tokenize)
关于python - 通过 Pandas 数据帧运行 nltk sent_tokenize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43922145/
我可以成功地将文本读入变量,但是在尝试标记文本时我遇到了这个奇怪的错误: sentences=nltk.sent_tokenize(sample) UnicodeDecodeError: 'ascii
我有一个由两列组成的数据框:ID 和 TEXT。假设数据如下: ID TEXT 265 The farmer plants grain. The fisher catches tun
我有一个包含数千个句子的文件,我想找到包含特定字符/单词的句子。 最初,我对整个文件进行标记(使用 sent_tokenize),然后迭代句子来查找单词。然而,这太慢了。既然我可以快速找到单词的索引,
我正在浏览 this question . 我只是想知道 NLTK 在单词/句子标记化方面是否会比正则表达式更快。 最佳答案 默认的 nltk.word_tokenize() 使用 Treebank
我正在使用 Google Colab 编写通过视频学习的脚本。不幸的是,我按照视频说明操作时遇到错误。 sentences_tokens=nltk.sent_tokenize(allParagraph
我是一名优秀的程序员,十分优秀!