mte.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. """
  2. A reader for corpora whose documents are in MTE format.
  3. """
  4. import os
  5. import re
  6. from functools import reduce
  7. from nltk.corpus.reader import concat, TaggedCorpusReader
  8. from nltk.corpus.reader.xmldocs import XMLCorpusView
  9. def xpath(root, path, ns):
  10. return root.findall(path, ns)
  11. class MTECorpusView(XMLCorpusView):
  12. """
  13. Class for lazy viewing the MTE Corpus.
  14. """
  15. def __init__(self, fileid, tagspec, elt_handler=None):
  16. XMLCorpusView.__init__(self, fileid, tagspec, elt_handler)
  17. def read_block(self, stream, tagspec=None, elt_handler=None):
  18. return list(
  19. filter(
  20. lambda x: x is not None,
  21. XMLCorpusView.read_block(self, stream, tagspec, elt_handler),
  22. )
  23. )
  24. class MTEFileReader:
  25. """
  26. Class for loading the content of the multext-east corpus. It
  27. parses the xml files and does some tag-filtering depending on the
  28. given method parameters.
  29. """
  30. ns = {
  31. "tei": "http://www.tei-c.org/ns/1.0",
  32. "xml": "http://www.w3.org/XML/1998/namespace",
  33. }
  34. tag_ns = "{http://www.tei-c.org/ns/1.0}"
  35. xml_ns = "{http://www.w3.org/XML/1998/namespace}"
  36. word_path = "TEI/text/body/div/div/p/s/(w|c)"
  37. sent_path = "TEI/text/body/div/div/p/s"
  38. para_path = "TEI/text/body/div/div/p"
  39. def __init__(self, file_path):
  40. self.__file_path = file_path
  41. @classmethod
  42. def _word_elt(cls, elt, context):
  43. return elt.text
  44. @classmethod
  45. def _sent_elt(cls, elt, context):
  46. return [cls._word_elt(w, None) for w in xpath(elt, "*", cls.ns)]
  47. @classmethod
  48. def _para_elt(cls, elt, context):
  49. return [cls._sent_elt(s, None) for s in xpath(elt, "*", cls.ns)]
  50. @classmethod
  51. def _tagged_word_elt(cls, elt, context):
  52. if "ana" not in elt.attrib:
  53. return (elt.text, "")
  54. if cls.__tags == "" and cls.__tagset == "msd":
  55. return (elt.text, elt.attrib["ana"])
  56. elif cls.__tags == "" and cls.__tagset == "universal":
  57. return (elt.text, MTETagConverter.msd_to_universal(elt.attrib["ana"]))
  58. else:
  59. tags = re.compile("^" + re.sub("-", ".", cls.__tags) + ".*$")
  60. if tags.match(elt.attrib["ana"]):
  61. if cls.__tagset == "msd":
  62. return (elt.text, elt.attrib["ana"])
  63. else:
  64. return (
  65. elt.text,
  66. MTETagConverter.msd_to_universal(elt.attrib["ana"]),
  67. )
  68. else:
  69. return None
  70. @classmethod
  71. def _tagged_sent_elt(cls, elt, context):
  72. return list(
  73. filter(
  74. lambda x: x is not None,
  75. [cls._tagged_word_elt(w, None) for w in xpath(elt, "*", cls.ns)],
  76. )
  77. )
  78. @classmethod
  79. def _tagged_para_elt(cls, elt, context):
  80. return list(
  81. filter(
  82. lambda x: x is not None,
  83. [cls._tagged_sent_elt(s, None) for s in xpath(elt, "*", cls.ns)],
  84. )
  85. )
  86. @classmethod
  87. def _lemma_word_elt(cls, elt, context):
  88. if "lemma" not in elt.attrib:
  89. return (elt.text, "")
  90. else:
  91. return (elt.text, elt.attrib["lemma"])
  92. @classmethod
  93. def _lemma_sent_elt(cls, elt, context):
  94. return [cls._lemma_word_elt(w, None) for w in xpath(elt, "*", cls.ns)]
  95. @classmethod
  96. def _lemma_para_elt(cls, elt, context):
  97. return [cls._lemma_sent_elt(s, None) for s in xpath(elt, "*", cls.ns)]
  98. def words(self):
  99. return MTECorpusView(
  100. self.__file_path, MTEFileReader.word_path, MTEFileReader._word_elt
  101. )
  102. def sents(self):
  103. return MTECorpusView(
  104. self.__file_path, MTEFileReader.sent_path, MTEFileReader._sent_elt
  105. )
  106. def paras(self):
  107. return MTECorpusView(
  108. self.__file_path, MTEFileReader.para_path, MTEFileReader._para_elt
  109. )
  110. def lemma_words(self):
  111. return MTECorpusView(
  112. self.__file_path, MTEFileReader.word_path, MTEFileReader._lemma_word_elt
  113. )
  114. def tagged_words(self, tagset, tags):
  115. MTEFileReader.__tagset = tagset
  116. MTEFileReader.__tags = tags
  117. return MTECorpusView(
  118. self.__file_path, MTEFileReader.word_path, MTEFileReader._tagged_word_elt
  119. )
  120. def lemma_sents(self):
  121. return MTECorpusView(
  122. self.__file_path, MTEFileReader.sent_path, MTEFileReader._lemma_sent_elt
  123. )
  124. def tagged_sents(self, tagset, tags):
  125. MTEFileReader.__tagset = tagset
  126. MTEFileReader.__tags = tags
  127. return MTECorpusView(
  128. self.__file_path, MTEFileReader.sent_path, MTEFileReader._tagged_sent_elt
  129. )
  130. def lemma_paras(self):
  131. return MTECorpusView(
  132. self.__file_path, MTEFileReader.para_path, MTEFileReader._lemma_para_elt
  133. )
  134. def tagged_paras(self, tagset, tags):
  135. MTEFileReader.__tagset = tagset
  136. MTEFileReader.__tags = tags
  137. return MTECorpusView(
  138. self.__file_path, MTEFileReader.para_path, MTEFileReader._tagged_para_elt
  139. )
  140. class MTETagConverter:
  141. """
  142. Class for converting msd tags to universal tags, more conversion
  143. options are currently not implemented.
  144. """
  145. mapping_msd_universal = {
  146. "A": "ADJ",
  147. "S": "ADP",
  148. "R": "ADV",
  149. "C": "CONJ",
  150. "D": "DET",
  151. "N": "NOUN",
  152. "M": "NUM",
  153. "Q": "PRT",
  154. "P": "PRON",
  155. "V": "VERB",
  156. ".": ".",
  157. "-": "X",
  158. }
  159. @staticmethod
  160. def msd_to_universal(tag):
  161. """
  162. This function converts the annotation from the Multex-East to the universal tagset
  163. as described in Chapter 5 of the NLTK-Book
  164. Unknown Tags will be mapped to X. Punctuation marks are not supported in MSD tags, so
  165. """
  166. indicator = tag[0] if not tag[0] == "#" else tag[1]
  167. if not indicator in MTETagConverter.mapping_msd_universal:
  168. indicator = "-"
  169. return MTETagConverter.mapping_msd_universal[indicator]
  170. class MTECorpusReader(TaggedCorpusReader):
  171. """
  172. Reader for corpora following the TEI-p5 xml scheme, such as MULTEXT-East.
  173. MULTEXT-East contains part-of-speech-tagged words with a quite precise tagging
  174. scheme. These tags can be converted to the Universal tagset
  175. """
  176. def __init__(self, root=None, fileids=None, encoding="utf8"):
  177. """
  178. Construct a new MTECorpusreader for a set of documents
  179. located at the given root directory. Example usage:
  180. >>> root = '/...path to corpus.../'
  181. >>> reader = MTECorpusReader(root, 'oana-*.xml', 'utf8') # doctest: +SKIP
  182. :param root: The root directory for this corpus. (default points to location in multext config file)
  183. :param fileids: A list or regexp specifying the fileids in this corpus. (default is oana-en.xml)
  184. :param enconding: The encoding of the given files (default is utf8)
  185. """
  186. TaggedCorpusReader.__init__(self, root, fileids, encoding)
  187. def __fileids(self, fileids):
  188. if fileids is None:
  189. fileids = self._fileids
  190. elif isinstance(fileids, str):
  191. fileids = [fileids]
  192. # filter wrong userinput
  193. fileids = filter(lambda x: x in self._fileids, fileids)
  194. # filter multext-east sourcefiles that are not compatible to the teip5 specification
  195. fileids = filter(lambda x: x not in ["oana-bg.xml", "oana-mk.xml"], fileids)
  196. if not fileids:
  197. print("No valid multext-east file specified")
  198. return fileids
  199. def readme(self):
  200. """
  201. Prints some information about this corpus.
  202. :return: the content of the attached README file
  203. :rtype: str
  204. """
  205. return self.open("00README.txt").read()
  206. def raw(self, fileids=None):
  207. """
  208. :param fileids: A list specifying the fileids that should be used.
  209. :return: the given file(s) as a single string.
  210. :rtype: str
  211. """
  212. return reduce([self.open(f).read() for f in self.__fileids(fileids)], [])
  213. def words(self, fileids=None):
  214. """
  215. :param fileids: A list specifying the fileids that should be used.
  216. :return: the given file(s) as a list of words and punctuation symbols.
  217. :rtype: list(str)
  218. """
  219. return concat(
  220. [
  221. MTEFileReader(os.path.join(self._root, f)).words()
  222. for f in self.__fileids(fileids)
  223. ]
  224. )
  225. def sents(self, fileids=None):
  226. """
  227. :param fileids: A list specifying the fileids that should be used.
  228. :return: the given file(s) as a list of sentences or utterances,
  229. each encoded as a list of word strings
  230. :rtype: list(list(str))
  231. """
  232. return concat(
  233. [
  234. MTEFileReader(os.path.join(self._root, f)).sents()
  235. for f in self.__fileids(fileids)
  236. ]
  237. )
  238. def paras(self, fileids=None):
  239. """
  240. :param fileids: A list specifying the fileids that should be used.
  241. :return: the given file(s) as a list of paragraphs, each encoded as a list
  242. of sentences, which are in turn encoded as lists of word string
  243. :rtype: list(list(list(str)))
  244. """
  245. return concat(
  246. [
  247. MTEFileReader(os.path.join(self._root, f)).paras()
  248. for f in self.__fileids(fileids)
  249. ]
  250. )
  251. def lemma_words(self, fileids=None):
  252. """
  253. :param fileids: A list specifying the fileids that should be used.
  254. :return: the given file(s) as a list of words, the corresponding lemmas
  255. and punctuation symbols, encoded as tuples (word, lemma)
  256. :rtype: list(tuple(str,str))
  257. """
  258. return concat(
  259. [
  260. MTEFileReader(os.path.join(self._root, f)).lemma_words()
  261. for f in self.__fileids(fileids)
  262. ]
  263. )
  264. def tagged_words(self, fileids=None, tagset="msd", tags=""):
  265. """
  266. :param fileids: A list specifying the fileids that should be used.
  267. :param tagset: The tagset that should be used in the returned object,
  268. either "universal" or "msd", "msd" is the default
  269. :param tags: An MSD Tag that is used to filter all parts of the used corpus
  270. that are not more precise or at least equal to the given tag
  271. :return: the given file(s) as a list of tagged words and punctuation symbols
  272. encoded as tuples (word, tag)
  273. :rtype: list(tuple(str, str))
  274. """
  275. if tagset == "universal" or tagset == "msd":
  276. return concat(
  277. [
  278. MTEFileReader(os.path.join(self._root, f)).tagged_words(
  279. tagset, tags
  280. )
  281. for f in self.__fileids(fileids)
  282. ]
  283. )
  284. else:
  285. print("Unknown tagset specified.")
  286. def lemma_sents(self, fileids=None):
  287. """
  288. :param fileids: A list specifying the fileids that should be used.
  289. :return: the given file(s) as a list of sentences or utterances, each
  290. encoded as a list of tuples of the word and the corresponding
  291. lemma (word, lemma)
  292. :rtype: list(list(tuple(str, str)))
  293. """
  294. return concat(
  295. [
  296. MTEFileReader(os.path.join(self._root, f)).lemma_sents()
  297. for f in self.__fileids(fileids)
  298. ]
  299. )
  300. def tagged_sents(self, fileids=None, tagset="msd", tags=""):
  301. """
  302. :param fileids: A list specifying the fileids that should be used.
  303. :param tagset: The tagset that should be used in the returned object,
  304. either "universal" or "msd", "msd" is the default
  305. :param tags: An MSD Tag that is used to filter all parts of the used corpus
  306. that are not more precise or at least equal to the given tag
  307. :return: the given file(s) as a list of sentences or utterances, each
  308. each encoded as a list of (word,tag) tuples
  309. :rtype: list(list(tuple(str, str)))
  310. """
  311. if tagset == "universal" or tagset == "msd":
  312. return concat(
  313. [
  314. MTEFileReader(os.path.join(self._root, f)).tagged_sents(
  315. tagset, tags
  316. )
  317. for f in self.__fileids(fileids)
  318. ]
  319. )
  320. else:
  321. print("Unknown tagset specified.")
  322. def lemma_paras(self, fileids=None):
  323. """
  324. :param fileids: A list specifying the fileids that should be used.
  325. :return: the given file(s) as a list of paragraphs, each encoded as a
  326. list of sentences, which are in turn encoded as a list of
  327. tuples of the word and the corresponding lemma (word, lemma)
  328. :rtype: list(List(List(tuple(str, str))))
  329. """
  330. return concat(
  331. [
  332. MTEFileReader(os.path.join(self._root, f)).lemma_paras()
  333. for f in self.__fileids(fileids)
  334. ]
  335. )
  336. def tagged_paras(self, fileids=None, tagset="msd", tags=""):
  337. """
  338. :param fileids: A list specifying the fileids that should be used.
  339. :param tagset: The tagset that should be used in the returned object,
  340. either "universal" or "msd", "msd" is the default
  341. :param tags: An MSD Tag that is used to filter all parts of the used corpus
  342. that are not more precise or at least equal to the given tag
  343. :return: the given file(s) as a list of paragraphs, each encoded as a
  344. list of sentences, which are in turn encoded as a list
  345. of (word,tag) tuples
  346. :rtype: list(list(list(tuple(str, str))))
  347. """
  348. if tagset == "universal" or tagset == "msd":
  349. return concat(
  350. [
  351. MTEFileReader(os.path.join(self._root, f)).tagged_paras(
  352. tagset, tags
  353. )
  354. for f in self.__fileids(fileids)
  355. ]
  356. )
  357. else:
  358. print("Unknown tagset specified.")