haha

#1
by adarshxs - opened
import os
from datasets import Dataset
import pandas as pd
from tqdm import tqdm

class CorpusCreator:
    def __init__(self, arrow_dir, output_dir="./corpus", output_file_name="kannada_sentence_corpus.txt"):
        self.arrow_dir = arrow_dir
        self.output_dir = output_dir
        self.output_file_name = output_file_name

    def create_sentence_corpus(self, text_col):
        # Make sure the output directory exists
        os.makedirs(self.output_dir, exist_ok=True)
        corpus_path = os.path.join(self.output_dir, self.output_file_name)

        # Initialize an empty DataFrame to collect all texts
        full_df = pd.DataFrame()

        # Load each arrow file and concatenate the data into the full DataFrame
        for file in os.listdir(self.arrow_dir):
            if file.endswith(".arrow"):
                file_path = os.path.join(self.arrow_dir, file)
                # Load the dataset from an Arrow file
                dataset = Dataset.from_file(file_path)
                # Convert to pandas DataFrame
                df = dataset.to_pandas()
                # Concatenate to the full DataFrame
                full_df = pd.concat([full_df, df], ignore_index=True)

        with open(corpus_path, "w", encoding="utf-8") as file:
            for index, value in tqdm(full_df[text_col].iteritems(), total=len(full_df)):
                file.write(str(value) + "\n")
        print(f"Corpus created at {corpus_path}")

    def run(self):
        # You can call this method directly with the column name containing the text
        self.create_sentence_corpus(text_col="text") # Replace "text" with the actual text column name in your dataset


# Example usage:
arrow_dir = './train/'  # Replace with the actual path to the 'train' directory containing .arrow files
corpus_creator = CorpusCreator(arrow_dir)
corpus_creator.run()

Sign up or log in to comment