import json
import jieba
import pandas as pd
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score
import torch
from torch.utils.data import Dataset, DataLoader
from torch.nn.utils.rnn import pad_sequence

def load_vocab(vocab_filepath):
    with open(vocab_filepath, 'r') as file:
        vocab = file.read()
        vocab = json.loads(vocab)
    return vocab


def load_data(filepath):
    # 读取文件内容
    train_data = pd.read_csv(filepath)
    # 提取文本列
    train_sentence = train_data['sentence']
    # train_label = train_data['label']
    return train_data

class Lang:
    def __init__(self, name):
        self.name = name
        self.word2index = {}
        self.word2count = {}
        self.index2word = {0: "好评", 1: "差评"}
        self.n_words = 2  # Count SOS and EOS



# 将文本先分词，再从字典里找到key值所对应的value值，转化为序列
def text_to_sequences(vocab, row):
    # print(f"text:{text}”")
    tokens = jieba.lcut(row["sentence"])
    # print(f"jieba.cut token:{tokens}")
    sequences = []
    for token in tokens:
        if token in vocab:
            # print(f"token:{token} ---> Index:{vocab[token]}")
            sequences.append(vocab[token])
    return sequences, row["label"]
class RNN(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.embeddings = torch.nn.Embedding(24000, 200)
        self.rnn = torch.nn.RNN(input_size=200, hidden_size=10)
        self.dense = torch.nn.Linear(in_features=9000, out_features=2)
        self.flatten = torch.nn.Flatten()
        self.softmax = torch.nn.Softmax()

    def forward(self, x):
        output = self.embeddings(x)
        output, hidden = self.rnn(output)
        output = self.flatten(output)
        output = self.dense(output)
        output = self.softmax(output)
        return output

class TextDataset(Dataset):
    def __init__(self, data, vocab):
        self.data = data
        self.vocab = vocab

    def __len__(self):
        return len(self.data)

    def __getitem__(self, index):
        row = self.data[index]
        sequences, label = text_to_sequences(self.vocab, row)
        return torch.tensor(sequences, dtype=torch.long), torch.tensor(label, dtype=torch.long)

def collate_fn(batch):
    sequences, labels = zip(*batch)
    sequences_padded = pad_sequence(sequences, batch_first=True, padding_value=0)
    labels = torch.tensor(labels, dtype=torch.long)
    return sequences_padded, labels






if __name__ == '__main__':
    vocab_filepath = "./vocab.json"
    vocab = load_vocab(vocab_filepath)

    filepath = '/Users/adila/PycharmProjects/Textclassification/data/cn_data/train.csv'
    data = load_data(filepath).to_dict('records')

    # 创建Dataset
    dataset = TextDataset(data,vocab)

    # 创建DataLoader，使用collate_fn进行填充
    data_loader = DataLoader(dataset, batch_size=32, shuffle=True, collate_fn=collate_fn)

    # sequences = [text_to_sequences(vocab, row) for row in data.to_dict("records")]
    # extracted_lists = [item[0] for item in sequences]
    # extracted_loabel = [item[1] for item in sequences]


    model = RNN()
    # inputs = torch.tensor([1,200])
    #
    # X = torch.tensor(extracted_lists)
    #
    # y = torch.tensor(extracted_loabel)

    loss_fn = torch.nn.MSELoss()
    optimizer = torch.optim.Adam(lr=1e-5, params=model.parameters())

    for epoch in range(100):
        for X_batch, y_batch in data_loader:
            optimizer.zero_grad()
            outputs = model(X_batch)
            y_batch = y_batch.squeeze()
            loss = loss_fn(outputs, y_batch)
            loss.backward()
            optimizer.step()
            print(loss.item())

    for epoch in range(1):
        for X_batch, y_batch in data_loader:
            outputs = model(X_batch)
            predict = outputs.argmax(dim=1)
            loss = loss_fn(outputs, y_batch.float())  # 根据模型输出和loss函数的要求，可能需要调整y_batch的类型
            print(f"Eval loss:{loss}")
            acc = accuracy_score(y_batch.numpy(), predict.numpy())
            f1 = f1_score(y_batch.numpy(), predict.numpy())
            recall = recall_score(y_batch.numpy(), predict.numpy())
            precision = precision_score(y_batch.numpy(), predict.numpy())

            print(f"F1:{f1} Recall:{recall} Precision:{precision} Acc:{acc}")


    # for i in sequences:
    #     print(i)

    # for text in text_list:
    #     sequences = text_to_sequences(vocab, text)
    #
    #     # print(f"输入文本:{text}")
    #     print(f"{sequences}")
    #     print(text_to_sequences(vocab, text))
帮我更正代码与其运行成功