Skip to content

Instantly share code, notes, and snippets.

@dmoney
Last active August 29, 2015 14:22
Show Gist options
  • Save dmoney/046c53bf459dc69d0937 to your computer and use it in GitHub Desktop.
Save dmoney/046c53bf459dc69d0937 to your computer and use it in GitHub Desktop.
# sentence_diagram.pseu.py
# A doodle in sentence diagramming using Python.
# Author: Dustin King ([email protected], twitter.com/cathodion)
#
# By "doodle" I mean not actually functional, but if this
# sentence diagramming library existed, this is what part of
# a client program would look like.
#
# Based on the example sentence and diagram given at:
# http://www.npr.org/sections/ed/2014/08/22/341898975/a-picture-of-language-the-fading-art-of-diagramming-sentences
#
# "As Gregor Samsa awoke one moring from uneasy dreams
# he found himself transformed in his bed into a monstrous vermin."
example_sentence=sentence(
clause("he", "found", direct_object="himself",
complement=[
word("transformed", modifies="himself"),
prep_phrase("in", "bed", word("his", modifies="bed")),
prep_phrase("into", "vermin",
word("a", modifies="vermin"),
word("monstrous", modifies="vermin"))])
clause("George Samsa", "awoke",
link=conjunction("As", type=Subordinating, modifies="found"),
prep_phrase(None, "morning", word("one", modifies="morning")),
prep_phrase("from", "dreams", word("uneasy", modifies="dreams"))))
__doc__= """
We had a lesson on this in grade school but it never came up again. I don't think
it's so useful for teaching English, but as an intro to linguistics. Even if you
don't do it visually, your brain must assemble a sentence into some kind of structure
like this, with each component having a particular role, and the components having
particular relationships to each other. Computer languages work the same way, but they
are designed to be relatively easily broken down like this by a computer program
(called a "parser"), whereas natural languages have evolved organically, and therefore
require some (mostly unconscious) intelligence to determine which of the possible
structures a given sentence is intended to mean (I know almost nothing about Natural
Language Processing, but I do know it's not a completely solved problem, or Siri would
always know what I mean). Where was I going with this? Something to do with how a
sentence diagram of English is like a parse tree of a computer language.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment