LING%581:%Advanced%Computaonal% Linguiscssandiway/ling581-15/lecture11.pdf · –...

65
LING 581: Advanced Computa7onal Linguis7cs Lecture Notes April 16th

Transcript of LING%581:%Advanced%Computaonal% Linguiscssandiway/ling581-15/lecture11.pdf · –...

LING  581:  Advanced  Computa7onal  Linguis7cs  

Lecture  Notes  April  16th  

Administrivia  

•  Factoid  Ques-on  Answering  homework  – did  you  submit  your  simula.on?  

Seman7cs  

•  New  topic!  – We  want  computers  to  be  able  to  understand  sentences,  

– model  the  world,    – compute  meaning,  truth  values,  entailments  etc.  

Meaning  •  What  is  a  meaning  and  how  do  we  represent  it?  

–  difficult  to  pin  down  precisely  for  computers  –  even  difficult  for  humans  some.mes…  

•  Example:  word  dog  –  by  reference  to  other  words  

•  Merriam-­‐Webster:  a  highly  variable  domes.c  mammal  (Canis  familiaris)  closely  related  to  the  gray  wolf  

–  transla7on  •  犬  (inu,  Japanese)  =  狗  (gou,  Chinese)  =  “dog”  (English)  

–  Computer:    •  meaning  è  formal  concept  (or  thought  or  idea)  •  “dog”  maps  to  DOG  •  <word>  maps  to  <concept>  •  need  to  provide  a  concept  for  every  meaningful  piece  of  language?  

Understanding  •  Suppose  we  write  a  computer  program  to  compute  the  meaning  of  

sentences  •  Ques7on:  does  it  understand  sentences?  •  How  do  you  know?    •  Ask  ques7ons?  

•  Turing  test:    –  converse  with  a  human,  convince  human  the  computer  is  a  human  

•  Searle’s  Chinese  room  experiment  (adapted)  –  suppose  we  have  a  Perl/Python/Prolog  program  capable  of  processing  

Chinese,  and  we  “run”  the  program  manually  –  i.e.  we  carry  out  the  instruc.ons  of  the  program  –  do  we  understand  Chinese?  

•  Weak  AI  /  Strong  AI  

Truth  Condi7ons  and  Values  

•  What  is  the  meaning  of  a  sentence  anyway?  •  What  is  Meaning?  (Portner  2005)  

•  Example:  the  circle  is  inside  the  square  – We  can  draw  a  picture  of  scenarios  for  which  the  statement  is  true  and  the  statement  is  false  

•  Proposi7on  expressed  by  a  sentence  is  its  truth-­‐condi7ons  –  “under  what  condi7ons  a  sentence  is  true”  –  i.e.  sets  of  possible  worlds  (aka  situa7ons)  –  truth-­‐condi.ons  different  from  truth-­‐value  

Truth  Condi7ons  and  Values  

•  Example:  –  The  circle  is  inside  the  square  and  the  circle  is  dark  – What  is  the  meaning  of  and  here?  –  and  =  set  intersec7on  (of  scenarios)  –  [The  circle  is  inside  the  square]  and  [the  circle  is  dark]  

•  Example:  – Mary  is  a  student  and  a  baseball  fan  –  and  =  set  intersec7on  (of  ???)  – Mary  is  [a  student]  and  [a  baseball  fan]  

Truth  Condi7ons  and  Values  

•  Example:  – Mary  and  John  bought  a  book  – Does  and  =  set  intersec7on?  – Are  Mary  and  John  sets  anyway?  

–  [Mary]  and  [John]  bought  a  book  

– Set  intersec7on  =  ∅  – how  about  “and  =  set  union”  then?  

Truth  Condi7ons  and  Values  

•  Example:  – The  square  is  bigger  than  the  circle  – The  circle  is  smaller  than  the  square  

– Are  they  synonymous?  – Are  they  contradictory?  –  Is  there  an  entailment  rela7onship?  – Are  they  tautologies?  

More  examples  •  1.  Does  sleep  entail  snore?  

A.  He  is  sleeping  entails  He  is  snoring  B.  He  is  snoring  entails  He  is  sleeping  

•  2.  Does  snore  presuppose  sleep?  •  3.  What  does  “when  did  you  stop  bea.ng  your  wife?”  presuppose?  

