├── LICENSE ├── README.md └── Grammars.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Anthony H 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GramPy 2 | 3 | An easily readable context-free grammar text generator! 4 | 5 | 6 | generate_items() function cotrbuted by https://github.com/tgsachse 7 | 8 | Big thanks to https://github.com/Caleb-Shepard for help with the thinking this through! 9 | 10 | Inspired by Daniel Shiffman's CFG coding challenge: https://www.youtube.com/watch?v=8Z9FRiW2Jlc 11 | 12 | If you're new to using CFG's for text generation Daniel Shiffman's video on the subject is a great resource: https://www.youtube.com/watch?v=Rhqk9HYiB7Q 13 | 14 | ## How to use 15 | * Our "Rules" dict is composed of a string key and is a list of lists 16 | * The generate_items() function is used to insert our list of strings element by element. 17 | * The expansion() function takes an input list and compares each element to see if it is one of our Rule's keys. If so it then randomly selects a corresponding list from that key value and replaces it in the list. After the initial for loop we perform another check to make sure our list has expanded correctly and if so we call it recursively until our list contains no more non-terminal values 18 | 19 | 20 | ## What to do 21 | * Provide more detailed examples of using GramPy with more then just text 22 | * Provide functions to read and import grammars from JSON, and CSV values 23 | -------------------------------------------------------------------------------- /Grammars.py: -------------------------------------------------------------------------------- 1 | import random as rd 2 | 3 | # Example Grammar rules 4 | rules = { 5 | "S":[ 6 | ["The", "N", "V"] 7 | ], 8 | "N":[ 9 | ["Adj","cat"], 10 | ["Adj","dog"], 11 | ["Adj","tiger"] 12 | ], 13 | "V":[ 14 | ["meows", "A"], 15 | ["barks", "A"], 16 | ["moos", "A"] 17 | ], 18 | "Adj": [ 19 | ["Stinky"], 20 | ["Giant"], 21 | ["Blue"] 22 | ], 23 | "A": [ 24 | ["and", "the", "N", "Va"] 25 | ], 26 | "Va": [ 27 | ["sings"], 28 | ["yawns"], 29 | ["eats grass"] 30 | ] 31 | } 32 | 33 | # Contributed by Tiger Sachase 34 | # Used to parse any list of strings and insert them in place in a list 35 | def generate_items(items): 36 | for item in items: 37 | if isinstance(item, list): 38 | for subitem in generate_items(item): 39 | yield subitem 40 | else: 41 | yield item 42 | 43 | # Our expansion algo 44 | def expansion(start): 45 | for element in start: 46 | if element in rules: 47 | loc = start.index(element) 48 | start[loc] = rd.choice(rules[element]) 49 | result = [item for item in generate_items(start)] 50 | 51 | for item in result: 52 | if not isinstance(item, list): 53 | if item in rules: 54 | result = expansion(result) 55 | 56 | return result 57 | 58 | 59 | def to_string(result): 60 | return ' '.join(result) 61 | 62 | 63 | # An example test you can run to see it at work 64 | result = ["S"] 65 | print(result) # Print our starting result 66 | result = expansion(result) # Expand our starting list 67 | final = to_string(result) 68 | print(final) # Print the final result 69 | 70 | --------------------------------------------------------------------------------