def querier_enclosure(i, q):
    """
    Wrapper for the query procedure in order to be used in a Worker
    """
    while not QUITTHREAD:
        print('Worker {}: Looking for the next query'.format(i))
        args = q.get()
        query = inquirer.AcademicQuerier(args['query_type'], args['payload'])
        if query is not None:
            results = query.post()
            if results:
                if args['query_type'] == inquirer.AcademicQueryType.INTERPRET:
                    expr = 'OR({})'.format(','.join([
                        interpretation['rules'][0]['value']
                        for interpretation in results
                    ]))
                    THE_QUEUE.put({
                        'query_type': inquirer.AcademicQueryType.EVALUATE,
                        'payload': {
                            'expr': expr,
                            'attributes': '*'
                        },
                        'parent': None
                    })
                elif args['query_type'] == inquirer.AcademicQueryType.EVALUATE:
                    parent = args.get('parent', None)
                    branch = ROOT['articles'] if parent is None else (
                        find_article(parent))['cites']
                    for result in results:
                        article = find_article(result['id'], parent)
                        if article is None:
                            branch.append(result)
                            if parent is None:
                                expr = 'RId={}'.format(result['id'])
                                THE_QUEUE.put({
                                    'query_type':
                                    inquirer.AcademicQueryType.EVALUATE,
                                    'payload': {
                                        'expr': expr,
                                        'attributes': '*'
                                    },
                                    'parent':
                                    result['id']
                                })
                    total = len(branch)
                    if total % 50 == 0:
                        new_payload = args['payload'].copy()
                        new_payload['offset'] = total
                        THE_QUEUE.put({
                            'query_type': args['query_type'],
                            'payload': new_payload,
                            'parent': args['parent']
                        })
        q.task_done()
        sleep(DELAY)
Пример #2
0
def main():
    """
    The method called when running this script
    """
    usage = """calc_histogram.py --expr "expresion"
A command-line tool to test similarity to Microsoft's Academic Knowledge."""

    fmt = IndentedHelpFormatter(max_help_position=50, width=100)
    parser = OptionParser(usage=usage, formatter=fmt)
    group = OptionGroup(
        parser, 'Query arguments',
        'These options define search query arguments and parameters.')
    group.add_option('-e',
                     '--expresion',
                     metavar='EXPR',
                     default=None,
                     help='Expression')
    group.add_option('-a',
                     '--attributes',
                     metavar='ATTR',
                     default='Id',
                     help='Expression')
    parser.add_option_group(group)
    options, _ = parser.parse_args()

    # Show help if we have not an expression
    if len(sys.argv) == 1:
        parser.print_help()
        return 1
    if options.expresion is None:
        print('Expression is mandatory!')
        return 1

    query = inquirer.AcademicQuerier(inquirer.AcademicQueryType.HISTOGRAM, {
        'expr': options.expresion,
        'attributes': options.attributes
    })
    if query is not None:
        histograms = query.post()
        for histogram in histograms:
            data = histogram['data']
            rng = range(1, len(data) + 1)
            labels = [val['value'] for val in data]
            plt.bar(rng, [val['count'] for val in data])
            plt.xticks(rng, labels, rotation='vertical')
            plt.margins(0.2)
            plt.subplots_adjust(bottom=0.15)
            plt.legend()
            plt.xlabel(histogram['attribute'])
            plt.ylabel('count')
            plt.title('Histogram for {}'.format(histogram['attribute']))
            plt.show()
def main():
    """
    The method called when running this script
    """
    usage = """similarity.py --s1 "this is a test" --s2 "that was a test"
A command-line tool to test similarity to Microsoft's Academic Knowledge."""

    fmt = IndentedHelpFormatter(max_help_position=50, width=100)
    parser = OptionParser(usage=usage, formatter=fmt)
    group = OptionGroup(
        parser, 'Query arguments',
        'These options define search query arguments and parameters.')
    group.add_option('--s1',
                     metavar='STRING1',
                     default=None,
                     help='First string')
    group.add_option('--s2',
                     metavar='STRING2',
                     default=None,
                     help='Second string')
    parser.add_option_group(group)
    options, _ = parser.parse_args()

    # Show help if we have neither keyword search nor author name
    if len(sys.argv) == 1:
        parser.print_help()
        return 1
    if options.s1 is None or options.s2 is None:
        print('Both strings are mandatory!')
        return 1

    query = inquirer.AcademicQuerier(inquirer.AcademicQueryType.SIMILARITY, {
        's1': options.s1,
        's2': options.s2
    })
    if query is not None:
        similarity = query.post()
        print('Similarity between "{}" and "{}" is {}'.format(
            options.s1, options.s2, similarity))