1. 程式人生 > >基於crf的CoNLL2002資料集命名實體識別模型實現-pycrfsuite

基於crf的CoNLL2002資料集命名實體識別模型實現-pycrfsuite

下面是用python的pycrfsuite庫實現的命名實體識別,是我最初為了感知命名實體識別到底是什麼,調研命名實體識別時跑的案例,記錄在下面,為了以後查閱。

案例說明:

內容:在通用語料庫CoNLL2002上,用crf方法做命名實體識別(地點、組織和人名)。	
工具:Anaconda2
	
語料庫介紹:
 - 通用語料庫:	CoNLL2002
 - 語言:		西班牙語
 - 訓練集:		8323句
 - 測試集:		1517句
 - 語料格式:	三列,分別表示詞彙、詞性、實體型別;使用Bakeoff-3評測中所採用的的BIO標註集,即B-PER、I-PER代表人名首字、人名非首字,
				B-LOC、I-LOC代表地名首字、地名非首字,B-ORG、I-ORG代表組織機構名首字、組織機構名非首字,O代表該字不屬於命名實體的一部分。
				如:EFE NC B-ORG
			
特徵處理:
主要選擇處理了如下幾個特徵:
 - 當前詞的小寫格式 
 - 當前詞的字尾
 - 當前詞是否全大寫 isupper
 - 當前詞的首字母大寫,其他字母小寫判斷 istitle
 - 當前詞是否為數字 isdigit
 - 當前詞的詞性
 - 當前詞的詞性字首
 
演算法選擇:crf
 
預測效果:
 
             precision    recall  f1-score   support

      B-LOC       0.78      0.75      0.76      1084
      I-LOC       0.66      0.60      0.63       325
     B-MISC       0.69      0.47      0.56       339
     I-MISC       0.61      0.49      0.54       557
      B-ORG       0.79      0.81      0.80      1400
      I-ORG       0.80      0.79      0.80      1104
      B-PER       0.82      0.87      0.84       735
      I-PER       0.87      0.93      0.90       634

avg / total       0.77      0.76      0.76      6178

