text_plain_output_2.png text_plain_output_1.png
from sklearn.utils import shuffle

import pandas as pd

import random

import re

df = pd.read_csv('/kaggle/input/kullanlcaklar/Womens_Clothing_E-Commerce_Reviews_1.csv')

df = shuffle(df)
df = df.reset_index(drop=True)

N = 10
population_size = 100
mutation_rate = 0.3
generations = 200

text_column_name = 'Title'
rating_column_name = 'Rating'

def clean_text(text):
    text = text.lower()
    text = re.sub('[^\\w\\s]', '', text)
    return text
def tokenize(text):
    return text.split()

def prepare_population(df, text_column_name):
    all_words = []
    for text in df[text_column_name]:
        clean_review = clean_text(text)
        tokens = tokenize(clean_review)
        all_words.extend(tokens)
    random.shuffle(all_words)
    sublists = [all_words[i:i + N] for i in range(0, len(all_words), N)]
    population = sublists
    return (population, all_words)

def count_words(text, word_list):
    count = 0
    for word in word_list:
        count += text.count(word)
    return count

def fitness(individual):
    correct_classification = 0
    for i in range(len(df)):
        row = df.iloc[i]
        text, rating = (row[text_column_name], row[rating_column_name])
        pozitif = count_words(text, individual[:N // 2])
        negatif = count_words(text, individual[N // 2:])
        if pozitif == negatif:
            if random.random() < 0.5:
                classification = 1
            else:
                classification = 5
        elif pozitif > negatif:
            classification = 5
        else:
            classification = 1
        if classification == rating:
            correct_classification += 1
    return correct_classification / len(df)

def fitness2(individual):
    correct_classification = 0
    for i in range(len(df)):
        row = df.iloc[i]
        text, rating = (row[text_column_name], row[rating_column_name])
        pozitif = count_words(text, individual[:N // 2])
        negatif = count_words(text, individual[N // 2:])
        if pozitif == negatif:
            if random.random() < 0.5:
                classification = 'negative'
            else:
                classification = 'positive'
        elif pozitif > negatif:
            classification = 'positive'
        else:
            classification = 'negative'
        if classification == rating:
            correct_classification += 1
    return correct_classification / len(df)

population, all_words = prepare_population(df, text_column_name)
print(population)