Source

News Semantic Search / models.py

from google.appengine.ext import db
from django.utils.html import strip_tags
import re
from google.appengine.ext import search 

class Entity(search.SearchableModel):
    TYPES = ['Person', 'Company', 'Product', 'Country', 'City']
    type = db.StringProperty(choices=set(TYPES))
    url = db.LinkProperty()
    name = db.StringProperty()
    extra_info = db.TextProperty()


class News(db.Model):
    url = db.LinkProperty()
    body = db.TextProperty()
    title = db.StringProperty()
    thumbnail = db.LinkProperty()
    category = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True)


class Result(db.Model):
    news = db.ReferenceProperty(News)
    related_entity = db.ReferenceProperty(Entity)
    prefix = db.StringProperty()
    suffix = db.StringProperty()
    detection = db.StringProperty()
    relevance = db.FloatProperty()
    created = db.DateTimeProperty(auto_now_add=True)
    extra_info = db.TextProperty()
    
    @property
    def formated_detection(self):
        text = strip_tags(self.detection)
        return text.replace('[', '<span class="prefix-suffix">').replace(']', '</span>')


class NewsCategory(db.Model):
    url = db.LinkProperty()
    news = db.ReferenceProperty(News)
    category = db.StringProperty()
    relevance = db.FloatProperty()
    extra_info = db.TextProperty()