我如何在名称,数字,金钱,日期等内容中对文字进行分类?

我在一周前做了一些关于文本挖掘的问题,但我有点困惑,但现在我知道我想做什么。

情况:我有很多带有HTML内容的下载页面。 例如,其中一些可以是来自博客的文本。 它们不是结构化的,来自不同的网站。

我想做什么:我将用空白分割所有单词,我想在一些预先定义的内容中对每个单词或一组单词进行分类,如姓名,号码,电话,电子邮件,url,日期,金钱,温度等。

我所知道的:我知道有关自然语言处理,命名实体重新接收器,POSTagging,NayveBayesian,HMM,培训和许多要做分类的事情的概念/听说过,但是有一些不同的NLP库有不同的分类器和这样做的方法,我不知道有什么用途或做什么。

我需要什么:我需要一些来自分类器,NLP的代码示例,无论如何,它可以对文本中的每个单词进行分类,而不是整个文本。 像这样的东西:

//This is pseudo-code for what I want, and not a implementation classifier.trainFromFile("file-with-train-words.txt"); words = text.split(" "); for(String word: words){ classifiedWord = classifier.classify(word); System.out.println(classifiedWord.getType()); } 

有人可以帮帮我吗? 我对各种API,分类器和算法感到困惑。

你应该尝试Apache OpenNLP 。 它易于使用和定制。

如果你是为葡萄牙语做的,那么有关如何使用Amazonia Corpus对项目文档进行操作的信息。 支持的类型有:

人,组织,集团,地方,事件,ArtProd,抽象,事,时间和数字。

  1. 下载OpenNLP和亚马逊语料库 。 解压缩并将文件amazonia.ad复制到apache-opennlp-1.5.1-incubating文件夹。

  2. 执行TokenNameFinderConverter工具将Amazonia语料库转换为OpenNLP格式:

     bin/opennlp TokenNameFinderConverter ad -encoding ISO-8859-1 -data amazonia.ad -lang pt > corpus.txt 
  3. 训练模型(将编码更改为corpus.txt文件的编码,应该是您的系统默认编码。此命令可能需要几分钟):

     bin/opennlp TokenNameFinderTrainer -lang pt -encoding UTF-8 -data corpus.txt -model pt-ner.bin -cutoff 20 
  4. 从命令行执行它(你应该只执行一个句子,并且标记应该分开):

     $ bin/opennlp TokenNameFinder pt-ner.bin Loading Token Name Finder model ... done (1,112s) Meu nome é João da Silva , moro no Brasil . Trabalho na Petrobras e tenho 50 anos . Meu nome é  João da Silva  , moro no  Brasil  .  Trabalho  na  Petrobras  e tenho  50 anos  . 
  5. 使用API​​执行它:

     InputStream modelIn = new FileInputStream("pt-ner.bin"); try { TokenNameFinderModel model = new TokenNameFinderModel(modelIn); } catch (IOException e) { e.printStackTrace(); } finally { if (modelIn != null) { try { modelIn.close(); } catch (IOException e) { } } } // load the name finder NameFinderME nameFinder = new NameFinderME(model); // pass the token array to the name finder String[] toks = {"Meu","nome","é","João","da","Silva",",","moro","no","Brasil",".","Trabalho","na","Petrobras","e","tenho","50","anos","."}; // the Span objects will show the start and end of each name, also the type Span[] nameSpans = nameFinder.find(toks); 
  6. 要评估您的模型,您可以使用10倍交叉validation:(仅适用于1.5.2-INCUBATOR,今天使用它需要使用SVN中继线)(可能需要几个小时)

     bin/opennlp TokenNameFinderCrossValidator -lang pt -encoding UTF-8 -data corpus.txt -cutoff 20 
  7. 使用自定义要素生成(检查文档)提高精度/召回率,例如添加名称词典。

您可以使用命名实体识别器(NER)方法完成此任务,我强烈建议您查看Stanford Core NLP页面并使用模块中的nerfunction来完成您的任务。 你可以将你的句子分解为令牌,然后将它们传递给斯坦福NER系统。 我认为Stanford Core NLP页面有很多可以帮助你的例子,如果你需要玩具代码,请告诉我。

这里是示例代码,这只是整个代码的片段:

 // creates a StanfordCoreNLP object, with NER Properties props = new Properties(); props.put("annotators", "ner"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); classifier.trainFromFile("file-with-train-words.txt"); words = text.split(" "); for(String word: words){ Annotation document = new Annotation(word); pipeline.annotate(document); System.out.println(Annotation); } 

这个问题属于来自不同领域的几个想法的交叉点。 你提到命名实体识别,即一个。 但是,您可能正在考虑部分语音标记 (用于名词,名称等)和信息提取 (用于数字,电话号码,电子邮件)。

不幸的是,这样做并使其在实际工作数据上工作将需要一些努力,并且它不像使用这个或那个API那么简单。

您必须创建特定的函数来提取和检测每种数据类型及其错误。

或者以其众所周知的名称对象为导向。 即检测货币我们所做的是检查开头或结尾的美元符号,并检查是否附加了非数字字符,这意味着错误。

你应该写下你已经做过的事情。 如果你遵守这些规则并不难。 Robotics / AI中有3条黄金法则:

  1. 分析它。
  2. 简化它
  3. 数字化它。

这样你就可以与电脑交谈了。