•  3.  Given  the  statement  “All  crows  are  black”,        give  an  example  of  a  sentence  expressing  a        tautology  involving  this  statement?  –  Stmt  or  nega7on  Stmt  

Proposi7onal  Logic  

•  Recall  the  dis7nc7on  between  truth  condi7ons  and  truth  values  …  

•  Possible  world  or  situa7on:  – we  can  create  a  possible  world  in  Prolog  by  asser7ng  (posi7ve)  facts  into  its  database  

– Prolog  use  the  closed  world  assump7on  •  i.e.  things  not  explicity  stated  to  be  true  are  assumed  to  be  false  

Proposi7onal  Logic  Cheat  sheet  •  Star7ng  SWI  Prolog  from  Terminal/Shell:  

–  swipl            (if  in  PATH)  –  /opt/local/bin/swipl    (default  install  loca7on  on  my  mac)  

^D  (control-­‐D)  or  halt.  to  quit  

Proposi7onal  Logic  Cheat  sheet  •  Viewing  the  database:  

–  listing.

•  Assert  (and  delete)  facts  at  the  command  line  directly  using  predicates:  –  assert(fact).–  retract(fact).

•  Put  facts  into  a  file  and  load  file  –  [filename].      (assumed  to  have  extension  .pl)  –  (or  via  pull-­‐down  menu  in  Windows)  

•  Proposi7ons:  –  named  beginning  with  a  lower  case  lejer  (not  number,  not  star7ng  with  capital  lejer  or  

underscore:  variable  –  no  variables  in  proposi7onal  logic),  examples:  

–  assert(p).        (makes  p  true  in  this  situa7on)  –  p. (asks  Prolog  if  p  true  in  this  situa7on)  –  dynamic q.        (registers  proposi7on  q,  prevents  error  message)  

Proposi7onal  Logic  

•  Example:  

Note:  meta-­‐level  predicates  like  dynamic  and  assertevaluate  to  true  if  they  succeed  

Proposi7onal  Logic  •  Proposi7ons  can  be  combined  using  logical  connec7ves  and  

operators  –  Conjunc7on    p  ,  q.  –  Disjunc7on    p  ;  q.  –  Nega7on      \+  p.  

•  Not  directly  implemented  in  Prolog  –  Implica7on    p  -­‐>  q.    (IS  NOT  THIS!!!)  

can’t  add  p, q. to  the  database  can  only  query  it  

Use  parentheses  ( ) to  restrict/clarify  scope  

needs  both  p  and  q  to  be  true,  see  next  slide  

Proposi7onal  Logic  

•  Help:  –  ?- help(->).–  true.

takes  a  very  long  7me  for  this  window  to  pop  up  …  it  uses  the  X11  Window  system,  which  may  or  may  not  exist  on  your  system  

IF  -­‐>  THEN  ;  ELSE  is  a  programming  construct  

Proposi7onal  Logic  •  Also  not  implemented  

–  Logical  equality      p = q.

Proposi7onal  Logic  •  Help:  

Not  quite  the  right  documenta7on  page  =  is  unifiability  in  Prolog  

Proposi7onal  Logic  •  Prolog  exercise:    

–  evaluate  formula  below  for  different  truth  values  of  A  and  B  

From  wikipedia  

Proposi7onal  Logic  

•  How  to  demonstrate  a  proposi7onal  formula  is  a  tautology?  

•  One  answer:  exhaus7vely  enumerate  a  truth  table  

hjp://en.wikipedia.org/wiki/Truth_table  

Proposi7onal  Logic  •  Example:  

(A   ,   B)   ;   (\+   A)   ;   (\+   B)  (A   ,   B)   ;   (\+   A)   ;   (\+   B)  

T   T   T   T  

T   F   T   F  

F   T   F   T  

F   F   F   F  

(A   ,   B)   ;   (\+   A)   ;   (\+   B)  

T   T   T   T   T  

T   F   F   T   F  

F   F   T   F   T  

F   F   F   F   F  

(A   ,   B)   ;   (\+   A)   ;   (\+   B)  

T   T   T   F   T   F   T  

T   F   F   F   T   T   F  

F   F   T   T   F   F   T  

F   F   F   T   F   T   F  

(A   ,   B)   ;   (\+   A)   ;   (\+   B)  

T   T   T   F   T   F   F   T  

T   F   F   F   T   T   T   F  

F   F   T   T   F   T   F   T  

F   F   F   T   F   T   T   F  

(A   ,   B)   ;   (\+   A)   ;   (\+   B)  

T   T   T   F   T   F   F   T  

T   F   F   F   T   T   T   F  

F   F   T   T   F   T   F   T  

F   F   F   T   F   T   T   F  

(A   ,   B)   ;   (\+   A)   ;   (\+   B)  

T   T   T   T   F   T   F   F   T  

T   F   F   T   F   T   T   T   F  

F   F   T   T   T   F   T   F   T  

F   F   F   T   T   F   T   T   F  

table  has  2n  rows,  where  n  is  the  number  of  proposi7onal  elements  complexity:  exponen7al  in  n    

Proposi7onal  Logic  

•  Other  connec7ves  (are  non-­‐primi7ve)  

Proposi7onal  Logic  

•  Other  connec7ves  (are  non-­‐primi7ve)  

aka  p  ↔  q  

•  From  1st  and  4th  line  of  truth  table,  we  can  easily  deduce  how  to  simulate  p  ↔  q  in  Prolog  using  ,    ;  and  \+  

Proposi7onal  Logic  

hjp://en.wikipedia.org/wiki/Tautology_(logic)  

Let’s  prove  the  law  of  contraposi7on  

Proposi7onal  Logic  

•  Prove  both  sides  of  De  Morgan’s  Laws:  

Note:  De  Morgan’s  laws  tell  us  we  can  do  without  one  of  conjunc7on  or  disjunc7on.  Why?  

Proposi7onal  Logic  

•  It’s  easy  to  write  a  short  program  in  Prolog  to  automate  all  this  …  

Program:  plogic.pl  

Proposi7onal  Logic  •  Example  using  try/2:  

It's  a  tautology!  true  under  all  possible  condi7ons  

Proposi7onal  Logic  •  We  can  get  a  bit  fancier,  support  -­‐>  and  <-­‐>  

Program:  plogic2.pl  

Proposi7onal  Logic  •  We  can  get  even  fancier;  eliminate  having  to  supply  the  

proposi7onal  variables  

Program:  plogic3.pl  

Truth  table  enumera7on  

•  Parsing  the  formula:  

11.  \+  X  converts  to  \+  A  if  (subformula)  X  converts  to  A  12.  X,Y  converts  to  A,B  if  X  converts  to  A  and  Y  converts  to  B  13.  X;Y  converts  to  A;B  if  X  converts  to  A  and  Y  converts  to  B  14.  X-­‐>Y  converts  to  \+A;B  if  X  converts  to  A  and  Y  converts  to  B  15.  X<-­‐>Y  converts  to  (A,B)  ;  (\+A,\+B)  if  X  converts  to  A  and  Y  converts  to  B  16.  X  converts  to  X  and  add  X  to  the  list  of  proposi7onal  variables  if  it  isn’t  already  in  the  list  

Proposi7onal  Logic  Program:  plogic3.pl  

Seman7c  Grammars  •  Use  slides  from  course  –  LING  324  –  Introduc-on  to  Seman-cs  –  Simon  Frasier  University,  Prof.  F.J.  Pelle7er  –  hjp://www.sfu.ca/~jeffpell/Ling324/ypSlides4.pdf  

•  Difference  is  we’re  computa.onal  linguists…      so  we’re  going  to  implement  the  slides  

•     •  We’ll  do  the  syntax  part  this  lecture,  and  the  seman7cs  next  7me  

Syntax  

ypSlides4.pdf  Slide  3  

Syntax  •  We  already  know  how  to  build  Prolog  grammars  •  See  –  hjp://www.swi-­‐prolog.org/pldoc/doc_for?object=sec7on(2,'4.12',swi('/doc/Manual/DCG.html'))  

for  the  execu7ve  summary  

Syntax  

•  Class  exercise  

Syntax  •  Step  1:  let’s  build  the  simplest  possible  Prolog  grammar  for  this    

ypSlides4.pdf  Slide  4  

Simplest  possible  grammar  Excluding  (2b)  for  the  7me  being  g1.pl  

