# Path: conceptnet/corpus/models.py
# class Sentence(models.Model, ScoredModel):
#     """
#     A statement entered by a contributor, in unparsed natural language.
#     """
#     text = models.TextField(blank=False)
#     creator = models.ForeignKey(User)
#     created_on = models.DateTimeField(default=datetime.now)
#     language = models.ForeignKey(Language)
#     activity = models.ForeignKey(Activity)
#     score = models.IntegerField(default=0)
#     votes = generic.GenericRelation(Vote)
# 
#     def __unicode__(self):
#         return  u'<' + self.language.id + u': ' + \
#                 u'"' + self.text + u'"' + \
#                 u'(by:' + unicode(self.creator_id) + \
#                 u' activity:' + self.activity.name + \
#                 u')>'
#     
# 
#     def update_consistency(self):
#         """
#         Assume that the creator of this sentence voted for it, and calculate
#         the score.
#         """
#         try:
#             if self.creator is not None and self.get_rating(self.creator) is None:
#                 if self.creator.username != 'verbosity':
#                     Vote.objects.record_vote(self, self.creator, 1)
#             self.update_score()
#         except User.DoesNotExist:
#             self.creator = User.objects.get(username='_ghost')
#             Vote.objects.record_vote(self, self.creator, 1)
#             self.update_score()
# 
# class TaggedSentence(models.Model):
#     """
#     The results of running a sentence through a tagger such as MXPOST.
# 
#     We could use this as a step in parsing ConceptNet, but we currently don't.
#     """
#     text = models.TextField()
#     language = models.ForeignKey(Language)
#     sentence = models.ForeignKey(Sentence, primary_key=True)
#     
#     def tagged_words(self):
#         for part in self.text.split(" "):
#             word, tag = part.rsplit("/", 1)
#             yield word, tag
#     
#     def __unicode__(self):
#         return self.text


from pymeta.grammar import OMeta, ParseError
from conceptnet.corpus.models import Sentence, TaggedSentence
from itertools import chain
import sys
| <NP>:t1 <ADVP>:adv (<text "contain"> | <text "contains">):x1 <NP>:t2
    => dict(frame=words(SLOT1, adv, x1, SLOT2),
            relation="HasA", text1=t1, text2=t2, adv=adv)

| <NP>:t1 <BE>:x1 <ADVP>:adv <NP>:t2 <POST>
    => dict(frame=words(SLOT1, x1, adv, SLOT2),
            relation="IsA", text1=t1, text2=t2, adv=adv)

| <NP>:t1 <text "can">:x2 <ADVP>:adv <VP>:t2
    => dict(frame=words(SLOT1, x2, adv, t2),
            relation="CapableOf", text1=t1, text2=t2)

| <NP>:t1 (<text "ca n't"> | <text "cannot">):x2 <VP>:t2
    => dict(frame=words(SLOT1, x2, t2),
            relation="CapableOf", text1=t1, text2=t2, adv="not")

| <NP>:t1 <ADVP>:adv <VP>:t2
    => dict(frame=words(SLOT1, adv, SLOT2),
            relation="CapableOf", text1=t1, text2=t2, adv=adv)
)
"""

parser = PatternParserBase.makeGrammar(metapatterns, globals(), name="Metachunker")
def parse(tagged_sent):
    try:
        return parser(tagged_sent).apply("assertion")
    except ParseError:
        return None
print parser("ball_NN").apply("NN")
print parser("Sometimes_RB ball_NN causes_VBZ competition_NN").apply("assertion")

# Path: conceptnet/corpus/models.py
# class Sentence(models.Model, ScoredModel):
#     """
#     A statement entered by a contributor, in unparsed natural language.
#     """
#     text = models.TextField(blank=False)
#     creator = models.ForeignKey(User)
#     created_on = models.DateTimeField(default=datetime.now)
#     language = models.ForeignKey(Language)
#     activity = models.ForeignKey(Activity)
#     score = models.IntegerField(default=0)
#     votes = generic.GenericRelation(Vote)
# 
#     def __unicode__(self):
#         return  u'<' + self.language.id + u': ' + \
#                 u'"' + self.text + u'"' + \
#                 u'(by:' + unicode(self.creator_id) + \
#                 u' activity:' + self.activity.name + \
#                 u')>'
#     
# 
#     def update_consistency(self):
#         """
#         Assume that the creator of this sentence voted for it, and calculate
#         the score.
#         """
#         try:
#             if self.creator is not None and self.get_rating(self.creator) is None:
#                 if self.creator.username != 'verbosity':
#                     Vote.objects.record_vote(self, self.creator, 1)
#             self.update_score()
#         except User.DoesNotExist:
#             self.creator = User.objects.get(username='_ghost')
#             Vote.objects.record_vote(self, self.creator, 1)
#             self.update_score()
# 
# class TaggedSentence(models.Model):
#     """
#     The results of running a sentence through a tagger such as MXPOST.
# 
#     We could use this as a step in parsing ConceptNet, but we currently don't.
#     """
#     text = models.TextField()
#     language = models.ForeignKey(Language)
#     sentence = models.ForeignKey(Sentence, primary_key=True)
#     
#     def tagged_words(self):
#         for part in self.text.split(" "):
#             word, tag = part.rsplit("/", 1)
#             yield word, tag
#     
#     def __unicode__(self):
#         return self.text


# Path: conceptnet/corpus/parse/metapattern.py
from pymeta.grammar import OMeta, ParseError
from conceptnet.corpus.models import Sentence, TaggedSentence
from itertools import chain
import sys

| <NP>:t1 <ADVP>:adv (<text "contain"> | <text "contains">):x1 <NP>:t2
    => dict(frame=words(SLOT1, adv, x1, SLOT2),
            relation="HasA", text1=t1, text2=t2, adv=adv)

| <NP>:t1 <BE>:x1 <ADVP>:adv <NP>:t2 <POST>
    => dict(frame=words(SLOT1, x1, adv, SLOT2),
            relation="IsA", text1=t1, text2=t2, adv=adv)

| <NP>:t1 <text "can">:x2 <ADVP>:adv <VP>:t2
    => dict(frame=words(SLOT1, x2, adv, t2),
            relation="CapableOf", text1=t1, text2=t2)

| <NP>:t1 (<text "ca n't"> | <text "cannot">):x2 <VP>:t2
    => dict(frame=words(SLOT1, x2, t2),
            relation="CapableOf", text1=t1, text2=t2, adv="not")

| <NP>:t1 <ADVP>:adv <VP>:t2
    => dict(frame=words(SLOT1, adv, SLOT2),
            relation="CapableOf", text1=t1, text2=t2, adv=adv)
)
"""

parser = PatternParserBase.makeGrammar(metapatterns, globals(), name="Metachunker")
def parse(tagged_sent):
    try:
        return parser(tagged_sent).apply("assertion")
    except ParseError:
        return None
print parser("ball_NN").apply("NN")
print parser("Sometimes_RB ball_NN causes_VBZ competition_NN").apply("assertion")

for sent in TaggedSentence.objects.all():