forked from ttasovac/tei-pynotator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpynotate.py
More file actions
68 lines (55 loc) · 1.91 KB
/
Copy pathpynotate.py
File metadata and controls
68 lines (55 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /usr/local/bin/python2
import xml.dom.minidom as dom
from xml.dom.minidom import parse, parseString
import re
from nltk_helper import *
file = "TreasureIsland-excerpt.xml"
class Annotator:
def __init__(self, file):
self.file = file
self.xmlObj = dom.parse(self.file)
#print(self.xmlObj.toxml("utf-8"))
def printXML(self):
self.file = file
print(self.xmlObj.toxml("utf-8"))
def clean_up(self, parent):
#get rid of empty nodes which were created when removing original paragraphs
self.parent = parent
for child in list(self.parent.childNodes):
if child.nodeType == child.TEXT_NODE and child.data.strip()=="":
self.parent.removeChild(child)
def remove_whites(self, string):
#i may have to check for other string issues here
#apostrophes etc.
self.string = string
whites = re.compile(r"\s+")
sanitized = whites.sub(" ", self.string)
return sanitized
def get_paragraphs (self):
#returns a list of paragraph dom objects and its parent nodes
self.paragraphs = []
for text_node in self.xmlObj.getElementsByTagName("text"):
for paragraph in text_node.getElementsByTagName("p"):
parent = paragraph.parentNode
self.paragraphs.append([paragraph, parent])
return self.paragraphs
def edit_paragraphs (self):
self.pars = self.get_paragraphs()
for each in self.pars:
orig_p = each[0]
parent = each[1]
p_text = self.remove_whites(orig_p.firstChild.nodeValue)
#print(p_text.encode("utf-8"))
# send to NLTK
nltk_analyze = NLTK_Helper ()
analyzed_p = nltk_analyze.process(p_text)
#print analyzed_p.encode("utf-8")
xml_p = parseString("<p>"+analyzed_p.encode("utf-8")+"</p>").getElementsByTagName("p")[0]
# replace in xml
parent.removeChild(orig_p)
parent.appendChild(xml_p)
self.clean_up(parent)
#main body of the program
annotate = Annotator(file)
annotate.edit_paragraphs()
annotate.printXML()