Simplest  possible  grammar  

Examples  (3),  (4)  and  (5)  from  two  slides  back  

Syntax  •  Step  2:  let’s  add  the  parse  tree  component  to  our  grammar  …  

Recall:  grammar  rules  can  have  extra  arguments  (1)  Parse  tree  (2)  Implement  agreement  etc.  

Syntax  Note:  on  handling  lez  recursion  in  Prolog  grammar  rules  •  techniques:    

1.  use  a  bojom-­‐up  parser      2.  rewrite  grammar        (lez  recursive  -­‐>  right  recursive)  3.  or  use  lookahead        (today’s  lecture)  

lookahead  is  a  dummy  nonterminal  that  does  not  contribute  to  the  parse,  it  is  a  “guard”  that  prevents  rule  from  firing  unless  appropriate    lookahead  succeeds  if  it  can  find  a  conjunc7on  in  the  input  and  marks  it  (so  it  can’t  find  it  twice)  

Grammar:  version  2  g2.pl  

Grammar:  version  2  

Grammar:  version  2  

Examples  (3),  (4)  and  (5)  again  from  slide  9  

Grammar:  version  2  

Examples  (6)  and  (7)  from  slide  9  

Seman7cs  

•  We  want  to  obtain  a  seman7c  parse  for  our  sentences  that  we  can  “run”  (i.e.  evaluate)  against  the  Prolog  database  (i.e.  situa7on  or  possible  world).  

•  So  the  seman7c  parse  should  be  valid  Prolog  code  (that  we  can  call)  

•  We’ll  need  (built-­‐in)  member/2  and  setof/3  defined  in  the  following  2  slides  (a  quick  review)  

setof/3  •  See  

–  hjp://www.swi-­‐prolog.org/pldoc/doc_for?object=sec7on(2,'4.29',swi('/doc/Manual/allsolu7ons.html'))  

•  SWI  Prolog  built-­‐in:  

setof/3  

•  Example:  

member/2  •  See  –  hjp://www.swi-­‐prolog.org/pldoc/man?predicate=member%2F2  

Seman7cs  

ypSlides4.pdf  Slide  7  

Seman7cs  

ypSlides4.pdf  Slide  8  

Seman7cs  

ypSlides4.pdf  Slide  9  

Seman7cs  

ypSlides4.pdf  Slide  10  

Seman7cs:  Implementa7on  

•  Desired  implementa7on:  

The  extra  argument  returns  a  Prolog  query  that  can  be  evaluated  against  the  database  

Note:  we  are  bypassing  the  (explicit)  construc7on  of  the  syntax  tree  Imagine  if  the  Penn  Treebank  was  labeled  using  a  seman.c  representa.on  

Seman7cs:  Implementa7on  •  Let’s  write  the  seman7c  grammar  to  handle  “Jack  is  hungry”  

–  first,  let’s  introduce  a  bit  of  nota7on  (lambda  calculus)  –  λ  =  func7on  –  λx.x+1  denotes  a  func7on  that  takes  an  argument  x  and  computes  

value  x+1    •  (a  period  separates  the  argument  from  the    func.on  body)  

–  (λx.x+1)(5)  means  apply  5  to  the  lambda  func7on  •  subs7tute  5  in  place  of  x  and  evaluate  •  answer  =  6  

Seman7cs:  Implementa7on  

setof(X,hungry(X),S) jack

setof(X,hungry(X),S), member(jack,S)

Syntax:  

Seman7cs:  Implementa7on  •  Seman7c  grammar:  

Seman7cs:  Implementa7on  •  Seman7c  grammar:  

Seman7cs:  Implementa7on  

•  More  examples  of  computa7on:  ypSlides4.pdf  Slide  10  

Seman7cs:  Implementa7on  

•  More  examples  of  computa7on:  

Seman7cs:  Implementa7on  

•  More  examples  of  computa7on:  

Seman7cs  

ypSlides4.pdf  Slide  11  

Seman7cs:  Implementa7on  

•  Scope  of  nega7on:  wide  or  narrow  

narrow  

wide  

Grammar:  version  3  g3.pl  

Grammar:  version  3  

Evalua7on  

•  Check  our  computer  implementa7on  on…  

ypSlides4.pdf  Slide  12