ieer.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Natural Language Toolkit: IEER Corpus Reader
  2. #
  3. # Copyright (C) 2001-2020 NLTK Project
  4. # Author: Steven Bird <stevenbird1@gmail.com>
  5. # Edward Loper <edloper@gmail.com>
  6. # URL: <http://nltk.org/>
  7. # For license information, see LICENSE.TXT
  8. """
  9. Corpus reader for the Information Extraction and Entity Recognition Corpus.
  10. NIST 1999 Information Extraction: Entity Recognition Evaluation
  11. http://www.itl.nist.gov/iad/894.01/tests/ie-er/er_99/er_99.htm
  12. This corpus contains the NEWSWIRE development test data for the
  13. NIST 1999 IE-ER Evaluation. The files were taken from the
  14. subdirectory: /ie_er_99/english/devtest/newswire/*.ref.nwt
  15. and filenames were shortened.
  16. The corpus contains the following files: APW_19980314, APW_19980424,
  17. APW_19980429, NYT_19980315, NYT_19980403, and NYT_19980407.
  18. """
  19. import nltk
  20. from nltk.corpus.reader.api import *
  21. #: A dictionary whose keys are the names of documents in this corpus;
  22. #: and whose values are descriptions of those documents' contents.
  23. titles = {
  24. "APW_19980314": "Associated Press Weekly, 14 March 1998",
  25. "APW_19980424": "Associated Press Weekly, 24 April 1998",
  26. "APW_19980429": "Associated Press Weekly, 29 April 1998",
  27. "NYT_19980315": "New York Times, 15 March 1998",
  28. "NYT_19980403": "New York Times, 3 April 1998",
  29. "NYT_19980407": "New York Times, 7 April 1998",
  30. }
  31. #: A list of all documents in this corpus.
  32. documents = sorted(titles)
  33. class IEERDocument(object):
  34. def __init__(self, text, docno=None, doctype=None, date_time=None, headline=""):
  35. self.text = text
  36. self.docno = docno
  37. self.doctype = doctype
  38. self.date_time = date_time
  39. self.headline = headline
  40. def __repr__(self):
  41. if self.headline:
  42. headline = " ".join(self.headline.leaves())
  43. else:
  44. headline = (
  45. " ".join([w for w in self.text.leaves() if w[:1] != "<"][:12]) + "..."
  46. )
  47. if self.docno is not None:
  48. return "<IEERDocument %s: %r>" % (self.docno, headline)
  49. else:
  50. return "<IEERDocument: %r>" % headline
  51. class IEERCorpusReader(CorpusReader):
  52. """
  53. """
  54. def raw(self, fileids=None):
  55. if fileids is None:
  56. fileids = self._fileids
  57. elif isinstance(fileids, str):
  58. fileids = [fileids]
  59. return concat([self.open(f).read() for f in fileids])
  60. def docs(self, fileids=None):
  61. return concat(
  62. [
  63. StreamBackedCorpusView(fileid, self._read_block, encoding=enc)
  64. for (fileid, enc) in self.abspaths(fileids, True)
  65. ]
  66. )
  67. def parsed_docs(self, fileids=None):
  68. return concat(
  69. [
  70. StreamBackedCorpusView(fileid, self._read_parsed_block, encoding=enc)
  71. for (fileid, enc) in self.abspaths(fileids, True)
  72. ]
  73. )
  74. def _read_parsed_block(self, stream):
  75. # TODO: figure out while empty documents are being returned
  76. return [
  77. self._parse(doc)
  78. for doc in self._read_block(stream)
  79. if self._parse(doc).docno is not None
  80. ]
  81. def _parse(self, doc):
  82. val = nltk.chunk.ieerstr2tree(doc, root_label="DOCUMENT")
  83. if isinstance(val, dict):
  84. return IEERDocument(**val)
  85. else:
  86. return IEERDocument(val)
  87. def _read_block(self, stream):
  88. out = []
  89. # Skip any preamble.
  90. while True:
  91. line = stream.readline()
  92. if not line:
  93. break
  94. if line.strip() == "<DOC>":
  95. break
  96. out.append(line)
  97. # Read the document
  98. while True:
  99. line = stream.readline()
  100. if not line:
  101. break
  102. out.append(line)
  103. if line.strip() == "</DOC>":
  104. break
  105. # Return the document
  106. return ["\n".join(out)]