Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import nltk
  4. from nltk.probability import DictionaryProbDist
  5. from nltk.parse.generate import generate
  6. from nltk.grammar import CFG
  7. import random
  8. import sys
  9.  
  10. sentences = []
  11.  
  12. grammar = CFG.fromstring("""
  13. S -> NP VP
  14. VP -> TV NP
  15. VP -> IV
  16. VP -> DatV NP NP
  17. TV -> 'saw'
  18. IV -> 'ate'
  19. DatV -> 'gave'
  20. NP -> 'telescopes'
  21. NP -> 'Jack'
  22.  
  23. """)
  24.  
  25. for sentence in generate(grammar, n=100):
  26. sentences.append(' '.join(sentence))
  27.  
  28. print(random.choice(sentences))
  29.  
  30. grammar = grammar.FeatureGrammar.fromstring("""
  31. % start DP
  32. DP[AGR=?a] -> D[AGR=?a] N[AGR=?a]
  33. N[AGR=[NUM='sg', GND='m']] -> 'boy'
  34. D[AGR=[NUM='sg', PERS=3]] -> 'this' | 'that'
  35. D[AGR=[NUM='pl', PERS=3]] -> 'these' | 'those'
  36. D[AGR=[NUM='pl', PERS=1]] -> 'we'
  37. D[AGR=[PERS=2]] -> 'you'
  38. N[AGR=[NUM='pl', GND='m']] -> 'boys'
  39. N[AGR=[NUM='sg', GND='f']] -> 'girl'
  40. N[AGR=[NUM='pl', GND='f']] -> 'girls'
  41. N[AGR=[NUM='sg']] -> 'student'
  42. N[AGR=[NUM='pl']] -> 'students'
  43. """)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement