詞義消除歧義NLP項目實驗
本項目主要使用https://github.com/alvations/pywsd 中的pywsd庫來實現詞義消除歧義html
目前,該庫一部分已經移植到了nltk中,爲了得到更好的性能WSD,而不是使用的NLTK
模塊pywsd
庫。通常來講,從pywsd
的simple_lesk()
比NLTK
的lesk
好。當我有空時,我會盡可能更新NLTK
模塊。在本文檔中主要介紹原pywsd庫的使用。python
1、使用的技術:git
-
Lesk 算法github
- Original Lesk (Lesk, 1986)
- Adapted/Extended Lesk (Banerjee and Pederson, 2002/2003)
- Simple Lesk (with definition, example(s) and hyper+hyponyms)
- Cosine Lesk (use cosines to calculate overlaps instead of using raw counts)
-
最大化類似度 (see also, Pedersen et al. (2003))web
- Path similarity (Wu-Palmer, 1994; Leacock and Chodorow, 1998)
- Information Content (Resnik, 1995; Jiang and Corath, 1997; Lin, 1998)
-
基線算法
- Random sense
- First NLTK sense
- Highest lemma counts
2、使用方法:app
安裝:dom
pip install -U nltk python -m nltk.downloader 'popular' pip install -U pywsd
使用:性能
from pywsd.lesk import simple_lesk #引入pywsd庫 sent = 'I went to the bank to deposit my money' #設定包含具備多義的詞的句子 ambiguous = 'bank' #設定多義的詞語 answer = simple_lesk(sent, ambiguous, pos='n') #設置answer的參數,將句子與詞進行判斷 print (answer.definition()) #打印出答案
3、原理優化
詞義消岐,英文名稱爲Word Sense Disambiguation,英語縮寫爲WSD,LESK算法是詞義消歧的主要算法。
LESK算法是以一種以TF-IDF爲權重的頻數判別算法,主要流程能夠簡述爲:
- 去掉停用詞
- 統計出該詞之外的TF-IDF值
- 累加起來,比較多個義項下這個值的大小,值越大說明是該句子的義項
下面以NBA火箭隊爲示例來簡要實現一下lesk算法:
import os import jieba from math import log2 # 讀取每一個義項的語料 def read_file(path): with open(path, 'r', encoding='utf-8') as f: lines = [_.strip() for _ in f.readlines()] return lines # 對示例句子分詞 sent = '賽季初的時候,火箭是衆望所歸的西部決賽球隊。' wsd_word = '火箭' jieba.add_word(wsd_word) sent_words = list(jieba.cut(sent, cut_all=False)) # 去掉停用詞 stopwords = [wsd_word, '我', '你', '它', '他', '她', '了', '是', '的', '啊', '誰', '什麼','都',\ '很', '個', '之', '人', '在', '上', '下', '左', '右', '。', ',', '!', '?'] sent_cut = [] for word in sent_words: if word not in stopwords: sent_cut.append(word) print(sent_cut) # 計算其餘詞的TF-IDF以及頻數 wsd_dict = {} for file in os.listdir('.'): if wsd_word in file: wsd_dict[file.replace('.txt', '')] = read_file(file) # 統計每一個詞語在語料中出現的次數 tf_dict = {} for meaning, sents in wsd_dict.items(): tf_dict[meaning] = [] for word in sent_cut: word_count = 0 for sent in sents: example = list(jieba.cut(sent, cut_all=False)) word_count += example.count(word) if word_count: tf_dict[meaning].append((word, word_count)) idf_dict = {} for word in sent_cut: document_count = 0 for meaning, sents in wsd_dict.items(): for sent in sents: if word in sent: document_count += 1 idf_dict[word] = document_count # 輸出值 total_document = 0 for meaning, sents in wsd_dict.items(): total_document += len(sents) # 計算tf_idf值 mean_tf_idf = [] for k, v in tf_dict.items(): print(k+':') tf_idf_sum = 0 for item in v: word = item[0] tf = item[1] tf_idf = item[1]*log2(total_document/(1+idf_dict[word])) tf_idf_sum += tf_idf print('%s, 頻數爲: %s, TF-IDF值爲: %s'% (word, tf, tf_idf)) mean_tf_idf.append((k, tf_idf_sum)) sort_array = sorted(mean_tf_idf, key=lambda x:x[1], reverse=True) true_meaning = sort_array[0][0].split('_')[1] print('\n通過詞義消岐,%s在該句子中的意思爲 %s .' % (wsd_word, true_meaning))
結果以下:
['賽季', '初', '時候', '衆望所歸', '西部', '決賽', '球隊'] 火箭_燃氣推動裝置: 初, 頻數爲: 2, TF-IDF值爲: 12.49585502688717 火箭_NBA球隊名: 賽季, 頻數爲: 63, TF-IDF值爲: 204.6194333469459 初, 頻數爲: 1, TF-IDF值爲: 6.247927513443585 時候, 頻數爲: 1, TF-IDF值爲: 8.055282435501189 西部, 頻數爲: 16, TF-IDF值爲: 80.88451896801904 決賽, 頻數爲: 7, TF-IDF值爲: 33.13348038429679 球隊, 頻數爲: 40, TF-IDF值爲: 158.712783770034
通過詞義消岐,火箭在該句子中的意思爲 NBA球隊名 .
又如:
輸入句子:三十多年前,戰士們在戈壁灘白手起家,建起了我國的火箭發射基地。
['三十多年', '前', '戰士', '們', '戈壁灘', '白手起家', '建起', '我國', '發射', '基地'] 火箭_燃氣推動裝置: 前, 頻數爲: 2, TF-IDF值爲: 9.063440958888354 們, 頻數爲: 1, TF-IDF值爲: 6.05528243550119 我國, 頻數爲: 3, TF-IDF值爲: 22.410959804340102 發射, 頻數爲: 89, TF-IDF值爲: 253.27878721862933 基地, 頻數爲: 7, TF-IDF值爲: 42.38697704850833 火箭_NBA球隊名: 前, 頻數爲: 3, TF-IDF值爲: 13.59516143833253 們, 頻數爲: 1, TF-IDF值爲: 6.05528243550119 通過詞義消岐,火箭在該句子中的意思爲 燃氣推動裝置 .
概述:輸入的文段或者句子,以後,將分割好的該詞的釋義進行分割,造成幾個詞。然後,在每一個文段和句子中計算被分割詞的個數,而後算出TF-IDF的值,計算哪一個TF-IDF的值最大,爲更適用於該釋義。
4、改進
對於代碼自己,能夠作到一點點進步的優化,算法上的優化能夠作到更大的跨越,如http://www.doc88.com/p-9959426974439.html這篇文章提到的lesk算法的改進。
對於lesk算法的缺點,釋義的判斷很容易被相同TF-IDF的值誤擾,即權值相同的狀況。
原文出處:https://www.cnblogs.com/pwn2web/p/11582613.html