指令碼:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
@author:
@contact:
@time:
@context: makes a simple example of NER.
"""

from itertools import chain
import nltk,pycrfsuite
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.preprocessing import LabelBinarizer

#通用語料conll2002下載
nltk.download("conll2002", "E:/nltk_data/") print(nltk.corpus.conll2002.fileids()) #讀取測試集和訓練集 train_sents = list(nltk.corpus.conll2002.iob_sents('esp.train')) test_sents = list(nltk.corpus.conll2002.iob_sents('esp.testb')) # print(len(train_sents)) # print(len(test_sents)) #特徵處理 """ 特徵處理流程,主要選擇處理了如下幾個特徵: - 當前詞的小寫格式 - 當前詞的字尾 - 當前詞是否全大寫 isupper - 當前詞的首字母大寫,其他字母小寫判斷 istitle - 當前詞是否為數字 isdigit - 當前詞的詞性 - 當前詞的詞性字首 - 還有就是與之前後相關聯的詞的上述特徵(類似於特徵模板的定義) """
def word2features(sent, i): word = sent[i][0] postag = sent[i][1] features = [ 'bias', 'word.lower=' + word.lower(), 'word[-3:]=' + word[-3:], 'word[-2:]=' + word[-2:], 'word.isupper=%s' % word.isupper(), 'word.istitle=%s' % word.istitle(), 'word.isdigit=%s' % word.isdigit(), 'postag=' + postag, 'postag[:2]=' + postag[:2], ] if i > 0: word1 = sent[i-1][0] postag1 = sent[i-1][1] features.extend([ '-1:word.lower=%s' % word1.lower(), '-1:word.istitle=%s' % word1.istitle(), '-1:word.issupper=%s' % word1.isupper(), '-1:postag=%s' % postag1, '-1:postag[:2]=%s' % postag1[:2], ]) else: features.append('BOS') if i < len(sent)-1: word1 = sent[i+1][0] postag1 = sent[i+1][1] features.extend([ '+1:word.lower=%s' % word1.lower(), '+1:word.istitle=%s' % word1.istitle(), '+1:word.issupper=%s' % word1.isupper(), '+1:postag=%s' % postag1, '+1:postag[:2]=%s' % postag1[:2], ]) else: features.append('EOS') return features #測試效果 # sent=train_sents[0] # print(len(sent)) # for i in range (len(sent)): # print(word2features(sent,i)) # print("======================================") # 完成特徵轉化 def sent2features(sent): return [word2features(sent,i) for i in range(len(sent))] #獲取類別,即標籤 def sent2labels(sent): return [label for token,postag,label in sent] #獲取詞 def sent2tokens(sent): return [token for token,postag,label in sent] #特徵如上轉化完成後,可以檢視下一行特徵內容 #print(sent2features(train_sents[0])[0]) #構造特徵訓練集和測試集 X_train = [sent2features(s) for s in train_sents] Y_train = [sent2labels(s) for s in train_sents] # print(len(X_train)) # print(len(Y_train)) X_test = [sent2features(s) for s in test_sents] Y_test = [sent2labels(s) for s in test_sents] # print(len(X_test)) # print(X_train[0]) # print(Y_train[0]) print(len(Y_test)) print(type(Y_test)) # 模型訓練 #1) 建立pycrfsuite.Trainer trainer = pycrfsuite.Trainer(verbose=False) #載入訓練特徵和分類的類別(label) for xseq,yseq in zip(X_train,Y_train): trainer.append(xseq,yseq) #2)設定訓練引數,選擇 L-BFGS 訓練演算法(預設)和 Elastic Net 迴歸模型 trainer.set_params({ 'c1' : 1.0, #coefficient for L1 penalty 'c2' : 1e-3, #coefficient for L2 penalty 'max_iterations':50, #stop earlier # include transitions that are possible, but not observed 'feature.possible_transitions':True }) #print(trainer.params()) #3)開始訓練 #含義是訓練出的模型名為:conll2002-esp.crfsuite # trainer.train('conll2002-esp.crfsuite') #使用訓練後的模型,建立用於測試的標註器。 tagger = pycrfsuite.Tagger() tagger.open('conll2002-esp.crfsuite') example_sent = test_sents[0] #檢視這句話的內容 # print(type(sent2tokens(example_sent))) # print(sent2tokens(example_sent)) # print(''.join(sent2tokens(example_sent))) # print('\n\n') # print("Predicted:", ' '.join(tagger.tag(sent2features(example_sent)))) # print("Predicted:", ' '.join(tagger.tag(X_test[0]))) # print("Correct: ", ' '.join(sent2labels(example_sent))) #檢視模型在訓練集上的效果 def bio_classification_report(y_true, y_pred): lb = LabelBinarizer() y_true_combined = lb.fit_transform(list(chain.from_iterable(y_true))) y_pred_combined = lb.transform(list(chain.from_iterable(y_pred))) tagset = set(lb.classes_) - {'O'} tagset = sorted(tagset, key=lambda tag: tag.split('-', 1)[::-1]) class_indices = {cls: idx for idx, cls in enumerate(lb.classes_)} return classification_report( y_true_combined, y_pred_combined, labels = [class_indices[cls] for cls in tagset], target_names = tagset, ) #標註所有資訊 Y_pred = [tagger.tag(xseq) for xseq in X_test] print(type(Y_pred)) print(type(Y_test)) #打印出評測報告 print(bio_classification_report(Y_test, Y_pred))

報錯

下載資料時出現了報錯,需要加“nltk.download(“conll2002”, “E:/nltk_data/”)”這一行指令碼。
下載資料出現報錯如何解決的資料

參考資料:

1.[Python]How to use CRFSuite ? (2)
2.Let’s use CoNLL 2002 data to build a NER system