-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_to_doc.py
72 lines (52 loc) · 1.85 KB
/
text_to_doc.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
import re
from langchain.text_splitter import MarkdownTextSplitter
from langchain.docstore.document import Document
# Data Cleaning functions
def merge_hyphenated_words(text):
return re.sub(r"(\w)-\n(\w)", r"\1\2", text)
def fix_newlines(text):
return re.sub(r"(?<!\n)\n(?!\n)", " ", text)
def remove_multiple_newlines(text):
return re.sub(r"\n{2,}", "\n", text)
def clean_text(text):
"""
Cleans the text by passing it through a list of cleaning functions.
Args:
text (str): Text to be cleaned
Returns:
str: Cleaned text
"""
cleaning_functions = [merge_hyphenated_words, fix_newlines, remove_multiple_newlines]
for cleaning_function in cleaning_functions:
text = cleaning_function(text)
return text
def text_to_docs(text, metadata):
"""
Converts input text to a list of Documents with metadata.
Args:
text (str): A string of text.
metadata (dict): A dictionary containing the metadata.
Returns:
List[Document]: List of documents.
"""
doc_chunks = []
text_splitter = MarkdownTextSplitter(chunk_size=2048, chunk_overlap=128)
chunks = text_splitter.split_text(text)
for i, chunk in enumerate(chunks):
doc = Document(page_content=chunk, metadata=metadata)
doc_chunks.append(doc)
return doc_chunks
def get_doc_chunks(text, metadata):
"""
Processes the input text and metadata to generate document chunks.
This function takes the raw text content and associated metadata, cleans the text,
and divides it into document chunks.
Args:
text (str): The raw text content to be processed.
metadata (dict): Metadata associated with the text content.
Returns:
List[Document]: List of documents.
"""
text = clean_text(text)
doc_chunks = text_to_docs(text, metadata)
return doc_chunks