在训练MALLET LDA之前,你们有什么建议我可以将文件细分为句子吗?
先感谢您
例如,您可以使用OpenNLP句子检测工具。他们已经存在了一段时间,在大多数情况下表现得不错。
文档是here,模型可以下载here。请注意,1.5版本的型号与较新的opennlp-tools版本1.8.4完全兼容
如果您使用的是Maven,只需将以下内容添加到您的pom中即可。
<dependency>
<groupId>org.apache.opennlp</groupId>
<artifactId>opennlp-tools</artifactId>
<version>1.8.4</version>
</dependency>
如果您打算将模型输入从文档切换到句子,请注意vanilla LDA(这也影响当前在Mallet中的实现,afaik)可能不会产生令人满意的结果,因为单词共现计数在句子中不是很明显。
我建议调查段落级别是否更有趣。可以使用换行符模式提取文档中的段落。例如,当您有两个连续的换行符时,会开始一个新段落。
根据您对句子的定义,这可以使用String.split("\\.\\s")
在Java中完成。假设用户以句点结束句子并开始使用空格的新句子。由于split的参数是正则表达式,因此转义期间。 \\s
的意思是“任何空格”,它也会处理行尾和制表符。
String test = "Hello. World. Cat eats dog.";
String[] splitString = test.split("\\.\\s");
splitString
的内容现在是{"Hello", "World", "Cat eats dog."}
,请注意最后一个时期没有删除,因为它没有空格。您现在可以将句子写入文件。你可以使用BufferedWriter来做到这一点:
try{
String filename = "test";
int i = 0;
for(String sentence : splitString) {
File file = new File(filename+""+i+""+".txt");
file.createNewFile();
/*returns false if the file already exists (you can prevent
overriding like this)*/
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.append(sentence + "\n");
i++;
}
} catch(IOException ioexception)
{
System.out.println(ioexception.getMessage());
System.exit(1);
}
现在,这将在新文件中打印拆分句子,每个文件都在不同的文件中。请注意,因为这会导致FAT32格式化系统(标准版)出现空间问题,因为它们为每个文件分配32kB,无论它是否至少为32kB(文件为8kB,但在驱动器上占用32kB空间)。这可能有点不切实际但它确实有效。现在你只需要import-dir
所有这些文件所在的目录,并使用LDA中的文件。您还可以在此处阅读所提供教程的一部分:
对于较大的文件(大约5000个句子及以上[导致至少160 MB的数据])我建议你进行拆分但不是写入许多文件,而是只写一个并编写自己的方式来导入数据使用MALLET API。请查看http://mallet.cs.umass.edu/import-devel.php以获取开发人员指南,并在http://mallet.cs.umass.edu/api/获取更多相关信息。
这些功能将准备您的文档传递到您的LDA。我也会考虑设置一个bow_corpus,因为LDA取数字而不是句子。这就像“go”这个词被定义为“go”然后编号/索引说2343并且按频率计数可能会弹出两次所以bow_corpus将是(2343,2),这是LDA所期望的。
# Gensim unsupervised topic modeling, natural language processing, statistical machine learning
import gensim
# convert a document to a list of tolkens
from gensim.utils import simple_preprocess
# remove stopwords - words that are not telling: "it" "I" "the" "and" ect.
from gensim.parsing.preprocessing import STOPWORDS
# corpus iterator
from gensim import corpora, models
# nltk - Natural Language Toolkit
# lemmatized — words in third person are changed to first person and verbs in past and future tenses are changed
# into present.
# stemmed — words are reduced to their root form.
import nltk
nltk.download('wordnet')
from nltk.stem import WordNetLemmatizer, SnowballStemmer
from nltk.stem.porter import *
# Create functions to lemmatize stem, and preprocess
# turn beautiful, beautifuly, beautified into stem beauti
def lemmatize_stemming(text):
stemmer = PorterStemmer()
return stemmer.stem(WordNetLemmatizer().lemmatize(text, pos='v'))
# parse docs into individual words ignoring words that are less than 3 letters long
# and stopwords: him, her, them, for, there, ect since "their" is not a topic.
# then append the tolkens into a list
def preprocess(text):
result = []
for token in gensim.utils.simple_preprocess(text):
if token not in gensim.parsing.preprocessing.STOPWORDS and len(token) > 3:
nltk.bigrams(token)
result.append(lemmatize_stemming(token))
return result
# send the comments row through the preprocessing step
# map itterates through rows into a function
processed_docs = documents['Your Comments title header'].map(preprocess)