Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from __future__ import print_function
- import nltk
- from nltk.probability import DictionaryProbDist
- from nltk.parse.generate import generate
- from nltk.grammar import CFG
- import random
- import sys
- sentences = []
- grammar = CFG.fromstring("""
- S -> NP VP
- VP -> TV NP
- VP -> IV
- VP -> DatV NP NP
- TV -> 'saw'
- IV -> 'ate'
- DatV -> 'gave'
- NP -> 'telescopes'
- NP -> 'Jack'
- """)
- for sentence in generate(grammar, n=100):
- sentences.append(' '.join(sentence))
- print(random.choice(sentences))
- grammar = grammar.FeatureGrammar.fromstring("""
- % start DP
- DP[AGR=?a] -> D[AGR=?a] N[AGR=?a]
- N[AGR=[NUM='sg', GND='m']] -> 'boy'
- D[AGR=[NUM='sg', PERS=3]] -> 'this' | 'that'
- D[AGR=[NUM='pl', PERS=3]] -> 'these' | 'those'
- D[AGR=[NUM='pl', PERS=1]] -> 'we'
- D[AGR=[PERS=2]] -> 'you'
- N[AGR=[NUM='pl', GND='m']] -> 'boys'
- N[AGR=[NUM='sg', GND='f']] -> 'girl'
- N[AGR=[NUM='pl', GND='f']] -> 'girls'
- N[AGR=[NUM='sg']] -> 'student'
- N[AGR=[NUM='pl']] -> 'students'
- """)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement