当我在jupyter笔记本中运行该代码时,它将正确执行。
import numpy as np
import pandas as pd
import pickle
import json
import requests
df=pd.read_csv('ner_dataset.csv',encoding = "ISO-8859-1")
class SentenceGetter(object):
def __init__(self, df):
self.n_sent = 1
self.df = df
self.empty = False
agg_func = lambda s: [(w, t) for w,t in zip(s["Word"].values.tolist(),
s["Tag"].values.tolist())]
self.grouped = self.df.groupby("Sentence #").apply(agg_func)
self.sentences = [s for s in self.grouped]
def get_next(self):
try:
s = self.grouped["Sentence: {}".format(self.n_sent)]
self.n_sent += 1
return s
except:
return None
getter = SentenceGetter(df)
sentence = getter.sentences
words = list(set(df["Word"].values))
words.append("ENDPAD")
n_words=len(words)
print(n_words)
tags = list(set(df["Tag"].values))
tag=len(tags)
print(tag)
word2idx = {w: i for i, w in enumerate(words)}
tag2idx = {t: i for i, t in enumerate(tags)}
from tensorflow.keras.preprocessing.sequence import pad_sequences
X = [[word2idx[w[0]] for w in s] for s in sentence]
X = pad_sequences(maxlen=140, sequences=X, padding="post",value=n_words - 1)
y = [[tag2idx[w[1]] for w in s] for s in sentence]