A Flask-based web application for generating syntax trees from sentences using generative grammar rules, spaCy POS tagging, NLTK parsing, and SVG rendering.
The application allows users to enter a sentence, select a grammar mode, optionally add custom grammar rules, choose a spaCy language model, and generate visual syntax trees together with bracket notation and the full generated grammar.
This project combines:
- Flask for the web application
- Gunicorn for production serving
- spaCy for POS tagging and linguistic annotation
- NLTK for context-free grammar parsing
- svgling for SVG syntax tree rendering
- Docker for containerized deployment (Dockerfile included)
The project is useful for demonstrating basic concepts in syntax, generative grammar, POS tagging, ambiguity, relative clauses, conjunctions, and grammar-based parsing.
- Generate syntax trees from user-provided sentences
- Use a default generative grammar
- Extend the default grammar with additional user-defined rules
- Use a completely custom grammar
- Select English or German spaCy POS tagging
- Display generated syntax trees as SVG graphics
- Display bracket notation for generated trees
- Display the complete grammar used for parsing
- Includes demo sentences for testing
- Docker-ready deployment
- Gunicorn production setup
- Optimized for small server deployments by using one Gunicorn worker
| Component | Purpose |
|---|---|
| Flask | Web framework |
| Gunicorn | Production WSGI server |
| spaCy | POS tagging and linguistic annotation |
| NLTK | Context-free grammar parsing |
| svgling | Syntax tree SVG rendering |
| Docker | Containerization |
flask_syntax_tree_generator/
│
├── app.py
├── requirements.txt
├── Dockerfile
├── docker-compose.yml
│
├── syntax/
│ ├── syntax.py
│ └── syntax_tree_generator.ipynb
│
├── templates/
│ ├── index.html
│ └── syntax.html
│
├── static/
│ ├── favicon.ico
│ └── styles/
│ └── style.css
│
├── .dockerignore
├── .gitignore
└── README.md
Main Flask application file.
It defines the web routes:
| Route | Purpose |
|---|---|
/ |
Landing page |
/syntax |
Syntax tree output page |
/parse-sentence/<inputData> |
Parses input data and generates syntax trees |
/favicon.ico |
Serves the favicon |
The route /parse-sentence/<inputData> extracts:
- sentence
- grammar rules
- grammar option
- language option
Then it calls:
generate_syntaxtree(sentence, grammar_rules, option_grammar, option_lang)from:
syntax/syntax.pyContains the core parsing logic.
Main responsibilities:
- Load spaCy language models
- Format sentence input
- POS-tag the input sentence
- Generate POS-based grammar rules
- Combine default grammar and custom rules
- Parse the sentence with NLTK
ChartParser - Render syntax trees as SVG
- Return:
- syntax tree SVGs
- bracket notation
- grammar used for parsing
The application supports three grammar modes.
Uses the predefined grammar:
S -> NP VP
S -> NP RC
S -> 'If' S 'then' S
VP -> VERB
VP -> VERB NP
VP -> VERB ADJ
VP -> VP PP
VP -> AUX VP
VP -> AUX 'not' VP
VP -> VP CCONJ VP
NP -> NOUN
NP -> DET NOUN
NP -> DET PROPN
NP -> PROPN
NP -> PRON
NP -> NP PP
NP -> NP RC
NP -> NP CCONJ NP
PP -> P NP
RC -> RPRON S
The sentence is POS-tagged by spaCy, and terminal rules are generated automatically.
Example:
He sees a piano
may generate terminal rules such as:
PRON -> 'He'
VERB -> 'sees'
DET -> 'a'
NOUN -> 'piano'
Uses the default grammar and extends it with user-defined rules.
This is useful when the default grammar does not cover a specific construction or terminal category.
Example:
VERB -> 'is'
Uses only the grammar rules provided by the user.
Example:
S -> NP VP
NP -> D N
VP -> V NP
D -> 'a'
N -> 'fish'
N -> 'He'
V -> 'eats'
The application supports POS tagging with spaCy.
| Option | spaCy Model |
|---|---|
| English | en_core_web_sm or en_core_web_lg |
| German | de_core_news_sm or de_core_news_lg |
For production on small servers, the smaller models are recommended:
en_core_web_sm
de_core_news_sm
The large models require significantly more RAM:
en_core_web_lg
de_core_news_lg
Create a virtual environment & Install Depencies:
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txtRun the app with Gunicorn:
gunicorn app:app --bind 0.0.0.0:5000 --workers 1 --timeout 120Open the application:
http://localhost:5000
Development by Deniz Inan.
