'Filter unstructured tweets'

@staticmethod
def parse_twitter(message):

    db = current.db
    s3db = current.s3db
    cache = s3db.cache
    priority = 0
    category = 'Unknown'
    service = 'twitter'
    table = s3db.msg_sender
    ctable = s3db.pr_contact
    query = (((table.deleted == False) & (ctable.pe_id == table.pe_id)) & (ctable.contact_method == 'TWITTER'))
    senders = db(query).select(table.priority, ctable.value, cache=cache)
    for s in senders:
        if (sender == s[ctable].value):
            priority += s[table].priority
            break
    ktable = s3db.msg_keyword
    keywords = db((ktable.deleted == False)).select(ktable.id, ktable.keyword, ktable.incident_type_id, cache=cache)
    incident_type_represent = S3Represent(lookup='event_incident_type')
    if NLTK:
        synonyms = {}
        for kw in keywords:
            syns = []
            try:
                synsets = wn.synsets(kw.keyword)
                for synset in synsets:
                    syns += [lemma.name for lemma in synset.lemmas]
            except LookupError:
                nltk.download('wordnet')
                synsets = wn.synsets(kw.keyword)
                for synset in synsets:
                    syns += [lemma.name for lemma in synset.lemmas]
            synonyms[kw.keyword.lower()] = syns
    ltable = s3db.gis_location
    query = ((ltable.deleted != True) & (ltable.name != None))
    locs = db(query).select(ltable.id, ltable.name, cache=cache)
    lat = lon = None
    location_id = None
    loc_matches = 0
    words = message.split('    ')
    index = 0
    max_index = (len(words) - 1)
    for word in words:
        word = word.lower()
        if (word.endswith('.') or word.endswith(':') or word.endswith(',')):
            word = word[:(-1)]
        skip = False
        if (word in ('safe', 'ok')):
            priority -= 1
        elif (word in 'help'):
            priority += 1
        elif ((service == 'twitter') and (word == 'RT')):
            priority -= 1
            skip = True
        if word.startswith('http://'):
            priority += 1
            skip = True
        if (index < max_index):
            if (word == 'lat'):
                skip = True
                try:
                    lat = words[(index + 1)]
                    lat = float(lat)
                except:
                    pass
            elif (word == 'lon'):
                skip = True
                try:
                    lon = words[(index + 1)]
                    lon = float(lon)
                except:
                    pass
        if (not skip):
            for kw in keywords:
                _word = kw.keyword.lower()
                if (_word == word):
                    if (index and (words[(index - 1)].lower() == 'no')):
                        pass
                    else:
                        category = incident_type_represent(kw.incident_type_id)
                        break
                elif NLTK:
                    if (word in synonyms[_word]):
                        if (index and (words[(index - 1)].lower() == 'no')):
                            pass
                        else:
                            category = incident_type_represent(kw.incident_type_id)
                            break
            for loc in locs:
                name = loc.name.lower()
                if (word == name):
                    if (not loc_matches):
                        location_id = loc.id
                        priority += 1
                    loc_matches += 1
                elif ((index < max_index) and (('%s    %s' % (word, words[(index + 1)])) == name)):
                    if (not loc_matches):
                        location_id = loc.id
                        priority += 1
                    loc_matches += 1
        index += 1
    if ((not loc_matches) or (loc_matches > 1)):
        if (lat and lon):
            location_id = ltable.insert(lat=lat, lon=lon)
        elif (service == 'twitter'):
            pass
    return None
