Note - Natural Language Processing with Python (Chapter9)

import nltk
from nltk import load_parser


# Example 9.1
kim = {'CAT': 'NP', 'ORTH': 'Kim', 'REF': 'k'}
chase = {'CAT': 'V', 'ORTH': 'chased', 'REL': 'chase'}
chase['AGT'] = 'sbj'
chase['PAT'] = 'obj'
lee = {'CAT': 'NP', 'ORTH': 'Lee', 'REF': 'l'}
sent = "Kim chased Lee"
tokens = sent.split()
def lex2fs(word):
    for fs in [kim, lee, chase]:
        if fs['ORTH'] == word:
            return fs
subj, verb, obj = lex2fs(tokens[0]), lex2fs(tokens[1]), lex2fs(tokens[2])
verb['AGT'] = subj['REF']
verb['PAT'] = obj['REF']
for k in ['ORTH', 'REL', 'AGT', 'PAT']:
    print("%-5s => %s" % (k, verb[k]))


surprise = {'CAT': 'V', 'ORTH': 'surprised', 'REL': 'surprise',
            'SRC': 'sbj', 'EXP': 'obj'}


nltk.data.show_cfg('grammars/book_grammars/feat0.fcfg')



grammar = nltk.data.load('grammars/book_grammars/feat0.fcfg')
print(grammar)


# Example 9.2
tokens = 'Kim likes children'.split()
cp = load_parser('grammars/book_grammars/feat0.fcfg', trace=2)
trees = cp.parse(tokens)
for tree in trees:
    print(tree)


# 9.2 Processing Feature Structures
fs1 = nltk.FeatStruct(TENSE='past', NUM='sg')
print(fs1)

fs1 = nltk.FeatStruct(PER=3, NUM='pl', GND='fem')
print(fs1['GND'])
fs1['CASE'] = 'acc'

fs2 = nltk.FeatStruct(POS='N', AGR=fs1)
print(fs2)
print(fs2['AGR'])
print(fs2['AGR']['PER'])

print(nltk.FeatStruct("[POS='N', AGR=[PER=3, NUM='pl', GND='fem']]"))

print(nltk.FeatStruct(NAME='Lee', TELNO='01 27 86 42 96', AGE=33))


print(nltk.FeatStruct("""[NAME='Lee', ADDRESS=(1)[NUMBER=74, STREET='rue Pascal'],
SPOUSE=[NAME='Kim', ADDRESS->(1)]]"""))

print(nltk.FeatStruct("[A='a', B=(1)[C='c'], D->(1), E->(1)]"))


# Subsumption and Unification
fs1 = nltk.FeatStruct(NUMBER=74, STREET='rue Pascal')
fs2 = nltk.FeatStruct(CITY='Paris')
print(fs1.unify(fs2))
print(fs2.unify(fs1))


fs0 = nltk.FeatStruct(A='a')
fs1 = nltk.FeatStruct(A='b')
fs2 = fs0.unify(fs1)
print(fs2)


fs0 = nltk.FeatStruct("""[
NAME=Lee,
ADDRESS=[NUMBER=74,STREET='rue Pascal'],
SPOUSE= [NAME=Kim,ADDRESS=[NUMBER=74,STREET='rue Pascal']]
]""")
print(fs0)

fs1 = nltk.FeatStruct("[SPOUSE = [ADDRESS = [CITY = Paris]]]")
print(fs1.unify(fs0))

fs2 = nltk.FeatStruct("""[
NAME=Lee,
ADDRESS=(1)[NUMBER=74, STREET='rue Pascal'],
SPOUSE=[NAME=Kim, ADDRESS->(1)]
]""")
print(fs1.unify(fs2))


fs1 = nltk.FeatStruct("[ADDRESS1=[NUMBER=74, STREET='rue Pascal']]")
fs2 = nltk.FeatStruct("[ADDRESS1=?x, ADDRESS2=?x]")
print(fs2)
print(fs2.unify(fs1))


# 9.3 Extending a Feature based Grammar
nltk.data.show_cfg('grammars/book_grammars/feat1.fcfg')
cp = load_parser('grammars/book_grammars/feat1.fcfg')
tokens = 'who do you claim that you like'.split()
trees = cp.parse(tokens)
for tree in trees:
    print(tree)


# Example 9.3
tokens = 'you claim that you like cats'.split()
for tree in cp.parse(tokens):
    print(tree)

tokens = 'rarely do you sing'.split()
for tree in cp.parse(tokens):
    print(tree)


nltk.data.show_cfg('grammars/book_grammars/german.fcfg')


# Example 9.4
tokens = 'ich folge den Katzen'.split()
cp = load_parser('grammars/book_grammars/german.fcfg')
for tree in cp.parse(tokens):
    print(tree)

cp = load_parser('grammars/book_grammars/german.fcfg', trace=2)
tokens = 'ich folge den Katze'.split()
for tree in cp.parse(tokens):
    print(tree)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Meilinger_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值