我估计通常我的标题的答案是去阅读文档,但我浏览了NLTK书,但它没有给出答案.我是python的新手.
我有一堆.txt
文件,我希望能够使用NLTK为语料库提供的语料库功能nltk_data
.
我已经尝试PlaintextCorpusReader
但是我无法进一步:
>>>import nltk
>>>from nltk.corpus import PlaintextCorpusReader
>>>corpus_root = './'
>>>newcorpus = PlaintextCorpusReader(corpus_root, '.*')
>>>newcorpus.words()
Run Code Online (Sandbox Code Playgroud)
如何newcorpus
使用punkt 对句子进行分段?我尝试使用punkt函数,但punkt函数无法读取PlaintextCorpusReader
类?
你能否告诉我如何将分段数据写入文本文件?
编辑: 这个问题有一次赏金,它现在有第二个赏金.请参阅赏金框中的文字.
我有以下代码:
# returns string w/o leading or trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
news_corpus <- Corpus(VectorSource(news_raw$text)) # a column of strings.
corpus_clean <- tm_map(news_corpus, tolower)
corpus_clean <- tm_map(corpus_clean, removeNumbers)
corpus_clean <- tm_map(corpus_clean, removeWords, stopwords('english'))
corpus_clean <- tm_map(corpus_clean, removePunctuation)
corpus_clean <- tm_map(corpus_clean, stripWhitespace)
corpus_clean <- tm_map(corpus_clean, trim)
news_dtm <- DocumentTermMatrix(corpus_clean) # errors here
Run Code Online (Sandbox Code Playgroud)
当我运行该DocumentTermMatrix()
方法时,它给了我这个错误:
错误:inherits(doc,"TextDocument")不为TRUE
为什么我会收到此错误?我的行不是文本文件吗?
这是检查时的输出corpus_clean
:
[[153]]
[1] obama holds technical school model us
[[154]]
[1] oil boom produces jobs bonanza archaeologists
[[155]] …
Run Code Online (Sandbox Code Playgroud) 我的项目使用NLTK.如何列出项目的语料库和模型要求,以便自动安装?我不想点击nltk.download()
GUI,逐个安装软件包.
还有,任何方法来冻结相同的要求列表(如pip freeze
)?
我使用python连接到一个mysql数据库con = _mysql.connect('localhost', 'dell-pc', '', 'test')
我写的程序需要花费大量时间才能完全执行,即大约10个小时.实际上,我试图从语料库中读出不同的词.阅读完成后出现超时错误.
我检查了Mysql的默认超时时间:
+----------------------------+----------+
| Variable_name | Value |
+----------------------------+----------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| lock_wait_timeout | 31536000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| wait_timeout | 28800 |
+----------------------------+----------+
Run Code Online (Sandbox Code Playgroud)
如何更改默认超时?
从创建R中的语料库中的单词子集,应答者可以轻松地将term-document matrix
词汇转换为词云.
python库是否有一个类似的功能,它将原始文本文件或NLTK
语料库或Gensim
Mmcorpus带入词云?
结果看起来有点像这样:
我可以免费或低于100美元下载Treebank of English短语吗?我需要训练数据包含任何格式的英语句法分析句子(> 1000).基本上我所需要的只是这句话中的单词被词性所识别.
我尝试过各种方法从Project Gutenberg文本中剥离许可证,用作语言学习项目的语料库,但我似乎无法想出一种无监督,可靠的方法.到目前为止,我提出的最好的启发式方法是剥离前二十八行和后一个398,它们适用于大量文本.关于我可以自动剥离文本的方法的任何建议(对于许多文本非常相似,但在每种情况下略有不同,以及一些不同的模板),以及如何验证该文本的建议文本已被准确删除,会非常有用.
我TermDocumentMatrix
用tm
R中的包创建了几个.
我想在每组文档中找到10个最常用的术语,最终得到一个输出表,如:
corpus1 corpus2
"beach" "city"
"sand" "sidewalk"
... ...
[10th most frequent word]
Run Code Online (Sandbox Code Playgroud)
根据定义,findFreqTerms(corpus1,N)
返回出现N次或更多次的所有术语.要手动执行此操作,我可以更改N,直到我返回10个左右的术语,但输出为findFreqTerms
按字母顺序排列,因此除非我选择正确的N,否则我实际上不知道哪个是前10个.我怀疑这个涉及操纵您可以看到的TDM的内部结构,str(corpus1)
如在R tm包中创建最常用术语的矩阵,但这里的答案对我来说非常不透明所以我想重新解释这个问题.
谢谢!
我正在使用该tm
软件包使用以下代码清理一些数据:
mycorpus <- Corpus(VectorSource(x))
mycorpus <- tm_map(mycorpus, removePunctuation)
Run Code Online (Sandbox Code Playgroud)
然后,我想将语料库转换回数据框,以便导出包含数据帧原始格式的数据的文本文件.我尝试过以下方法:
dataframe <- as.data.frame(mycorpus)
Run Code Online (Sandbox Code Playgroud)
但是这会返回一个错误:
"as.data.frame.default中的错误.(mycorpus):无法将类"c(vcorpus,> corpus")强制转换为data.frame
如何将语料库转换为数据框?
我已经教过一些用Python进行文本挖掘的入门课程,并且该课程尝试了与提供的练习文本类似的方法.有些学生对text1.similar()的结果与其他学生不同.
所有版本等都是一样的.
有谁知道为什么会出现这些差异?谢谢.
在命令行使用的代码.
python
>>> import nltk
>>> nltk.download() #here you use the pop-up window to download texts
>>> from nltk.book import *
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: …
Run Code Online (Sandbox Code Playgroud) corpus ×10
nlp ×4
nltk ×4
python ×4
r ×3
tm ×3
gensim ×1
heuristics ×1
install ×1
mysql ×1
packages ×1
requirements ×1
similarity ×1
stripping ×1
tagging ×1
text-mining ×1
word-cloud ×1