2025-10-07 15:36:51 +02:00
import pandas as pd
2025-10-07 17:41:53 +02:00
from pathlib import Path
import Project_Model . Libs . BPE as BPE
#from BPE import TokeNanoCore as Tokenizer
from Scripts . Libs . CleaningPipeline . special_token import SpecialToken
2025-10-07 15:36:51 +02:00
class Batcher :
2025-10-07 17:41:53 +02:00
def __init__ ( self , dataset_path : str , batch_size : int , tokenizer : BPE . TokeNanoCore ) - > None :
2025-10-07 15:36:51 +02:00
# ABSTRACT, TRIPLE
# tasks:
# rdf2text: X: TRIPLE, Y: ABSTRACT
# text2rdf: X: ABSTRACT, X:TRIPLE
# masking ( call masker): X: incomplete_triple Y: complete_triple (as exam)
# completation: X: TRIPLE SUBSET, Y: related TRIPLE SUBSET
# self._DATASET = pd.read_csv(dataset_path)
self . _dataset_path = dataset_path
self . _batch_size = batch_size
self . _tokenizer = tokenizer
def get_batch ( self ) :
for batch in pd . read_csv ( self . _dataset_path , chunksize = int ( self . _batch_size / 3 ) ) : #now we support 3 task
# each batch get 4 transformation for the 4 tasks and then shuffled
# now a batch is ["Abstract"], ["Triples"]
# tokenize the strings:
2025-10-07 17:41:53 +02:00
# batch = batch.drop(columns=['MovieID'])
tokenized_batch = pd . DataFrame ( )
# bho = batch.map(lambda x: self._tokenizer.encode(x))
tokenized_batch [ [ " Abstract " , " RDFs " ] ] = batch [ [ " Abstract " , " RDFs " ] ] . map (
lambda t : self . _tokenizer . encode_incomplete_string ( t ) )
2025-10-07 15:36:51 +02:00
2025-10-07 17:41:53 +02:00
# ??? i hope this works, later will be tested
rdf2txt_batch = self . __rdf2txt_transformation ( tokenized_batch )
txt2rdf_batch = self . __txt2rdf_transformation ( tokenized_batch )
2025-10-07 15:36:51 +02:00
2025-10-07 17:41:53 +02:00
output = pd . concat ( [ rdf2txt_batch , txt2rdf_batch ] , ignore_index = True )
output . sample ( frac = 1 ) . reset_index ( drop = True )
yield output
2025-10-07 15:36:51 +02:00
2025-10-07 17:41:53 +02:00
def __random_subset_rdfs ( self , batch : pd . DataFrame ) :
batch [ " RDFs " ] = batch [ " RDFs " ] . map (
lambda x : x . split ( SpecialToken . START_TRIPLE . value ) [ 1 : ]
)
2025-10-07 15:36:51 +02:00
def __rdf2txt_transformation ( self , batch : pd . DataFrame ) :
# rename ["Triples"] as ["X"]
# rename ["Abstract"] as ["Y"]
# return just them
2025-10-07 17:41:53 +02:00
batch = batch . rename ( columns = { " RDFs " : " X " , " Abstract " : " Y " } )
2025-10-07 15:36:51 +02:00
return batch [ [ " X " , " Y " ] ] #.sample(frac=1).reset_index(drop=True)
def __txt2rdf_transformation ( self , batch : pd . DataFrame ) :
2025-10-07 17:41:53 +02:00
batch = batch . rename ( columns = { " Abstract " : " X " , " RDFs " : " Y " } )
2025-10-07 15:36:51 +02:00
return batch [ [ " X " , " Y " ] ] # .sample(frac=1).reset_index(drop=True)
2025-10-07 17:41:53 +02:00
#def __masking()
DATASET_PATH = " Assets/Dataset/Tmp/rdf_text.csv "
VOCABULARY_path = " Assets/Dataset/Tmp/trimmed.json "
VOCABULARY = BPE . load_nanos_vocabulary ( Path ( VOCABULARY_path ) )
SPECIAL_TOKEN_LIST = [ token . value for token in SpecialToken ]
TOKENANO = BPE . TokeNanoCore ( VOCABULARY , SPECIAL_TOKEN_LIST )
prova = " <ABS>Cactus Flower is a 1969 American screwball comedy film directed by Gene Saks, and starring Walter Matthau, Ingrid Bergman and Goldie Hawn, who won an Academy Award for her performance.The screenplay was adapted by I. A. L. Diamond from the 1965 Broadway play of the same title written by Abe Burrows, which, in turn, is based on the French play Fleur de cactus by Pierre Barillet and Jean-Pierre Gredy. Cactus Flower was the ninth highest-grossing film of 1969. "
print ( TOKENANO . encode ( prova ) )
batcher = Batcher ( DATASET_PATH , 3 , TOKENANO )
for batch in batcher . get_batch ( ) :
print ( batch )