artistypl commited on
Commit
60b8fb4
1 Parent(s): 05ebb09

Create chinese_text_splitter.py

Browse files
Files changed (1) hide show
  1. chinese_text_splitter.py +24 -0
chinese_text_splitter.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from typing import List
3
+
4
+ from langchain.text_splitter import CharacterTextSplitter
5
+
6
+
7
+ class ChineseTextSplitter(CharacterTextSplitter):
8
+ def __init__(self, pdf: bool = False, **kwargs):
9
+ super().__init__(**kwargs)
10
+ self.pdf = pdf
11
+
12
+ def split_text(self, text: str) -> List[str]:
13
+ if self.pdf:
14
+ text = re.sub(r"\n{3,}", "\n", text)
15
+ text = re.sub('\s', ' ', text)
16
+ text = text.replace("\n\n", "")
17
+ sent_sep_pattern = re.compile('([﹒﹔﹖﹗.。!?]["’”」』]{0,2}|(?=["‘“「『]{1,2}|$))') # del :;
18
+ sent_list = []
19
+ for ele in sent_sep_pattern.split(text):
20
+ if sent_sep_pattern.match(ele) and sent_list:
21
+ sent_list[-1] += ele
22
+ elif ele:
23
+ sent_list.append(ele)
24
+ return sent_list