NLP(十二)指代消解

  • 代词是用来代替重复出现的名词
    例句:
    1.Ravi is a boy. He often donates money to the poor.
    先出现主语,后出现代词,所以流动的方向从左到右,这类句子叫回指(Anaphora)
    2.He was already on his way to airport.Realized Ravi.
    这种句子表达的方式的逆序的,这类句子叫预指(Cataphora)
  • 代码
import nltk
from nltk.chunk import tree2conlltags
from nltk.corpus import names # 有 人名和性别 标签
import random

class AnaphoraExample:
    def __init__(self): # 不需要参数就能构造
        males = [(name,'male') for name in names.words('male.txt')]
        females = [(name,'female') for name in names.words('female.txt')]
        combined = males + females # 列表元素:人名和性别构成的元组
        random.shuffle(combined)
        # print(combined)
        training = [(self.feature(name),gender) for (name,gender) in combined]
        self._classifier = nltk.NaiveBayesClassifier.train(training) # 分类器

    def feature(self,word): # 单词最后一个字母当特征
        return {'last(1)' : word[-1]}

    def gender(self,word): # 返回单词放到分类器中得到的性别标签
        return self._classifier.classify(self.feature(word))

    def learnAnaphora(self):
        sentences = [
            "John is a man. He walks",
            "John and Mary are married. They have two kids",
            "In order for Ravi to be successful, he should follow John",
            "John met Mary in Barista. She asked him to order a Pizza",
        ]

        for sent in sentences:
            chunks = nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sent)),binary=False)
            # 实现分词,词性标注,组块(实体)抽取,返回组块树结果,赋给chunks
            stack = []
            print(sent)
            items = tree2conlltags(chunks) # 将一个句子展平成一个列表,并以IOB格式表示
            for item in items:
                if item[1] == 'NNP' and (item[2] == 'B-PERSON' or item[2] == '0'): # 人名
                    stack.append((item[0],self.gender(item[0]))) # 人名和性别的元组
                elif item[1] == 'CC': # 连词
                    stack.append(item[0])
                elif item[1] == 'PRP': # 人称代词
                    stack.append(item[0])
            print('\t{}'.format(stack))

if __name__ == "__main__":
    anaphora = AnaphoraExample()
    anaphora.learnAnaphora()

输出:

John is a man. He walks
    [('John', 'male'), 'He']
John and Mary are married. They have two kids
    [('John', 'male'), 'and', ('Mary', 'female'), 'They']
In order for Ravi to be successful, he should follow John
    [('Ravi', 'female'), 'he', ('John', 'male')]
John met Mary in Barista. She asked him to order a Pizza
    [('John', 'male'), ('Mary', 'female'), 'She', 'him']

转载于:https://www.cnblogs.com/peng8098/p/nlp_12.html

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值