- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我最近开始使用 nltk 模块进行文本分析。我被困在一个点上。我想在数据帧上使用 word_tokenize,以获得数据帧特定行中使用的所有单词。
data example:
text
1. This is a very good site. I will recommend it to others.
2. Can you please give me a call at 9983938428. have issues with the listings.
3. good work! keep it up
4. not a very helpful site in finding home decor.
expected output:
1. 'This','is','a','very','good','site','.','I','will','recommend','it','to','others','.'
2. 'Can','you','please','give','me','a','call','at','9983938428','.','have','issues','with','the','listings'
3. 'good','work','!','keep','it','up'
4. 'not','a','very','helpful','site','in','finding','home','decor'
基本上,我想分离所有单词并找到数据框中每个文本的长度。
我知道 word_tokenize 可以用于字符串,但如何将它应用于整个数据框?
请帮忙!
提前致谢...
最佳答案
您可以使用 DataFrame API 的apply方法:
import pandas as pd
import nltk
df = pd.DataFrame({'sentences': ['This is a very good site. I will recommend it to others.', 'Can you please give me a call at 9983938428. have issues with the listings.', 'good work! keep it up']})
df['tokenized_sents'] = df.apply(lambda row: nltk.word_tokenize(row['sentences']), axis=1)
输出:
>>> df
sentences \
0 This is a very good site. I will recommend it ...
1 Can you please give me a call at 9983938428. h...
2 good work! keep it up
tokenized_sents
0 [This, is, a, very, good, site, ., I, will, re...
1 [Can, you, please, give, me, a, call, at, 9983...
2 [good, work, !, keep, it, up]
要找到每个文本的长度,请再次尝试使用 apply 和 lambda 函数:
df['sents_length'] = df.apply(lambda row: len(row['tokenized_sents']), axis=1)
>>> df
sentences \
0 This is a very good site. I will recommend it ...
1 Can you please give me a call at 9983938428. h...
2 good work! keep it up
tokenized_sents sents_length
0 [This, is, a, very, good, site, ., I, will, re... 14
1 [Can, you, please, give, me, a, call, at, 9983... 15
2 [good, work, !, keep, it, up] 6
关于python - 如何在数据框中使用 word_tokenize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33098040/
我是 python 的新手。我试图找到我的文本的频率分布。这是代码, import nltk nltk.download() import os os.getcwd() text_file=open(
我最近开始使用 nltk 模块进行文本分析。我被困在一个点上。我想在数据帧上使用 word_tokenize,以获得数据帧特定行中使用的所有单词。 data example: text
我正在使用 Python 的 nltk,我想标记一个包含引号的句子,但它将 " 变成了 `` 和 '' . 例如: >>> from nltk import word_tokenize >>> sen
我是 Python nltk 新手 目前,我有一个程序可以对句子进行 word_tokenize。然后对 word_tokenize 进行处理,纠正某些名词的大小写。这个过程工作正常,现在我想将处理后
我正在探索 NLTK 的一些语料库并遇到以下行为:word_tokenize() 和单词产生不同的单词集()。 这是一个使用 webtext 的例子: from nltk.corpus import
我有一个字典,包含一个数据库主键和一个字符串。 self.mydict = { 1:'a small example' ,2:'some sentence' ,3:'a very long strin
我正在尝试对 Twitter 文本进行标记化。当我将函数 nltk.word_tokenize() 应用于每个单独的 twitter 文本时,即使对于一些非常丑陋的文本,它也能完美运行,例如 '\xd
我正在尝试使用 NLTK word_tokenize 对 French 中的文本使用: txt = ["Le télétravail n'aura pas d'effet sur ma vie"] p
我正在做 NLTK 书中关于从网络和磁盘访问文本(第 3 章)的一些练习。调用 word_tokenize 时出现错误。 这是我的代码: >>> import nltk >>> from urllib
我正在使用 nltk.word_tokenize 对文本进行标记,我还想将原始原始文本中的索引获取到每个标记的第一个字符,即 import nltk x = 'hello world' tokens
有谁知道nltk的wordpunct_tokenize和word_tokenize的区别?我正在使用 nltk=3.2.4 并且 wordpunct_tokenize 的文档字符串中没有任何内容可以解
我希望使用 NLTK word_tokenize 忽略单词之间的字符。 如果我有一句话: test = 'Should I trade on the S&P? This works with a ph
我正在使用 NLTK 对一些相当大的数据集执行自然语言处理,并且希望利用我的所有处理器核心。似乎多处理模块就是我所追求的,当我运行以下测试代码时,我看到所有核心都在被利用,但代码永远不会完成。 执行相
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
关于删除标点符号有很多可用的方法,但我似乎找不到任何保留它的方法。 如果我这样做: from nltk import word_tokenize test_str = "Some Co Inc. Ot
我将在我的帐户空间配额非常有限的集群上使用 nltk.tokenize.word_tokenize。在家里,我通过 nltk.download() 下载了所有 nltk 资源,但我发现它需要大约 2.
我正在使用 Python 的 NLTK 库来标记我的句子。 如果我的代码是 text = "C# billion dollars; we don't own an ounce C++" print n
我正在使用 nltk.word_tokenize用于标记一些包含编程语言、框架等的句子,这些句子被错误标记。 例如: >>> tokenize.word_tokenize("I work with C
我无法理解两者之间的区别。不过,我开始知道 word_tokenize 使用 Penn-Treebank 进行标记化。但是 TweetTokenizer 上没有任何内容可用。对于哪种数据,我应该使用
我需要对多种语言的文本进行名称实体提取:西类牙语、葡萄牙语、希腊语、捷克语、中文。 是否有这两个功能的所有支持语言的列表?是否有使用其他语料库的方法,以便可以包含这些语言? 最佳答案 默认情况下,这两
我是一名优秀的程序员,十分优秀!