Refactoring the EmailObject build process
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from ..object import KnowledgeObject
|
from ..object import KnowledgeObject, ObjectRelationStore
|
||||||
from ..data import ChunkList, ChunkListWriter
|
from ..data import ChunkList, ChunkListWriter
|
||||||
from ..object import ObjectType
|
from ..object import ObjectType
|
||||||
from .. import KnowledgeStore
|
from .. import KnowledgeStore
|
||||||
@@ -49,9 +49,16 @@ class DocumentObjectBuilder:
|
|||||||
self.text = text
|
self.text = text
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def build(self) -> DocumentObject:
|
def build(self, relation_store: ObjectRelationStore) -> DocumentObject:
|
||||||
chunk_list = KnowledgeStore().get_chunk_list_writer().create_chunk_list_from_text(
|
chunk_list = KnowledgeStore().get_chunk_list_writer().create_chunk_list_from_text(
|
||||||
self.text,
|
self.text,
|
||||||
1024 * 4,
|
1024 * 4,
|
||||||
)
|
)
|
||||||
return DocumentObject(self.meta, self.tags, chunk_list)
|
doc = DocumentObject(self.meta, self.tags, chunk_list)
|
||||||
|
doc_id = doc.calculate_id()
|
||||||
|
|
||||||
|
# Add relation to store
|
||||||
|
for chunk_id in chunk_list.chunk_list:
|
||||||
|
relation_store.add_relation(chunk_id, doc_id)
|
||||||
|
|
||||||
|
return doc
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
|
from .. import KnowledgeStore
|
||||||
from .rich_text_object import RichTextObject, RichTextObjectBuilder
|
from .rich_text_object import RichTextObject, RichTextObjectBuilder
|
||||||
from ..object import ObjectID, ObjectType, KnowledgeObject
|
from ..object import ObjectID, ObjectType, KnowledgeObject
|
||||||
from .document_object import DocumentObjectBuilder
|
from .document_object import DocumentObjectBuilder
|
||||||
from .image_object import ImageObjectBuilder
|
from .image_object import ImageObjectBuilder
|
||||||
from .video_object import VideoObjectBuilder
|
from .video_object import VideoObjectBuilder
|
||||||
from .rich_text_object import RichTextObjectBuilder
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
@@ -69,6 +69,11 @@ class EmailObjectBuilder:
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
def build(self) -> EmailObject:
|
def build(self) -> EmailObject:
|
||||||
|
|
||||||
|
# Just get the object store and relation store from global KnowledgeStore
|
||||||
|
store = KnowledgeStore().get_object_store()
|
||||||
|
relation = KnowledgeStore().get_relation_store()
|
||||||
|
|
||||||
# Read meta.json
|
# Read meta.json
|
||||||
meta = {}
|
meta = {}
|
||||||
meta_file = os.path.join(self.folder, "meta.json")
|
meta_file = os.path.join(self.folder, "meta.json")
|
||||||
@@ -89,8 +94,10 @@ class EmailObjectBuilder:
|
|||||||
with open(content_file, "r", encoding="utf-8") as f:
|
with open(content_file, "r", encoding="utf-8") as f:
|
||||||
text = f.read()
|
text = f.read()
|
||||||
|
|
||||||
document = DocumentObjectBuilder({}, {}, text).build()
|
document = DocumentObjectBuilder({}, {}, text).build(relation_store=relation)
|
||||||
documents = {"email.txt": document}
|
document_id = document.calculate_id()
|
||||||
|
store.put_object(document_id, document.encode())
|
||||||
|
documents = {"email.txt": document_id}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Failed to read email.txt {content_file} {e}")
|
logging.error(f"Failed to read email.txt {content_file} {e}")
|
||||||
else:
|
else:
|
||||||
@@ -106,7 +113,9 @@ class EmailObjectBuilder:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
image = ImageObjectBuilder({}, {}, image_path).build()
|
image = ImageObjectBuilder({}, {}, image_path).build()
|
||||||
images[image_file] = image
|
image_id = image.calculate_id()
|
||||||
|
store.put_object(image_id, image.encode())
|
||||||
|
images[image_file] = image_id
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Failed to read image file {image_path} {e}")
|
logging.error(f"Failed to read image file {image_path} {e}")
|
||||||
continue
|
continue
|
||||||
@@ -121,13 +130,31 @@ class EmailObjectBuilder:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
video = VideoObjectBuilder({}, {}, video_path).build()
|
video = VideoObjectBuilder({}, {}, video_path).build()
|
||||||
videos[video_file] = video
|
video_id = video.calculate_id()
|
||||||
|
store.put_object(video_id, video.encode())
|
||||||
|
videos[video_file] = video_id
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Failed to read video file {video_path} {e}")
|
logging.error(f"Failed to read video file {video_path} {e}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Create RichTextObject
|
# Create RichTextObject
|
||||||
rich_text = RichTextObject(images, videos, documents)
|
rich_text = RichTextObject(images, videos, documents)
|
||||||
|
rich_text_id = rich_text.calculate_id()
|
||||||
|
|
||||||
|
# build relations with rich_text
|
||||||
|
for image_id in images.values():
|
||||||
|
relation.add_relation(image_id, rich_text_id)
|
||||||
|
for video_id in videos.values():
|
||||||
|
relation.add_relation(video_id, rich_text_id)
|
||||||
|
for document_id in documents.values():
|
||||||
|
relation.add_relation(document_id, rich_text_id)
|
||||||
|
|
||||||
# Create EmailObject
|
# Create EmailObject
|
||||||
return EmailObject(meta, {}, rich_text)
|
email_object = EmailObject(meta, {}, rich_text)
|
||||||
|
email_object_id = email_object.calculate_id()
|
||||||
|
store.put_object(email_object_id, email_object.encode())
|
||||||
|
|
||||||
|
# build relations with email_object
|
||||||
|
relation.add_relation(rich_text_id, email_object_id)
|
||||||
|
|
||||||
|
return email_object
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class ChunkListWriter:
|
|||||||
|
|
||||||
def create_chunk_list_from_text(
|
def create_chunk_list_from_text(
|
||||||
self, text: str, chunk_max_words: int, separator_chars: str = ".,"
|
self, text: str, chunk_max_words: int, separator_chars: str = ".,"
|
||||||
) -> Tuple[List[ChunkID], HashValue]:
|
) -> ChunkList:
|
||||||
text_list = self._split_text_list(text, chunk_max_words, separator_chars)
|
text_list = self._split_text_list(text, chunk_max_words, separator_chars)
|
||||||
chunk_list = []
|
chunk_list = []
|
||||||
hash_obj = hashlib.sha256()
|
hash_obj = hashlib.sha256()
|
||||||
|
|||||||
Reference in New Issue
Block a user