Created
December 16, 2018 23:18
-
-
Save dsalaj/58135f441757507baa67791c3e5bf59c to your computer and use it in GitHub Desktop.
Reber grammar generation and validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nltk | |
from nltk.parse.generate import generate | |
# Define Reber grammar | |
# http://christianherta.de/lehre/dataScience/machineLearning/neuralNetworks/pics/embeddedReberGrammar.png | |
grammarStr = ''' | |
START -> 'B' 'T' REBER 'T' 'E' | 'B' 'P' REBER 'P' 'E' | |
REBER -> 'B' E1 | |
E1 -> 'T' E2 | 'P' E3 | |
E2 -> 'S' E2 | 'X' E4 | |
E3 -> 'T' E3 | 'V' E5 | |
E4 -> 'X' E3 | 'S' E6 | |
E5 -> 'P' E4 | 'V' E6 | |
E6 -> 'E' | |
''' | |
grammar = nltk.CFG.fromstring(grammarStr) | |
sentences = set([''.join(sentence) for sentence in generate(grammar, depth=30)]) | |
print(len(sentences)) | |
def valid(string): | |
r = nltk.ChartParser(grammar).parse(string) | |
return len(list(r)) > 0 | |
print("'BTBPVVETE' is valid?", valid('BTBPVVETE')) | |
print("'BTBPVVETE' is valid?", 'BTBPVVETE' in sentences) | |
print("'BTBPVVETEE' is valid?", valid('BTBPVVETEE')) | |
print("'BTBPVVETEE' is valid?", 'BTBPVVETEE' in sentences) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment