Skip to content

About

A Flask-based web application for generating syntax trees from sentences using generative grammar rules, spaCy POS tagging, NLTK parsing, and SVG rendering.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Flask Syntax Tree Generator

screenshot_Syntax_Tree_Generator.png

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.


Project Overview

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.


Main Features

  • 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

Technology Stack

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

Project Structure

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

Core Files

app.py

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.py

syntax/syntax.py

Contains 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

Grammar Modes

The application supports three grammar modes.

1. Default Grammar

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'

2. Default Grammar + Additional Rules

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'

3. Own Grammar

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'

Language Options

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

Installation

Create a virtual environment & Install Depencies:

python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt

Run the app with Gunicorn:

gunicorn app:app --bind 0.0.0.0:5000 --workers 1 --timeout 120

Open the application:

http://localhost:5000

Author

Development by Deniz Inan.


About

A Flask-based web application for generating syntax trees from sentences using generative grammar rules, spaCy POS tagging, NLTK parsing, and SVG rendering.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages