-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc_tree_object.py
More file actions
144 lines (129 loc) · 5.2 KB
/
doc_tree_object.py
File metadata and controls
144 lines (129 loc) · 5.2 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# coding=utf-8
import string
from nltk.tokenize import sent_tokenize, word_tokenize
from collections import defaultdict
from lib_process import stemSentence,remove_stopwords,cleanSentence
from list_term_object import List_Term_Object
class Doc_Tree_Object:
T=None
title=None
size=None
depth2idx=None
used_content_field=None
temp_max=None
def __init__(self,article):
article=article.strip()
self.depth2idx={}
self.size=0
self.used_content_field='stemmed_contents'
self.T=self.docTree()
self.initializeNode(self.T,'ROOT',1)
self.createDocumentTree(self.T,article)
self.temp_max=0
def docTree(self):
return defaultdict(self.docTree)
def calcSectionDepth(self,line):
dep=0
for chr in line:
if chr=='=':
dep+=1
else:
break
return dep
def initializeNode(self,T,label,depth):
self.size+=1
p=self.size
#print ('create label=%s depth=%d'%(label,depth))
T[p]['stemmed_contents']=[]
T[p]['list_term_object']=None
T[p]['label']=label
T[p]['depth']=depth
T[p]['child']=[]
T[p]['father']=None
if depth not in self.depth2idx:
self.depth2idx[depth]=[]
self.depth2idx[depth].append(p)
return p
def createDocumentTree(self,T,article):
cur_node=1
for line in article.split('\n'):
depth=self.calcSectionDepth(line)
if line.startswith('=')==False or line.endswith('=')==False:
T[cur_node][self.used_content_field].append(line)
else:
# find new section
label=line.split('='*depth)[1]
new_node=self.initializeNode(T,label,depth)
if T[cur_node]['depth']>=depth:
while (cur_node is not None) and (T[cur_node]['depth']>=depth):
cur_node=T[cur_node]['father']
T[cur_node]['child'].append(new_node)
T[new_node]['father']=cur_node
cur_node=new_node
return T
def traverse(self,root=1):
T=self.T
print ('label=%s father=%s depth=%d'%(T[root]['label'],T[root]['father'],T[root]['depth']))
print ('child='+str(T[root]['child']))
print ('contents=%s'%(str(T[root][self.used_content_field])))
#print ('contents=%s'%(str(T[root]['contents']).encode('utf-8','ignore')))
print ('-----')
for c in T[root]['child']:
self.traverse(c)
def getSubTreePathContent(self,root,list_str_node,s):
T=self.T
str=' '.join(T[root][self.used_content_field])
st=s+' '+str
if len(T[root]['child'])==0:
list_str_node.append(st.strip())
return
for c in T[root]['child']:
self.getSubTreePathContent(c,list_str_node,st)
'''
def getSubTreePathContent(self,root):
T=self.T
str=' '.join(T[root][self.used_content_field])
list_str_path=[]
for c in T[root]['child']:
list_str_c=self.getSubTreePathContent(c)
list_str_path=[(str+' '+str_c).strip() for str_c in list_str_c]
return list_str_path
'''
def getSubTreeContent(self,root):
T=self.T
str=' '.join(T[root][self.used_content_field])
for c in T[root]['child']:
str=str+' '+self.getSubTreeContent(c)
return str.strip()
def getNodeByDepth(self,depth):
if depth in self.depth2idx:
return self.depth2idx[depth]
else:
return []
def getSubTreePathContentByDepth(self,depth,list_str_node):
# notice, output is an iterator
list_node=self.getNodeByDepth(depth)
list_temp=[]
for v in list_node:
s=''
self.getSubTreePathContent(v,list_str_node,s)
def getSubTreeContentByDepth(self,depth):
# notice, output is an iterator
list_node=self.getNodeByDepth(depth)
list_temp=[self.getSubTreeContent(v) for v in list_node]
return [item for item in list_temp if len(item)>10]
def getFormattedOutput(self):
list_section_content=[]
self.traverseAndOutput(1,1,list_section_content)
return '\n'.join(list_section_content)
def traverseAndOutput(self,v,depth,list_section_content):
label=self.T[v]['label']
temp_line=' '.join(self.T[v][self.used_content_field])
content=temp_line
if depth>1:
section=('%s%s%s\n%s'%('='*depth,label,'='*depth,content))
else:
section=('%s'%(content))
list_section_content.append(section)
for c in self.T[v]['child']:
self.traverseAndOutput(c,depth+1,list_section_content)