【发布时间】:2018-07-28 09:37:29
【问题描述】:
你们对我在训练 MALLET LDA 之前可能将文档细分为句子的方式有什么建议吗?
提前谢谢你
【问题讨论】:
你们对我在训练 MALLET LDA 之前可能将文档细分为句子的方式有什么建议吗?
提前谢谢你
【问题讨论】:
根据您对句子的定义,这可以在 Java 中使用 String.split("\\.\\s") 来完成。假设用户以句点结束一个句子,并以空格开始一个新句子。由于 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 接受数字而不是句子。就像“going”一词的词干是“go”,然后编号/索引为 2343 并按频率计数,它可能会弹出两次,因此 bow_corpus 将是 LDA 所期望的 (2343, 2)。
# 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)
【讨论】:
例如,您可以使用 OpenNLP 句子检测工具。它们已经存在了一段时间,并且在大多数情况下表现都不错。
文档是here,模型可以下载here。请注意,1.5 版模型与较新的 opennlp-tools 1.8.4 版完全兼容
如果您使用的是 Maven,只需将以下内容添加到您的 pom.xml 中即可。
<dependency>
<groupId>org.apache.opennlp</groupId>
<artifactId>opennlp-tools</artifactId>
<version>1.8.4</version>
</dependency>
如果您打算将模型输入从文档切换到句子,请注意,原版 LDA(这也会影响 Mallet 中的当前实现,afaik)可能不会产生令人满意的结果,因为单词共现计数在句子。
我建议调查一下段落级别是否更有趣。可以使用换行模式提取文档中的段落。例如,当您有两个连续的换行符时,新段落就会开始。
【讨论】: