User Tools

Site Tools


public:t-malv-15-3:8

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
public:t-malv-15-3:8 [2015/10/06 12:14] โ€“ created orvarkpublic:t-malv-15-3:8 [2024/04/29 13:33] (current) โ€“ external edit 127.0.0.1
Line 4: Line 4:
  
 If you can't manage to complete a particular problem please hand in your incomplete code -- comment it out if it produces an error. If you can't manage to complete a particular problem please hand in your incomplete code -- comment it out if it produces an error.
 +
 +===== 0. Introduction =====
 +
 +Create a short python script that uses the NLTK to evaluate whether a statement given in natural language is True or False for a given model of the world. //You are given complete freedom to choose what this model is like and what sentences your system understands.//
 +
 +The exercise can be divided into two distinct parts:
 +  - Semantic analysis of the natural language input using a parser and a grammar with semantic attachments, resulting in a FOL representation of the input.
 +  - Evaluating the FOL representation of the input against a model created to represent a particular state of affairs in the world.
  
 ===== 1. Parsing using "feature-based CFG" with semantic attachments ===== ===== 1. Parsing using "feature-based CFG" with semantic attachments =====
  
-**Semantic analysis of the natural language input using a parser and a grammar with semantic attachments, resulting in a FOL representation of the input.**+**Semantic analysis of the natural language input using a parser and a grammar with semantic attachments, resulting in a [[https://en.wikipedia.org/wiki/First-order_logic|First-Order Logic]] (FOLrepresentation of the input.**
  
 You can perform syntax-driven semantic analysis of the natural language input sentence (as seen in chapters 18.1 and 18.2 in the text book) by building a NLTK parser from a "feature-based context free grammar" (fcfg) that contains semantic attachments as features. [[http://www.nltk.org/book/ch09.html|Feature-based parsing is covered in chapter 9 in the NLTK book]]. You can perform syntax-driven semantic analysis of the natural language input sentence (as seen in chapters 18.1 and 18.2 in the text book) by building a NLTK parser from a "feature-based context free grammar" (fcfg) that contains semantic attachments as features. [[http://www.nltk.org/book/ch09.html|Feature-based parsing is covered in chapter 9 in the NLTK book]].
  
-Building the parser is as simple as calling ''nltk.load_parser("<fcfg filename>")'' and assigning the resulting parser object to a variable like ''parser''. NLTK creates the right kind of parser by inspecting the grammar. To begin with, simply use the fcfg file provided in the examples in [[http://www.nltk.org/book/ch10.html#the-semantics-of-english-sentences|chapter 10.4 in the NLTK book]]: ''nltk_data/grammars/book_grammars/simple-sem.fcfg''.+Building the parser is as simple as calling ''nltk.load_parser("<fcfg filename>")'' and assigning the resulting parser object to a variable like ''parser''. NLTK creates the right kind of parser by inspecting the grammar. To begin with, simply use the fcfg file provided in the examples in [[http://www.nltk.org/book/ch10.html#the-semantics-of-english-sentences|chapter 10.4 in the NLTK book]]: ''nltk_data/grammars/book_grammars/simple-sem.fcfg''. [[simple-sem-fcfg|View grammar file contents]].
  
 <code python> <code python>
Line 19: Line 27:
 >>> tokens = sentence.split() >>> tokens = sentence.split()
 >>> for tree in parser.parse(tokens): >>> for tree in parser.parse(tokens):
-...     print(tree.label()['SEM'])+...     print(tree.label()['SEM'])     ## print the resulting semantic representation
 all z2.(dog(z2) -> exists z1.(bone(z1) & give(angus,z1,z2))) all z2.(dog(z2) -> exists z1.(bone(z1) & give(angus,z1,z2)))
 </code> </code>
Line 63: Line 71:
 m = nltk.Model(val.domain, val) m = nltk.Model(val.domain, val)
  
-m.evaluate("...", var_assignments)+m.evaluate("...", var_assignments) ## evaluate a semantic representation given the world model
 </code> </code>
  
Line 70: Line 78:
 ===== 3. Interactive python script ===== ===== 3. Interactive python script =====
  
-Once you have the two parts working you glue them together in a python script. You should allow users to type in sentences and see their truth value printed until they ๔ฐ›finally type "quit". When the program starts, you should print out some useful information, for instance, provide samples of sentences that would produce results.+Once you have the two parts working you glue them together in a python script. You should allow users to type in sentences and see their truth value printed until they finally type "quit". When the program starts, you should print out some useful information, for instance, provide samples of sentences that would produce results.
  
 <code> <code>
Line 82: Line 90:
 </code> </code>
  
 +**Note** that ''tree.label()['SEM']'' returns an object of type ''nltk.sem.logic.ApplicationExpression'' (as can be seen by checking its type with ''type()'') and not a string even though you can print it out. To use it in ''evaluate()'' it has to be turned into a string. The simplest way to do that is to use ''str(semantic_expression)'' or the format operator ''"%s" % semantic_representation'' which return a string.
/var/www/cadia.ru.is/wiki/data/attic/public/t-malv-15-3/8.1444133655.txt.gz ยท Last modified: 2024/04/29 13:32 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki