-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_struct.py
More file actions
249 lines (226 loc) · 7.68 KB
/
text_struct.py
File metadata and controls
249 lines (226 loc) · 7.68 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/python
# coding:utf8
"""
@author: Cong Yu
@time: 2020-07-11 18:16
"""
import os
import re
import time
import json
import pandas as pd
def match(sentence, conditions):
for condition in conditions:
flag = True
for condition_and in condition:
if condition_and["operate"] == "Match":
if re.search(condition_and["value"], sentence):
pass
else:
flag = False
break
elif condition_and["operate"] == "NotMatch":
if not re.search(condition_and["value"], sentence):
pass
else:
flag = False
break
elif condition_and["operate"] == "TextLenLess":
if len(sentence) < int(condition_and["value"]):
pass
else:
flag = False
break
elif condition_and["operate"] == "TextLenGreater":
if len(sentence) > int(condition_and["value"]):
pass
else:
flag = False
break
else:
print(f"operate [{condition_and['operate']}] is not allowed...")
flag = False
break
if flag and len(condition):
print(condition, sentence)
return condition
return None
class TextStructure:
def __init__(self, config):
self.chapter1 = config["chapter1"]
self.chapter2 = config["chapter2"]
self.chapter3 = config["chapter3"]
self.chapter4 = config["chapter4"]
self.chapter5 = config["chapter5"]
self.clear = config["clear"]
# 是否为 章节标题 (0-无 1-5 对应标题level)
self.is_chapter_level = 0
self.chapter_text = ""
self.now_chapters = [""] * 5
pass
def match_chapter(self, sentence, level=1):
if level == 1:
has_chapter = match(sentence, self.chapter1["conditions"])
if has_chapter:
self.is_chapter_level = 1
self.chapter_text = sentence
self.now_chapters = [sentence] + [""] * 4
return True
elif level == 2:
has_chapter = match(sentence, self.chapter2["conditions"])
if has_chapter:
self.is_chapter_level = 2
self.chapter_text = sentence
self.now_chapters = self.now_chapters[:1] + [sentence] + [""] * 3
return True
elif level == 3:
has_chapter = match(sentence, self.chapter3["conditions"])
if has_chapter:
self.is_chapter_level = 3
self.chapter_text = sentence
self.now_chapters = self.now_chapters[:2] + [sentence] + [""] * 2
return True
elif level == 4:
has_chapter = match(sentence, self.chapter4["conditions"])
if has_chapter:
self.is_chapter_level = 4
self.chapter_text = sentence
self.now_chapters = self.now_chapters[:3] + [sentence] + [""] * 1
return True
elif level == 5:
has_chapter = match(sentence, self.chapter5["conditions"])
if has_chapter:
self.is_chapter_level = 5
self.chapter_text = sentence
self.now_chapters = self.now_chapters[:4] + [sentence]
return True
# self.is_chapter_level = 0
# self.chapter_text = ""
return False
def parser(self, text):
structure_data = []
sentences = text.split("\n")
former_chapter_text = ""
former_chapters = [""] * 5
temp = []
for sentence in sentences:
if len(sentence.strip()) < 1:
continue
for i in range(1, 6):
flag = self.match_chapter(sentence, level=i)
if flag:
# print(flag, sentence)
break
# print(sentence, self.is_chapter_level)
if match(sentence, self.clear["conditions"]):
self.is_chapter_level = 0
if self.is_chapter_level > 0:
# print(self.is_chapter_level, self.chapter_text, sentence)
# print(self.now_chapters, sentence)
if former_chapter_text and self.chapter_text != former_chapter_text:
structure_data.append({
"chapter1": former_chapters[0],
"chapter2": former_chapters[1],
"chapter3": former_chapters[2],
"chapter4": former_chapters[3],
"chapter5": former_chapters[4],
"text": "\n".join(temp)
})
temp = [sentence]
else:
temp.append(sentence)
former_chapter_text = self.chapter_text
former_chapters = self.now_chapters
else:
if len(temp):
structure_data.append({
"chapter1": former_chapters[0],
"chapter2": former_chapters[1],
"chapter3": former_chapters[2],
"chapter4": former_chapters[3],
"chapter5": former_chapters[4],
"text": "\n".join(temp)
})
temp = []
if len(temp):
structure_data.append({
"chapter1": former_chapters[0],
"chapter2": former_chapters[1],
"chapter3": former_chapters[2],
"chapter4": former_chapters[3],
"chapter5": former_chapters[4],
"text": "\n".join(temp)
})
df = pd.DataFrame(structure_data)
df.to_csv("demo.csv")
# a = df.groupby(["chapter1", "chapter2"])
# for k, v in a:
# print("group:", k)
# print("values:", v.values)
pass
# conditions, 内圈与, 外圈或
config_ = {
"chapter1": {
"conditions": [
[{"value": "^第[一二三四五六七八九十]+章", "operate": "Match"},
{"value": "50", "operate": "TextLenLess"},
],
[{"value": "^[一二三四五六七八九十]+、", "operate": "Match"},
{"value": "50", "operate": "TextLenLess"},
],
]
},
"chapter2": {
"conditions": [
# [{"value": "^([一二三四五六七八九十]+)", "operate": "Match"}
# ],
[{"value": "^第[一二三四五六七八九十]+条", "operate": "Match"}
],
]
},
"chapter3": {
"conditions": [[]]
},
"chapter4": {
"conditions": [[]]
},
"chapter5": {
"conditions": [[]]
},
"clear": {
"conditions": [
[{"value": "^(附件|附则)", "operate": "Match"},
{"value": "50", "operate": "TextLenLess"},
],
[{"value": "^.{0,6}年.{0,3}年.{0,3}日?", "operate": "Match"},
{"value": "15", "operate": "TextLenLess"},
],
[{"value": "(部|局)$", "operate": "Match"},
{"value": "10", "operate": "TextLenLess"},
],
]
},
}
text_ = "第一章总则"
print(match(text_, config_["chapter1"]["conditions"]))
ts = TextStructure(config_)
doc_ = """
xxxxx
xxxxxxx
xxxxx
第一章 1
第一条 11
111111
第二条 12
112121212
第三条 13
第四条 14
第五条 15
第六条 16
第二章 2
第七条 21
123123123123
第八条 22
附则
"""
ts.parser(doc_)