Skip to content

Instantly share code, notes, and snippets.

@schar
Last active March 23, 2022 14:37
Show Gist options
  • Save schar/e0abd9cd60b40caeb2a47481445939e9 to your computer and use it in GitHub Desktop.
Save schar/e0abd9cd60b40caeb2a47481445939e9 to your computer and use it in GitHub Desktop.
10-lines for CKY parsing in Haskell
mkChart :: (Eq cat, Eq term) =>
CFG cat term -> [term] -> [((Int, Int), [cat])]
mkChart g xs = helper (0,1) [] where
helper p@(i,j) tab
| j>length xs = tab
| i<0 = helper (j,j+1) tab
| i==j-1 = helper (i-1,j) $
(p, [n | n:-t <- g, t==xs!!(j-1)]):tab
| otherwise = helper (i-1,j) $
(p, [n | n:>(l,r) <- g, k <- [i+1..j-1],
lc <- fromJust $ lookup (i,k) tab, l==lc,
rc <- fromJust $ lookup (k,j) tab, r==rc]):tab
parseCYK :: (Eq cat, Eq term) => CFG cat term -> [term] -> [cat]
parseCYK g xs = snd $ head $ mkChart g xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment