import csv import random import pandas as pd def remap_polarity(row): if row["label"] < 2: row["label"] = 0 else: row["label"] = 1 return row def run(): rows = [] with open( "training.1600000.processed.noemoticon.csv", "r", encoding="ISO-8859-1" ) as file: reader = csv.reader(file) for row in reader: rows.append({"label": int(row[0]), "text": row[5]}) df = pd.DataFrame(rows) df.to_csv("complete.csv", index=False) polarity_rows = [remap_polarity(row) for row in rows if row["label"] != 2] positive_rows = [row for row in polarity_rows if row["label"] == 1] negative_rows = [row for row in polarity_rows if row["label"] == 0] min_size = min(len(positive_rows), len(negative_rows)) polarity_rows = positive_rows[:min_size] + negative_rows[:min_size] random.shuffle(polarity_rows) df = pd.DataFrame(polarity_rows) df.to_csv("polarity.csv", index=False) if __name__ == "__main__": run()