Ejemplo n.º 1
0
def get_content(post):
    if hasattr(post, 'content'):
        content = post.content
    elif isinstance(post, dict) and 'content' in post:
        content = post['content']
    else:
        content = post
    return unwrap_hidden(content)
Ejemplo n.º 2
0
 def _search(self, filter_type, item):
     '''Over-ride default to use ES'''
     content = hasattr(item,
                       'content') and item.content or item.get('content')
     content = unwrap_hidden(content)
     return ChannelFilterItem.objects.search(channel=self.channel,
                                             filter_type=filter_type,
                                             item=dict(content=content))
Ejemplo n.º 3
0
    def make_post_vector(self, item):
        ''' Convert the post to a useful dictionary of data that will allow
            all sorts of features to be applied.
        '''
        post = item
        if isinstance(item, dict):
            content = post['content']
            speech_acts = post['speech_acts']
        else:
            content = post.content
            speech_acts = post.speech_acts

        content = unwrap_hidden(content)
        speech_acts = map(unwrap_hidden, speech_acts)

        post_vector = self.channel.make_post_vector(post)
        post_vector.update(dict(speech_acts=speech_acts, content=content))
        return post_vector
Ejemplo n.º 4
0
    def _compute_distance(self, filter_type, item):
        score = 0
        top_matches = self._search(filter_type, item)

        if isinstance(item, ChannelFilterItem):
            content = item.vector['content']
        else:
            content = item.content
        content = unwrap_hidden(content)

        def _normalize(m):
            return isinstance(m, dict) and m['content'] or m.vector['content']

        for match in [_normalize(m) for m in top_matches]:
            d = distances.calc_distance(content, match)
            score = max(d, score)

        return score
Ejemplo n.º 5
0
    def _create_db_post(self,
                        content=None,
                        channel=None,
                        channels=None,
                        demand_matchables=False, **kw):
        from ..db.post.utils import factory_by_user
        from solariat.utils.hidden_proxy import unwrap_hidden

        if channel is None:
            channel = self.channel
        if channels is None:
            channels = [channel]

        from solariat_bottle.db.channel.base import Channel
        if not isinstance(channel, Channel):
            channel = Channel.objects.get(channel)

        if 'lang' not in kw or not kw['lang']:
            # posts are english by default;
            # Note: set lang=auto for autodetect
            kw['lang'] = 'en'

        user = kw.pop('user', None) or self.user
        if not ('_created' in kw or
                ('twitter' in kw and 'created_at' in kw['twitter']) or
                ('facebook' in kw and 'created_at' in kw['facebook'])):
            _created = now()
            minimal_interval = timedelta(milliseconds=1)
            if _created - self._post_last_created < minimal_interval:
                _created = self._post_last_created = _created + minimal_interval
            kw['_created'] = _created
        if content:
            kw['content'] = unwrap_hidden(content)
        post = factory_by_user(user, channels=channels, **kw)

        return post
Ejemplo n.º 6
0
 def get_post_content(p):
     _get = lambda p, attr: \
         p.get(attr) if isinstance(p, dict) else getattr(p, attr)
     attr = 'url' if context.get('platform') == 'Twitter' else 'content'
     return unwrap_hidden(_get(p, attr))