#!/usr/bin/python3

import sys, os
import nltk
import parser.tokenize
import parser.compare
import parser.grammar
import parser.keyword as keyword

if len(sys.argv) < 2:
    print("Please supply a filename.")
    sys.exit(1)

filename = sys.argv[1]

with open(filename, 'r') as f:
    data = f.read()

# Declare a list of keywords we might be looking for in the subjects of
# sentences in input.
compare_threshold = 3
keywordlist = keyword.KeywordList([
    keyword.Keyword("course id", "catalog id"),
    keyword.Keyword("start time", "begin time", "meeting time", "starts"),
    keyword.Keyword("end time"),
    keyword.Keyword("meeting days"),
    keyword.Keyword("email"),
    keyword.Keyword("name", "professor", "teacher")])

# Keep a list of any usable information we gather.
information_gathered = {}

# Break the input down into sentences, then into words, and position tag
# those words.
raw_sentences = parser.tokenize.split_phrases(data)
sentences = parser.tokenize.part_of_speech_tag(
        parser.tokenize.split_words(raw_sentences))

trees = parser.grammar.identify_constructs(sentences)

for index, tree in enumerate(trees):
    print("===\nSentence: %s\nNoun phrases:" %
            raw_sentences[index].replace('\n', ' '))
    for subtree in tree.subtrees(filter = lambda t: t.label() == 'NPR'):
        print("  %s" % subtree)
    print(tree)

    # Now, parse the key elements, identifying the subject and
    # object(s).
    print("Key elements:")
    subjectkw = None
    objects = []
    for subtree in tree.subtrees(filter = lambda t: t.label() == 'N' or
            t.label() == 'V'):

        # Return the element to string form, and then try to compare it
        # with the keyword list, if a subject has not already been
        # found.
        as_string = ' '.join(word for (word, tag) in subtree.leaves())

        if not subjectkw:
            # Sort the list of keywords based on how closely they
            # compare to our subject candidate, along with the exactly
            # how close they are to the subject letterwise, and select
            # the first element.

            score, kw = keywordlist.match(as_string, normalized =
                    True)

            # If even the best keyword doesn't compare to the subject,
            # drop it.
            if kw != None:
                subjectkw = kw.primary
                sentencepart = 'Subject'
            else:
                sentencepart = 'Unusable'

        # Only mark it an object if it's a noun.
        elif subtree.label() == 'N':
            # Here, we might try to parse the data further, according to
            # exactly what information we're looking for from the
            # object, such as a time, a date, or an email address. But
            # we won't, in this demo.
            objects.append(as_string)
            sentencepart = 'Object'

        else:
            sentencepart = 'Verb'

        print("  %-16s - %s" % (as_string, sentencepart))

    # Record the subject and objects from this sentence, if they are
    # both nonempty.
    if subjectkw and len(objects) > 0:
        information_gathered[subjectkw] = objects


# At the end, print out information we successfully gathered.
print("\n\n=======\nInformation Gathered:\n")
for field, data in information_gathered.items():
    print("%s: %s" % (field, ', '.join(data)))

@@ -139,16 +139,74 @@
  keyword
+%0Aimport parser.sentence%0Aimport parser.datatype as datatype
 %0A%0Aif len
@@ -550,16 +550,64 @@
 alog id%22
+,%0A        datamodel = datatype.DataType(None, 1)
 ),%0A    k
@@ -677,162 +677,403 @@
 rts%22
-),%0A    keyword.Keyword(%22end time%22),%0A    keyword.Keyword(%22meeting days%22),%0A    keyword.Keyword(%22email%22),%0A    keyword.Keyword(%22name%22, %22professor%22, %22teacher%22)
+,%0A        datamodel = datatype.TimeData(1)),%0A    keyword.Keyword(%22end time%22, %22ends%22,%0A        datamodel = datatype.TimeData(1)),%0A    keyword.Keyword(%22meeting days%22,%0A        datamodel = datatype.DataType(None, 7)),%0A    keyword.Keyword(%22email%22,%0A        datamodel = datatype.DataType(None, 1)),%0A    keyword.Keyword(%22name%22, %22professor%22, %22teacher%22,%0A        datamodel = datatype.DataType(None, 1))%0A    
 %5D)%0A%0A
@@ -1821,40 +1821,60 @@
    s
-ubjectkw = None%0A    objects = %5B%5D
+entencemodel = parser.sentence.Sentence(keywordlist)
 %0A   
@@ -2214,210 +2214,104 @@
     
-if not subjectkw:%0A            # Sort th
+# If the subtre
 e 
-l
 is
-t of keywords based on how closely they%0A            # compare to our subject candidate, along with the exactly%0A            # how close they are to the
+ a verb, then it cannot be an object, so try%0A        # to add it as a
  subject
  let
@@ -2310,13 +2310,13 @@
 ject
- lett
+. Oth
 erwi
@@ -2324,766 +2324,219 @@
 e, a
-nd select%0A            # the first element.%0A%0A            score, kw = keywordlist.match(as_string, normalized =%0A                    True)%0A%0A            # If even the best keyword doesn't compare to the subject,%0A            # drop it.%0A            if kw != None:%0A                subjectkw = kw.primary%0A                sentencepart
+dd it as either.%0A        if subtree.label()
  =
+=
  '
-Subject'%0A            else:%0A                sentencepart = 'Unusable'%0A%0A        # Only mark it an object if it's a noun.%0A        elif subtree.label() == 'N':%0A            # Here, we might try to parse the data further, according to%0A            # exactly what information we're looking for from the%0A            # object, such as a time, a date, or an email address. But%0A            # we won't, in this demo.%0A            objects.appen
+V':%0A            sentencepart = sentencemodel.new_subject(as_string)%0A        else:%0A            print(as_string)%0A            sentencepart = sentencemodel.include_wor
 d(as
@@ -2540,32 +2540,33 @@
 (as_string)%0A
+%0A
         
     sentence
@@ -2545,35 +2545,38 @@
 tring)%0A%0A        
-   
+if not
  sentencepart = 
@@ -2576,33 +2576,8 @@
 part
- = 'Object'%0A%0A        else
 :%0A  
@@ -2606,12 +2606,16 @@
  = '
-Verb
+Unusable
 '%0A%0A 
@@ -2774,88 +2774,164 @@
 if s
-ubjectkw and len(objects) %3E 0:%0A        information_gathered%5Bsubjectkw%5D = objects
+entencemodel.keyword != None and len(sentencemodel.data) %3E 0:%0A        information_gathered%5Bsentencemodel.keyword.primary%5D = %5C%0A            sentencemodel.data
 %0A%0A%0A#
@@ -3119,22 +3119,11 @@
 ld, 
-', '.join(
 data))
-)
 %0A

Use Sentence models and DataTypes in demo