本文主要是用ParagraphVectors方法做文档分类,训练数据有一些带类别的文档,预测没有类别的文档属于哪个类别。这里简单说下ParagraphVectors模型,每篇文档映射在一个唯一的向量上,由矩阵中的一列表示,每个word则类似的被映射到向量上,这个向量由另一个矩阵的列表示。使用连接方式获得新word的预测,可以说ParagraphVectors是在word2vec基础上加了一组paragraph输入列向量一起训练构成的模型public class ParagraphVectorsClassifierExample { ParagraphVectors paragraphVectors;//声明ParagraphVectors类
LabelAwareIterator iterator;//声明要实现的迭代器接口,用来识别句子或文档及标签,这里假定所有的文档已变成字符串或词表的形式 TokenizerFactory tokenizerFactory;//声明字符串分割器 private static final Logger log = LoggerFactory.getLogger(ParagraphVectorsClassifierExample.class); public static void main(String[] args) throws Exception { ParagraphVectorsClassifierExample app = new ParagraphVectorsClassifierExample();//又是这种写法,构建实现类 app.makeParagraphVectors();//调用构建模型方法 app.checkUnlabeledData();//检查标签数据 /* Your output should be like this: Document 'health' falls into the following categories: health: 0.29721372296220205 science: 0.011684473733853906 finance: -0.14755302887323793 Document 'finance' falls into the following categories: health: -0.17290237675941766 science: -0.09579267574606627 finance: 0.4460859189453788 so,now we know categories for yet unseen documents */ } void makeParagraphVectors() throws Exception { ClassPathResource resource = new ClassPathResource("paravec/labeled");//弄一个带标签的文档路径