基于深度学习的中文命名实体识别
import os
import sys
import codecs
import numpy as np
from sklearn.metrics import classification_report
from sklearn.preprocessing import LabelBinarizer
from keras.models import Model, Input
from keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional
from keras_contrib.layers import CRF
from keras_contrib.utils import save_load_utils
from keras.callbacks import ModelCheckpoint
# 定义数据读取函数
def read_data(file_path):
with codecs.open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
data = []
sentence = []
labels = []
for line in lines:
if line == '\n':
if sentence:
data.append(sentence)
labels.append(labels_)
sentence = []
labels_ = []
else:
word, label = line.strip().split()
sentence.append(word)
labels_.append(label)
return data, labels
# 定义特征提取函数
def feature_extract(data, word2id, label2id, max_len):
X = [[word2id.get(word, 0) for word in sentence] for sentence in data]
X = np.array([x + [0] * (max_len - len(x)) for x in X])
y = [[label2id[label] for label in sentence] for sentence in labels]
y = np.array([x + [0] * (max_len - len(x)) for x in y])
return X, y
# 定义模型结构
def build_model(input_dim, output_dim, input_length, embedding_dim=128, lstm_units=64):
input = Input(shape=(input_length,))
model = Embedding(input_dim=input_dim, output_dim=embedding_dim, input_length=input_length)(input)
model = Bidirectional(LSTM(units=lstm_units, return_sequences=True))(model)
model = TimeDistributed(Dense(units=lstm_units, activation='relu'))(model)
crf = CRF(output_dim=output_dim, sparse_target=True)
out = crf(model)
model = Model(input, out)
model.compile(optimizer='adam', loss=crf.loss_function, metrics=[crf.accuracy])
model.summary()
return model
# 定义训练函数
def train(X_train, y_train, X_dev, y_dev, word2id, label2id, max_len, batch_size=32, epochs=10):
input_dim = len(word2id) + 1
output_dim = len(label2id) + 1
model = build_model(input_dim, output_dim, max_len)
checkpoint = ModelCheckpoint('model.h5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(X_dev, y_dev), callbacks=[checkpoint])
save_load_utils.save_all_weights(model, 'model_weights.h5')
return model
# 定义预测函数
def predict(model, data, word2id, label2id, max_len):
X = [[word2id.get(word, 0) for word in sentence] for sentence in data]
X = np.array([x + [0] * (max_len - len(x)) for x in X])
y_pred = model.predict(X)
y_pred = np.argmax(y_pred, axis=-1)
y_pred = [[list(label2id.keys())[list(label2id.values()).index(pred)] for pred in sentence] for sentence in y_pred]
return y_pred
if __name__ == '__main__':
# 读取数据
train_data, train_labels = read_data('dev.conll')
test_data, test_labels = read_data('1.txt')
# 构建词表和标签表
words = set([word for sentence in train_data for word in sentence] + [word for sentence in test_data for word in sentence])
labels = set([label for sentence in train_labels for label in sentence])
word2id = {word: i + 1 for i, word in enumerate(words)}
label2id = {label: i + 1 for i, label in enumerate(labels)}
# 特征提取
max_len = max([len(sentence) for sentence in train_data + test_data])
X_train, y_train = feature_extract(train_data, word2id, label2id, max_len)
X_test, y_test = feature_extract(test_data, word2id, label2id, max_len)
# 训练模型
model = train(X_train, y_train, X_test, y_test, word2id, label2id, max_len)
# 预测并输出结果
y_pred = predict(model, test_data, word2id, label2id, max_len)
with codecs.open('对对对队_addr_parsing_runid.txt', 'w', encoding='utf-8') as f:
for i, sentence in enumerate(test_data):
for j, word in enumerate(sentence):
f.write('{}{}{}\n'.format(i+1, word, y_pred[i][j]))
f.write('\n')
算法解释:
该代码使用了基于**条件随机场(CRF)**的序列标注算法进行中文命名实体识别。
模型结构:
- Embedding 层: 将词语映射为词向量。
- 双向 LSTM 层: 提取上下文特征。
- 全连接层: 将 LSTM 输出映射到标签空间。
- CRF 层: 对标签序列进行解码,得到最优标签序列。
代码功能:
- 数据读取: 从文件中读取训练和测试数据。
- 特征提取: 将文本数据转换为模型可处理的数字特征。
- 模型构建: 定义模型结构,包括 Embedding 层、LSTM 层、全连接层和 CRF 层。
- 模型训练: 使用训练数据训练模型,并保存最佳模型参数。
- 模型预测: 使用训练好的模型对测试数据进行预测,并将预测结果输出到文件。
代码优势:
- 使用深度学习模型,能够有效捕捉文本中的语义信息。
- 使用 CRF 模型,能够有效考虑标签之间的依赖关系。
- 代码结构清晰,注释详细,易于理解和使用。
应用场景:
- 信息抽取
- 问答系统
- 机器翻译
- 语音识别
原文地址: https://www.cveoy.top/t/topic/fVHe 著作权归作者所有。请勿转载和采集!