python 的 corpora用法
Python中的corpora是一组文本语料库,包含大量文本和语言数据,可以在自然语言处理和机器学习等领域中使用。corpora提供了方便的接口来访问这些语料库,可以很容易地在Python中使用。
以下是corpora的用法:
- 导入corpora库
import nltk.corpus
- 查看可用的语料库
nltk.corpus.fileids()
- 加载语料库
corpus = nltk.corpus.gutenberg
- 访问语料库中的文件
corpus.fileids()
- 加载文件
file = corpus.raw('austen-emma.txt')
- 分词
tokens = nltk.word_tokenize(file)
- 去除停用词
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
filtered_tokens = [word for word in tokens if word.lower() not in stop_words]
- 统计单词出现次数
from collections import Counter
word_counts = Counter(filtered_tokens)
- 计算TF-IDF
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer()
tfidf.fit_transform(file)
- 查看语料库中的元数据
corpus.readme()
- 访问语料库中特定文本的元数据
corpus.raw('austen-emma.txt')[:100]
以上是corpora的常见用法,可以根据不同的语料库和需求进行调整和扩展。
原文地址: https://www.cveoy.top/t/topic/bsoi 著作权归作者所有。请勿转载和采集!