1.9 加载你自己的语料库

如果你有自己收集的文本文件,并且想使用前面讨论的方法访问它们,你可以很容易地在 NLTK 中的PlaintextCorpusReader帮助下加载它们。检查你的文件在文件系统中的位置;在下面的例子中,我们假定你的文件在/usr/share/dict目录下。不管是什么位置,将变量corpus_root [1]的值设置为这个目录。PlaintextCorpusReader初始化函数[2]的第二个参数可以是一个如['a.txt', 'test/b.txt']这样的 fileids 列表,或者一个匹配所有 fileids 的模式,如'[abc]/.*\.txt'(关于正则表达式的信息见3.4节)。

  1. >>> from nltk.corpus import PlaintextCorpusReader
  2. >>> corpus_root = '/usr/share/dict' ![[1]](/projects/nlp-py-2e-zh/Images/eeff7ed83be48bf40aeeb3bf9db5550e.jpg)
  3. >>> wordlists = PlaintextCorpusReader(corpus_root, '.*') ![[2]](/projects/nlp-py-2e-zh/Images/6efeadf518b11a6441906b93844c2b19.jpg)
  4. >>> wordlists.fileids()
  5. ['README', 'connectives', 'propernames', 'web2', 'web2a', 'words']
  6. >>> wordlists.words('connectives')
  7. ['the', 'of', 'and', 'to', 'a', 'in', 'that', 'is', ...]

举另一个例子,假设你在本地硬盘上有自己的宾州树库(第 3 版)的拷贝,放在C:\corpora。我们可以使用BracketParseCorpusReader访问这些语料。我们指定corpus_root为存放语料库中解析过的《华尔街日报》部分[1]的位置,并指定file_pattern与它的子文件夹中包含的文件匹配[2](用前斜杠)。

  1. >>> from nltk.corpus import BracketParseCorpusReader
  2. >>> corpus_root = r"C:\corpora\penntreebank\parsed\mrg\wsj" ![[1]](/projects/nlp-py-2e-zh/Images/eeff7ed83be48bf40aeeb3bf9db5550e.jpg)
  3. >>> file_pattern = r".*/wsj_.*\.mrg" ![[2]](/projects/nlp-py-2e-zh/Images/6efeadf518b11a6441906b93844c2b19.jpg)
  4. >>> ptb = BracketParseCorpusReader(corpus_root, file_pattern)
  5. >>> ptb.fileids()
  6. ['00/wsj_0001.mrg', '00/wsj_0002.mrg', '00/wsj_0003.mrg', '00/wsj_0004.mrg', ...]
  7. >>> len(ptb.sents())
  8. 49208
  9. >>> ptb.sents(fileids='20/wsj_2013.mrg')[19]
  10. ['The', '55-year-old', 'Mr.', 'Noriega', 'is', "n't", 'as', 'smooth', 'as', 'the',
  11. 'shah', 'of', 'Iran', ',', 'as', 'well-born', 'as', 'Nicaragua', "'s", 'Anastasio',
  12. 'Somoza', ',', 'as', 'imperial', 'as', 'Ferdinand', 'Marcos', 'of', 'the', 'Philippines',
  13. 'or', 'as', 'bloody', 'as', 'Haiti', "'s", 'Baby', Doc', 'Duvalier', '.']