Exercises - Natural Language Processing with Python (Chapter9)

import nltk

# 1
grammar = nltk.data.load('9_1_1.fcfg')
print(grammar)
tokens_1 = "I am happy".split()
tokens_2 = "she is happy".split()
tokens_3 = "she am happy".split()

parser = nltk.load_parser('9_1_1.fcfg')
for tree in parser.parse(tokens_1):
    print(tree)
for tree in parser.parse(tokens_2):
    print(tree)

parser = nltk.load_parser('9_1_2.fcfg')
for tree in parser.parse(tokens_1):
    print(tree)
for tree in parser.parse(tokens_2):
    print(tree)

# 9_1_1.fcfg
% start S
S   ->   PropN[PER=?n] VP[PER=?n]
NP[PER=?n] -> PropN[PER=?n]
VP[PER=?n] -> PRED[PER=?n] ADJ

PropN[PER=1] -> 'I'
PropN[PER=3] -> 'she'
PRED[PER=1] -> 'am'
PRED[PER=3] -> 'is'
ADJ -> 'happy'

# 9_1_2.fcfg
% start S
S                    -> NP[AGR=?n] VP[AGR=?n]
NP[AGR=?n]           -> PropN[AGR=?n]
VP[TENSE=?t, AGR=?n] -> Cop[TENSE=?t, AGR=?n] Adj

PropN[AGR=[NUM=sg, PER=3]]            -> 'she'
PropN[AGR=[NUM=sg, PER=1]]            -> 'I'
Cop[TENSE=pres,  AGR=[NUM=sg, PER=3]] -> 'is'
Cop[TENSE=pres,  AGR=[NUM=sg, PER=1]] -> 'am'
Adj                                   -> 'happy'


# 2
tokens_1_1 = "The boy sings".split()
tokens_1_2 = "Boy sings".split()

tokens_2_1 = "The boys sing".split()
tokens_2_2 = "Boys sing".split()

tokens_3_1 = "The water is precious".split()
tokens_3_2 = "Water is precious".split()

parser = nltk.load_parser('9_2.fcfg')
for tree in parser.parse(tokens_1_1):
    print(tree)
for tree in parser.parse(tokens_1_2):
    print(tree)
for tree in parser.parse(tokens_2_1):
    print(tree)
for tree in parser.parse(tokens_2_2):
    print(tree)
for tree in parser.parse(tokens_3_1):
    print(tree)
for tree in parser.parse(tokens_3_2):
    print(tree)

# 9_2.fcfg
% start S

    S[] -> NP[COUNT=?n] VP[COUNT=?n] | NP[COUNT=?n]

    NP[COUNT=?n] -> N[COUNT=?n]
    NP[COUNT=?n] -> Det[] N[COUNT=?n]
    NP[COUNT='pl'] -> N[COUNT='pl']
    VP[COUNT=?n, TENSE=?t] -> IV[COUNT=?n, TENSE=?t] | COP[] ADJ[]

    Det[] -> 'The'
    N[COUNT='less'] -> 'water' | 'Water'
    N[COUNT='sg'] -> 'boy' | 'Boy'
    N[COUNT='pl'] -> 'boys' | 'Boys'
    IV[COUNT='sg', TENSE='pres'] -> 'sings'
    IV[COUNT='pl', TENSE='pres'] -> 'sing'
    COP[] -> 'is'
    ADJ[] -> 'precious'


# 3
def subsumes(fs1, fs2):
    if fs1.unify(fs2) == fs2:
        return True


fs1 = nltk.FeatStruct(NUMBER=74)
fs2 = nltk.FeatStruct(NUMBER=74, STREET='rue Pascal')

print(subsumes(fs1, fs2))


# 4
tokens = "the student from France with good grades walks".split()
parser = nltk.load_parser('9_4.fcfg')
for tree in parser.parse(tokens):
    print(tree)

# 9_4.fcfg
% start S

S -> N[BAR=2] VP[TENSE=?t, NUM=?n]
N[BAR=2] -> Det N[BAR=1]
N[BAR=1] -> N[BAR=1] P[BAR=2]
N[BAR=1] -> N[BAR=0] P[BAR=2]
N[BAR=1] -> N[BAR=0]
P[BAR=2] -> PREP N[BAR=0] | PREP ADJ N[BAR=0]

VP[TENSE=?t, NUM=?n] -> V[SUBCAT=intrans, TENSE=?t, NUM=?n]
VP[TENSE=?t, NUM=?n] -> V[SUBCAT=trans, TENSE=?t, NUM=?n] NP
VP[TENSE=?t, NUM=?n] -> V[SUBCAT=clause, TENSE=?t, NUM=?n] SBar
SBar -> Comp S

V[SUBCAT=intrans, TENSE=pres, NUM=sg] -> 'disappears' | 'walks'
V[SUBCAT=trans, TENSE=pres, NUM=sg] -> 'sees' | 'likes'
V[SUBCAT=clause, TENSE=pres, NUM=sg] -> 'says' | 'claims'

V[SUBCAT=intrans, TENSE=pres, NUM=pl] -> 'disappear' | 'walk'
V[SUBCAT=trans, TENSE=pres, NUM=pl] -> 'see' | 'like'
V[SUBCAT=clause, TENSE=pres, NUM=pl] -> 'say' | 'claim'

V[SUBCAT=intrans, TENSE=past, NUM=?n] -> 'disappeared' | 'walked'
V[SUBCAT=trans, TENSE=past, NUM=?n] -> 'saw' | 'liked'
V[SUBCAT=clause, TENSE=past, NUM=?n] -> 'said' | 'claimed'

Comp -> 'that'
PREP -> 'from' | 'with'
Det -> 'a' | 'the'
N[BAR=0] -> 'student' | 'France' | 'grades'
ADJ -> 'good'


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


# 7
cp = nltk.load_parser('grammars/book_grammars/german.fcfg')
tokens = 'ich folge den Katze'.split()
tag = 0
for tree in cp.parse(tokens):
    if tree:
        tag = 1
        print(tree)
if tag == 0:
    print('FAIL')


# 8
fs1 = nltk.FeatStruct("[A = ?x, B= [C = ?x]]")
fs2 = nltk.FeatStruct("[B = [D = d]]")
fs3 = nltk.FeatStruct("[B = [C = d]]")
fs4 = nltk.FeatStruct("[A = (1)[B = b], C->(1)]")
fs5 = nltk.FeatStruct("[A = (1)[D = ?x], C = [E -> (1), F = ?x] ]")
fs6 = nltk.FeatStruct("[A = [D = d]]")
fs7 = nltk.FeatStruct("[A = [D = d], C = [F = [D = d]]]")
fs8 = nltk.FeatStruct("[A = (1)[D = ?x, G = ?x], C = [B = ?x, E -> (1)] ]")
fs9 = nltk.FeatStruct("[A = [B = b], C = [E = [G = e]]]")
fs10 = nltk.FeatStruct("[A = (1)[B = b], C -> (1)]")

print(fs2.unify(fs1))
print(fs1.unify(fs3))
print(fs4.unify(fs5))
print(fs5.unify(fs6))
print(fs5.unify(fs7))
print(fs5.unify(fs6))
print(fs8.unify(fs9))
print(fs8.unify(fs10))


# 9
fs1 = nltk.FeatStruct("[A = ?x, B= [C = ?x]]")
fs2 = nltk.FeatStruct("[ADDRESS1=?x, ADDRESS2=?x]")
print(fs1)
print(fs2)


# 12
parser = nltk.load_parser('9_12.fcfg')
token_1 = "The farmer loaded sand into the cart".split()
token_2 = "The farmer loaded the cart with sand".split()
for tree in parser.parse(token_1):
    print(tree)
for tree in parser.parse(token_2):
    print(tree)

# 9_12.fcfg
% start S

S -> NP VP[TENSE=?t, NUM=?n]

NP -> Det NS

VP[TENSE=?t, NUM=?n] -> V[SUBCAT=FO1, TENSE=?t, NUM=?n]
VP[TENSE=?t, NUM=?n] -> V[SUBCAT=FO2, TENSE=?t, NUM=?n]

VP[SUBCAT=FO1, TENSE=past, NUM=?n] -> V Det NO PREP NO
VP[SUBCAT=FO2, TENSE=past, NUM=?n] -> V NO PREP Det NO

Det -> 'The' | 'the'
NS -> 'farmer'
NO -> 'cart' | 'sand'
PREP -> 'with' | 'into'
V -> 'loaded' | 'filled' | 'dumped'

 

Mastering Natural Language Processing with Python by Deepti Chopra, Nisheeth Joshi, Iti Mathur 2016 | ISBN: 1783989041 | English | 238 pages Maximize your NLP capabilities while creating amazing NLP projects in Python About This Book Learn to implement various NLP tasks in Python Gain insights into the current and budding research topics of NLP This is a comprehensive step-by-step guide to help students and researchers create their own projects based on real-life applications Who This Book Is For This book is for intermediate level developers in NLP with a reasonable knowledge level and understanding of Python. What You Will Learn Implement string matching algorithms and normalization techniques Implement statistical language modeling techniques Get an insight into developing a stemmer, lemmatizer, morphological analyzer, and morphological generator Develop a search engine and implement POS tagging concepts and statistical modeling concepts involving the n gram approach Familiarize yourself with concepts such as the Treebank construct, CFG construction, the CYK Chart Parsing algorithm, and the Earley Chart Parsing algorithm Develop an NER-based system and understand and apply the concepts of sentiment analysis Understand and implement the concepts of Information Retrieval and text summarization Develop a Discourse Analysis System and Anaphora Resolution based system In Detail Natural Language Processing is one of the fields of computational linguistics and artificial intelligence that is concerned with human-computer interaction. It provides a seamless interaction between computers and human beings and gives computers the ability to understand human speech with the help of machine learning. This book will give you expertise on how to employ various NLP tasks in Python, giving you an insight into the best practices when designing and building NLP-based applications using Python. It will help you become an expert in no time and assist you in creating your own NLP projects using NLTK. You will sequentially be guided through applying machine learning tools to develop various models. We'll give you clarity on how to create training data and how to implement major NLP applications such as Named Entity Recognition, Question Answering System, Discourse Analysis, Transliteration, Word Sense disambiguation, Information Retrieval, Sentiment Analysis, Text Summarization, and Anaphora Resolution. Style and approach This is an easy-to-follow guide, full of hands-on examples of real-world tasks. Each topic is explained and placed in context, and for the more inquisitive, there are more details of the concepts used.
Python Natural Language Processing by Jalaj Thanaki English | 31 July 2017 | ISBN: 1787121429 | ASIN: B072B8YWCJ | 486 Pages | AZW3 | 11.02 MB Key Features Implement Machine Learning and Deep Learning techniques for efficient natural language processing Get started with NLTK and implement NLP in your applications with ease Understand and interpret human languages with the power of text analysis via Python Book Description This book starts off by laying the foundation for Natural Language Processing and why Python is one of the best options to build an NLP-based expert system with advantages such as Community support, availability of frameworks and so on. Later it gives you a better understanding of available free forms of corpus and different types of dataset. After this, you will know how to choose a dataset for natural language processing applications and find the right NLP techniques to process sentences in datasets and understand their structure. You will also learn how to tokenize different parts of sentences and ways to analyze them. During the course of the book, you will explore the semantic as well as syntactic analysis of text. You will understand how to solve various ambiguities in processing human language and will come across various scenarios while performing text analysis. You will learn the very basics of getting the environment ready for natural language processing, move on to the initial setup, and then quickly understand sentences and language parts. You will learn the power of Machine Learning and Deep Learning to extract information from text data. By the end of the book, you will have a clear understanding of natural language processing and will have worked on multiple examples that implement NLP in the real world. What you will learn Focus on Python programming paradigms, which are used to develop NLP applications Understand corpus analysis and different types of data attribute. Learn NLP using Python libraries such as NLTK, Polyglot,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_Meilinger_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值