当前位置:   article > 正文

自然语言理解_read_expr()函数

read_expr()函数

       声明:代码的运行环境为Python3。Python3与Python2在一些细节上会有所不同,希望广大读者注意。本博客以代码为主,代码中会有详细的注释。相关文章将会发布在我的个人博客专栏《Python自然语言处理》,欢迎大家关注。


       有了之前的学习之后,就可以进行自然语言的理解分析了,一起来看看吧~

一、基于SQL数据库的问答系统

1、首先输入一个特征文法 

  1. import nltk
  2. # 基于sql数据库的问答系统
  3. nltk.data.show_cfg('grammars/book_grammars/sql0.fcfg') # 输入一个特征文法

输出结果为:

  1. % start S
  2. S[SEM=(?np + WHERE + ?vp)] -> NP[SEM=?np] VP[SEM=?vp]
  3. VP[SEM=(?v + ?pp)] -> IV[SEM=?v] PP[SEM=?pp]
  4. VP[SEM=(?v + ?ap)] -> IV[SEM=?v] AP[SEM=?ap]
  5. NP[SEM=(?det + ?n)] -> Det[SEM=?det] N[SEM=?n]
  6. PP[SEM=(?p + ?np)] -> P[SEM=?p] NP[SEM=?np]
  7. AP[SEM=?pp] -> A[SEM=?a] PP[SEM=?pp]
  8. NP[SEM='Country="greece"'] -> 'Greece'
  9. NP[SEM='Country="china"'] -> 'China'
  10. Det[SEM='SELECT'] -> 'Which' | 'What'
  11. N[SEM='City FROM city_table'] -> 'cities'
  12. IV[SEM=''] -> 'are'
  13. A[SEM=''] -> 'located'
  14. P[SEM=''] -> 'in'

2、将询问的语句转换为一个可用于数据库查询的SQL语句

  1. from nltk import load_parser
  2. # 将正常的询问语句转化为正常的SQL语句
  3. cp = load_parser('grammars/book_grammars/sql0.fcfg')
  4. query = 'What cities are located in China'
  5. trees = list(cp.parse(query.split()))
  6. answer = trees[0].label()['SEM']
  7. answer = [s for s in answer if s]
  8. q = ' '.join(answer)
  9. print(q)

输出结果为:

SELECT City FROM city_table WHERE Country="china"

3、测试

  1. from nltk.sem import chat80
  2. rows = chat80.sql_query('corpora/city_database/city.db', q)
  3. for r in rows:
  4. print(r[0], " ")

输出结果为:

  1. canton
  2. chungking
  3. dairen
  4. harbin
  5. kowloon
  6. mukden
  7. peking
  8. shanghai
  9. sian
  10. tientsin

       这里有必要说明一下,这个输出结果为粤语的拼音,故而感觉起来有点怪怪的~由上例可以看出,只要我们定义了一个文法,我们就可以将一个问题提取转换成一个正常的可用于数据库查询的SQL语句。

二、命题逻辑

1、使用boolean_ops()方法进行表示

  1. nltk.boolean_ops()
  2. negation -
  3. conjunction &
  4. disjunction |
  5. implication ->
  6. equivalence <->

运算符为true的条件如下图所示:

2、测试

  1. read_expr = nltk.sem.Expression.fromstring
  2. read_expr('-(P & Q)')
  3. read_expr('P & Q')
  4. read_expr('P | (R -> Q)')
  5. read_expr('P <-> -- P')

最后一个的结果:

<IffExpression (P <-> --P)>

测试: 

  1. lp = nltk.sem.Expression.fromstring
  2. SnF = read_expr('SnF')
  3. NotFnS = read_expr('-FnS')
  4. R = read_expr('SnF -> -FnS')
  5. prover = nltk.Prover9()
  6. prover.config_prover9(r'D:\Program Files\LADR1007B-win\bin')
  7. prover.prove(NotFnS, [SnF, R])
  8. val = nltk.Valuation([('P', True), ('Q', True), ('R', False)])
  9. print(val['P'])

结果:

True
  1. dom = set()
  2. g = nltk.Assignment(dom)
  3. m = nltk.Model(dom, val)
  4. print(m.evaluate('(P & Q)', g))
  5. print(m.evaluate('-(P & Q)', g))
  6. print(m.evaluate('(P & R)', g))
  7. print(m.evaluate('(P | R)', g))
  8. True
  9. False
  10. False
  11. True

三、一阶逻辑

  1. # 一阶逻辑
  2. read_expr = nltk.sem.Expression.fromstring
  3. expr = read_expr('walk(angus)', type_check=True)
  4. print(expr.argument)
  5. print(expr.argument.type)
  6. print(expr.function)
  7. print(expr.function.type)

结果为:

  1. angus
  2. e
  3. walk
  4. <e,?>
  1. sig = {'walk': '<e, t>'}
  2. expr = read_expr('walk(angus)', signature=sig)
  3. print(expr.function.type)
  4. read_expr = nltk.sem.Expression.fromstring
  5. read_expr('dog(cyril)').free()
  6. read_expr('dog(x)').free()
  7. read_expr('own(angus, cyril)').free()
  8. read_expr('exists x.dog(x)').free()
  9. read_expr('((some x. walk(x)) -> sing(x))').free()
  10. read_expr('exists x.own(y, x)').free()

真值模型:

  1. # 真值模型
  2. dom = {'b', 'o', 'c'}
  3. v = """
  4. bertie => b
  5. olive => o
  6. cyril => c
  7. boy => {b}
  8. girl => {o}
  9. dog => {c}
  10. walk => {o, c}
  11. see => {(b, o), (c, b), (o, c)}
  12. """
  13. val = nltk.Valuation.fromstring(v)
  14. print(val)
  15. print(('o', 'c') in val['see'])
  16. print(('b',) in val['boy'])

运算:

  1. # 运算
  2. g = nltk.Assignment(dom, [('x', 'o'), ('y', 'c')])
  3. print(g)
  4. m = nltk.Model(dom, val)
  5. m.evaluate('see(olive, y)', g)
  6. print(g['y'])
  7. m.evaluate('see(y, x)', g)
  8. g.purge()
  9. print(g)
  10. m.evaluate('see(olive, y)', g)
  11. m.evaluate('see(bertie, olive) & boy(bertie) & -walk(bertie)', g)

四、量化

  1. m.evaluate('exists x.(girl(x) & walk(x))', g)
  2. m.evaluate('girl(x) & walk(x)', g.add('x', 'o'))
  3. fmla1 = read_expr('girl(x) | boy(x)')
  4. m.satisfiers(fmla1, 'x', g)
  5. fmla2 = read_expr('girl(x) -> walk(x)')
  6. m.satisfiers(fmla2, 'x', g)
  7. fmla3 = read_expr('walk(x) -> girl(x)')
  8. m.satisfiers(fmla3, 'x', g)
  9. m.evaluate('all x.(girl(x) -> walk(x))', g)

五、量化歧义

  1. v2 = """
  2. bruce => b
  3. elspeth => e
  4. julia => j
  5. matthew => m
  6. person => {b, e, j, m}
  7. admire => {(j, b), (b, b), (m, e), (e, m)}
  8. """
  9. val2 = nltk.Valuation.fromstring(v2)
  10. dom2 = val2.domain
  11. m2 = nltk.Model(dom2, val2)
  12. g2 = nltk.Assignment(dom2)
  13. fmla4 = read_expr('(person(x) -> exists y.(person(y) & admire(x, y)))')
  14. m2.satisfiers(fmla4, 'x', g2)
  15. fmla5 = read_expr('(person(y) & all x.(person(x) -> admire(x, y)))')
  16. m2.satisfiers(fmla5, 'y', g2)
  17. fmla6 = read_expr('(person(y) & all x.((x = bruce | x = julia) -> admire(x, y)))')
  18. m2.satisfiers(fmla6, 'y', g2)

六、句子语义理解

  1. read_expr = nltk.sem.Expression.fromstring
  2. expr = read_expr(r'\x.(walk(x) & chew_gum(x))')
  3. print(expr)
  4. expr.free()
  5. print(read_expr(r'\x.(walk(x) & chew_gum(y))'))
  6. expr = read_expr(r'\x.(walk(x) & chew_gum(x))(gerald)')
  7. print(expr)
  8. print(expr.simplify())
  9. print(read_expr(r'\x.\y.(dog(x) & own(y, x))(cyril)').simplify())
  10. print(read_expr(r'\x y.(dog(x) & own(y, x))(cyril, angus)').simplify())
  11. expr1 = read_expr('exists x.P(x)')
  12. print(expr1)
  13. expr2 = expr1.alpha_convert(nltk.sem.Variable('z'))
  14. print(expr2)
  15. print(expr1 == expr2)
  16. expr3 = read_expr('\P.(exists x.P(x))(\y.see(y, x))')
  17. print(expr3)
  18. print(expr3.simplify())

七、量词歧义

  1. from nltk.sem import cooper_storage as cs
  2. sentence = 'every girl chases a dog'
  3. trees = cs.parse_with_bindops(sentence, grammar='grammars/book_grammars/storage.fcfg')
  4. semrep = trees[0].label()['SEM']
  5. cs_semrep = cs.CooperStore(semrep)
  6. print(cs_semrep.core)
  7. for bo in cs_semrep.store:
  8. print(bo)
  9. cs_semrep.s_retrieve(trace=True)
  10. for reading in cs_semrep.readings:
  11. print(reading)

八、段落语义理解

  1. read_dexpr = nltk.sem.DrtExpression.fromstring
  2. drs1 = read_dexpr('([x, y], [angus(x), dog(y), own(x, y)])')
  3. print(drs1)
  4. drs1.draw()
  5. print(drs1.fol())
  6. drs2 = read_dexpr('([x], [walk(x)]) + ([y], [run(y)])')
  7. print(drs2)
  8. print(drs2.simplify())
  9. drs3 = read_dexpr('([], [(([x], [dog(x)]) -> ([y],[ankle(y), bite(x, y)]))])')
  10. print(drs3.fol())
  11. drs4 = read_dexpr('([x, y], [angus(x), dog(y), own(x, y)])')
  12. drs5 = read_dexpr('([u, z], [PRO(u), irene(z), bite(u, z)])')
  13. drs6 = drs4 + drs5
  14. print(drs6.simplify())
  15. print(drs6.simplify().resolve_anaphora())
  16. from nltk import load_parser
  17. parser = load_parser('grammars/book_grammars/drt.fcfg', logic_parser=nltk.sem.drt.DrtParser())
  18. trees = list(parser.parse('Angus owns a dog'.split()))
  19. print(trees[0].label()['SEM'].simplify())

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号