如何使用nltk python 3.4创建一个类似movie_review的语料库



我遇到了一个问题,我会向你总结一下我想要完成的任务,这样你就能有一个清晰的画面来指导我。

我想创建一个语料库类似于movie_reviews,其中movie_review只有2个类别,但在我的情况下,我将有多个类别和子类别

例如:

说我有一个语料库my_corpus,我想在其中创建诸如"A"、"B"、"C"、"D"one_answers"E"等类别。每一个类别将包含子类别,例如我想要的"A"子类别,如'a1', 'a2', 'a3'等,等等所有其他类别(B, C, D和E)。每个子类别都将还有它自己的子类别比如'a1'可能有'a1.1' a1.2'等。最后最底部(叶)将包含所有的文本与该类别或子类别相关的文件

我的问题是

1比;我该如何创建这样的语料库,是否有这样的方法,请指导我您的回复将是一个很大的帮助,您也可以将我映射到可以帮助我做到这一点的链接

2比;我可以运行naivebayes算法或任何其他适用于这种情况的算法吗?就像在movie_reviews中查找negative和pos一样,在我的情况下,我也需要找到与哪个类别及其子类别相关的新问题,等等?

请帮帮我。

查看CategorizedCorpusReader的文档(和/或源代码),例如:

>>> help(nltk.corpus.reader.CategorizedCorpusReader.__init__)

这是基类;实际上,您将使用适合您的数据格式的分类阅读器。如果你的文件是纯文本的,那就是CategorizedPlaintextCorpusReader。在创建阅读器时,可以通过以下方式定义类别:从文件名中提取类别的正则表达式,通过给出类别的文件,或通过直接传递给构造函数的字典:

- cat_pattern: A regular expression pattern used to find the
  category for each file identifier.  The pattern will be
  applied to each file identifier, and the first matching
  group will be used as the category label for that file.
- cat_map: A dictionary, mapping from file identifiers to
  category labels.
- cat_file: The name of a file that contains the mapping
  from file identifiers to categories.  The argument
  ``cat_delimiter`` can be used to specify a delimiter.

不直接支持分层分类,但您可以自己安排,因为一个文件可以属于多个类别。例如,将文件donkey.txt分配给animalmammal。nltk的brown语料库具有属于多个类别的文件,因此您可以检查它的细节(它使用cat_file方法)。

nltk的系统将类别映射到文件id,而不是较低级别的类别。如果您按照我的建议进行设置,您将能够编写mycorpus.words(categories=["A", "B"])并从Aa1, Aa2等类别中的所有文件中获取单词。如果想公开类别层次结构,就必须自己编写代码。(例如,您可以使用方法hierarchy扩展reader类,该方法只返回类别树。)

最新更新