-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab4fc49
commit 289aa31
Showing
6 changed files
with
56 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,54 @@ | ||
from pathlib import Path | ||
|
||
import pandas as pd | ||
from llama_index import Document | ||
from llama_index.node_parser.text import SentenceSplitter | ||
from llama_index.schema import TextNode | ||
from tqdm import tqdm | ||
|
||
from chat_doc.config import BASE_DIR, logger | ||
|
||
|
||
class DocumentProcessor: | ||
def __init__(self, loader, text_parser=SentenceSplitter(chunk_size=1024)): | ||
def __init__(self, loader, text_parser=SentenceSplitter()): | ||
self.loader = loader | ||
self.text_parser = text_parser | ||
|
||
def load_documents(self, file_path): | ||
return self.loader.load_data(file=Path(file_path)) | ||
|
||
def process_documents(self, documents=None): | ||
if not documents: | ||
documents = self.load_documents(file_path=Path(BASE_DIR + "/data/icd11.csv")) | ||
def process_documents(self, documents_df=None): | ||
if not documents_df: | ||
documents_df = pd.read_csv(Path(BASE_DIR + "/data/icd11.csv")) | ||
|
||
text_chunks = [] | ||
doc_idxs = [] | ||
for doc_idx, doc in enumerate(documents): | ||
cur_text_chunks = self.text_parser.split_text(doc.text) | ||
text_chunks.extend(cur_text_chunks) | ||
doc_idxs.extend([doc_idx] * len(cur_text_chunks)) | ||
def build_node(row): | ||
node = TextNode(text=row["definition"]) | ||
node.metadata = { | ||
"id": row["id"], | ||
"name": row["name"], | ||
} | ||
return node | ||
|
||
nodes = [] | ||
for idx, text_chunk in enumerate(text_chunks): | ||
node = TextNode(text=text_chunk) | ||
src_doc = documents[doc_idxs[idx]] | ||
node.metadata = src_doc.metadata | ||
nodes.append(node) | ||
|
||
for idx, row in tqdm(documents_df.iterrows(), total=len(documents_df)): | ||
nodes.append(build_node(row)) | ||
|
||
# text_chunks = [] | ||
# doc_idxs = [] | ||
# for doc_idx, doc in enumerate(documents): | ||
# cur_text_chunks = self.text_parser.split_text(doc.text) | ||
# text_chunks.extend(cur_text_chunks) | ||
# doc_idxs.extend([doc_idx] * len(cur_text_chunks)) | ||
|
||
# nodes = self.text_parser.split_text(list(map(lambda d: d.text[0], documents))) | ||
|
||
# nodes = [] | ||
# for raw_node in enumerate(nodes): | ||
# node = TextNode(text=raw_node) | ||
# # src_doc = documents[doc_idxs[idx]] | ||
# # node.metadata = src_doc.metadata | ||
# nodes.append(node) | ||
|
||
self.nodes = nodes | ||
return nodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters