def get(self, path, head=False): cache.create_prc() expression = WikiPage.path_to_title(path) parsed_expression = search.parse_expression(expression) scoretable = WikiPage.search(expression) positives = dict([(k, v) for k, v in scoretable.items() if v >= 0.0]) positives = OrderedDict( sorted(positives.iteritems(), key=operator.itemgetter(1), reverse=True)[:20]) negatives = dict([(k, abs(v)) for k, v in scoretable.items() if v < 0.0]) negatives = OrderedDict( sorted(negatives.iteritems(), key=operator.itemgetter(1), reverse=True)[:20]) self.response.headers['Content-Type'] = 'text/html; charset=utf-8' html = template( self.request, 'search.html', { 'expression': expression, 'parsed_expression': parsed_expression, 'positives': positives, 'negatives': negatives }) set_response_body(self.response, html, head)
def test_single_title(self): expected = { 'pos': [u'Hello'], 'neg': [], } actual = search.parse_expression(u'+Hello') self.assertEqual(expected, actual)
def test_multiple_titles(self): expected = { 'pos': [u'What the', u'Is it'], 'neg': [u'Fun'], } actual = search.parse_expression(u'+What the -Fun +Is it') self.assertEqual(expected, actual)
def test_no_space(self): expected = { 'pos': [u'User-centered design'], 'neg': [], } actual = search.parse_expression(u'+User-centered design') self.assertEqual(expected, actual)
def search(cls, expression): # parse parsed = search.parse_expression(expression) # evaluate pos, neg = parsed['pos'], parsed['neg'] pos_pages = [cls.get_by_title(t, True) for t in pos] neg_pages = [cls.get_by_title(t, True) for t in neg] scoretable = search.evaluate( dict((page.title, page.link_scoretable) for page in pos_pages), dict((page.title, page.link_scoretable) for page in neg_pages) ) return scoretable
def search(cls, expression): # parse parsed = search.parse_expression(expression) # evaluate pos, neg = parsed['pos'], parsed['neg'] pos_pages = [cls.get_by_title(t, True) for t in pos] neg_pages = [cls.get_by_title(t, True) for t in neg] scoretable = search.evaluate( dict((page.title, page.link_scoretable) for page in pos_pages), dict((page.title, page.link_scoretable) for page in neg_pages)) return scoretable
def load(self): expression = WikiPage.path_to_title(self.path) scoretable = WikiPage.search(expression) parsed_expression = search.parse_expression(expression) positives = dict([(k, v) for k, v in scoretable.items() if v >= 0.0]) positives = OrderedDict(sorted(positives.iteritems(), key=operator.itemgetter(1), reverse=True)[:20]) negatives = dict([(k, abs(v)) for k, v in scoretable.items() if v < 0.0]) negatives = OrderedDict(sorted(negatives.iteritems(), key=operator.itemgetter(1), reverse=True)[:20]) return { "expression": expression, "parsed_expression": parsed_expression, "positives": positives, "negatives": negatives, }
def get_search_result(self, path, head): expression = WikiPage.path_to_title(path) parsed_expression = search.parse_expression(expression) scoretable = WikiPage.search(expression) positives = dict([(k, v) for k, v in scoretable.items() if v >= 0.0]) positives = OrderedDict(sorted(positives.iteritems(), key=operator.itemgetter(1), reverse=True)[:20]) negatives = dict([(k, abs(v)) for k, v in scoretable.items() if v < 0.0]) negatives = OrderedDict(sorted(negatives.iteritems(), key=operator.itemgetter(1), reverse=True)[:20]) self.response.headers['Content-Type'] = 'text/html; charset=utf-8' html = self._template('search.html', {'expression': expression, 'parsed_expression': parsed_expression, 'positives': positives, 'negatives': negatives}) self._set_response_body(html, head)
def load(self): expression = WikiPage.path_to_title(self.path) scoretable = WikiPage.search(expression) parsed_expression = search.parse_expression(expression) positives = dict([(k, v) for k, v in scoretable.items() if v >= 0.0]) positives = OrderedDict( sorted(positives.iteritems(), key=operator.itemgetter(1), reverse=True)[:20]) negatives = dict([(k, abs(v)) for k, v in scoretable.items() if v < 0.0]) negatives = OrderedDict( sorted(negatives.iteritems(), key=operator.itemgetter(1), reverse=True)[:20]) return { 'expression': expression, 'parsed_expression': parsed_expression, 'positives': positives, 'negatives': negatives, }
def load(self): expression = WikiPage.path_to_title(self.path) scoretable = WikiPage.search(expression) parsed_expression = search.parse_expression(expression) positives = dict([(k, v) for k, v in scoretable.items() if v >= 0.0]) positives = OrderedDict(sorted(positives.iteritems(), key=operator.itemgetter(1), reverse=True)[:20]) negatives = dict([(k, abs(v)) for k, v in scoretable.items() if v < 0.0]) negatives = OrderedDict(sorted(negatives.iteritems(), key=operator.itemgetter(1), reverse=True)[:20]) context = { 'expression': expression, 'parsed_expression': parsed_expression, 'positives': positives, 'negatives': negatives, } if positives: context['positive_items'] = positives.items() if negatives: context['negative_items'] = negatives.items() return context
def test_single_title(self): expected = {"pos": [u"Hello"], "neg": []} actual = search.parse_expression(u"+Hello") self.assertEqual(expected, actual)
def test_no_space(self): expected = {"pos": [u"User-centered design"], "neg": []} actual = search.parse_expression(u"+User-centered design") self.assertEqual(expected, actual)
def test_multiple_titles(self): expected = {"pos": [u"What the", u"Is it"], "neg": [u"Fun"]} actual = search.parse_expression(u"+What the -Fun +Is it") self.assertEqual(expected, actual)