-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtext_cleaning_zh.py
94 lines (78 loc) · 3.21 KB
/
text_cleaning_zh.py
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
import re
from typing import List, Sequence, Dict, Any
from summa.syntactic_unit import SyntacticUnit
from baidunlp import ner_tags
DEBUG = 0
def insert_unit(target_list: List[SyntacticUnit], raw: List[str],
tokens: List[str], pidx: int, sidx: int) -> None:
target_list.append(
SyntacticUnit(
text="".join(raw),
token=" ".join(tokens),
index=sidx,
paragraph=pidx
)
)
def clean_text(text: str) -> str:
text = text.replace("\r", "")
text = re.sub(r"(\s)\1+", r"\1", text)
return text
def get_tokens(text):
text = clean_text(text)
return ner_tags(text.strip(), verbose=False)
def clean_and_cut_words(text: str, pos_tags: Sequence = ("noun", "verb"),
stopwords: Sequence = ("是", "有")) -> List[SyntacticUnit]:
tokens = get_tokens(text)
return cut_words(tokens, pos_tags, stopwords)
def cut_words(tokens: List[Dict[str, Any]], pos_tags: Sequence,
stopwords: Sequence) -> List[SyntacticUnit]:
results: List[SyntacticUnit] = []
paragraph_idx, word_idx = 0, 0
for token in tokens:
if token["item"] == "\n":
# Linebreak marks the end of a paragraph
paragraph_idx += 1
word_idx = 0
else:
# Usual token
if DEBUG:
print(token["item"], token["tag"], token.get("pos", ""))
if token["tag"] in pos_tags and (token["item"] not in stopwords):
insert_unit(results, [token["item"]],
[token["item"]], paragraph_idx, word_idx)
word_idx += 1
return results
def clean_and_cut_sentences(text: str, sentence_delimiter: str = "。!?;",
pos_tags: Sequence = ("noun", "verb"),
stopwords: Sequence = ("是",)) -> List[SyntacticUnit]:
tokens = get_tokens(text)
return cut_sentences(tokens, sentence_delimiter, pos_tags, stopwords)
def cut_sentences(tokens: List[Dict[str, Any]], sentence_delimiter: str,
pos_tags: Sequence, stopwords: Sequence) -> List[SyntacticUnit]:
results: List[SyntacticUnit] = []
paragraph_idx, sentence_idx = 0, 0
raw_text: List[str] = []
filtered: List[str] = []
for token in tokens:
if token["item"] in sentence_delimiter and raw_text:
# End of a sentence
raw_text.append(token["item"])
insert_unit(results, raw_text, filtered,
paragraph_idx, sentence_idx)
sentence_idx += 1
raw_text, filtered = [], []
elif token["item"] == "\n":
# Linebreak marks the end of a paragraph
if raw_text:
# is not right after a setence delimiter
insert_unit(results, raw_text, filtered,
paragraph_idx, sentence_idx)
raw_text, filtered = [], []
paragraph_idx += 1
sentence_idx = 0
else:
# Usual token
raw_text.append(token["item"])
if token["tag"] in pos_tags and (token["item"] not in stopwords):
filtered.append(" ".join(token["basic_words"]))
return results