コード例 #1
0
def text_resolve(reference, returned_format):
    """

    :param reference:
    :param returned_format:
    :return:
    """
    try:
        resolved = cache_resolved_get(reference)
        if resolved:
            return format_resolved_reference(returned_format,
                                             resolved=resolved,
                                             reference=reference)

        if bool(RE_NUMERIC_VALUE.search(reference)):
            parsed_ref = text_parser(reference)
            if parsed_ref:
                return format_resolved_reference(
                    returned_format,
                    resolved=str(solve_reference(Hypotheses(parsed_ref))),
                    reference=reference)
            raise NoSolution("NotParsed")
        else:
            raise ValueError(
                'Reference with no year and volume cannot be resolved.')
    except (NoSolution, Incomplete, ValueError) as e:
        current_app.logger.error('Exception: {error}'.format(error=str(e)))
        return format_resolved_reference(returned_format,
                                         resolved='0.0 %s' % (19 * '.'),
                                         reference=reference)
    except Exception as e:
        current_app.logger.error('Exception: {error}'.format(error=str(e)))
        raise
コード例 #2
0
ファイル: solve.py プロジェクト: adsabs/reference_service
def solve_reference(ref):
    """
    returns a solution for what record is presumably meant by ref.

    ref is an instance of Reference (or rather, its subclasses).
    If no matching record is found, NoSolution is raised.
    :param ref:
    :return:
    """
    if not enough_to_proceed(ref):
        current_app.logger.error(
            "Not enough information to resolve the record")
        raise Incomplete("Not enough information to resolve the record.",
                         str(ref))

    possible_solutions = []
    for hypothesis in Hypotheses.iter_hypotheses(ref):
        try:
            return solve_for_fields(hypothesis)
        except Undecidable as ex:
            possible_solutions.extend(ex.considered_solutions)
        except (NoSolution, OverflowOrNone) as ex:
            current_app.logger.debug("(%s)" % ex.__class__.__name__)
        except (Solr, KeyboardInterrupt):
            raise
        except Exception as ex:
            current_app.logger.error(
                "Unhandled exception of type {0} occurred with arguments:{1!r}, thus killing a single hypothesis."
                .format(type(ex).__name__, ex.args))
            current_app.logger.error(traceback.format_exc())

    # if we have collected possible solutions for which we didn't want
    # to decide the first time around, now see if any one is better than
    # all others and accept that
    if possible_solutions:
        current_app.logger.debug("Considering stashed ties: %s" %
                                 (possible_solutions))

        cands = {}
        for score, sol in possible_solutions:
            cands.setdefault(sol, []).append((score, sol))
        for bibcode in cands:
            cands[bibcode] = max(cands[bibcode])
        scored = sorted(zip(cands.values(), cands.keys()))

        if len(scored) == 1:
            return Solution(scored[0][1], scored[0][0],
                            "only remaining of tied solutions")
        elif scored[-1][0] > scored[-2][0]:
            return Solution(scored[0][1], scored[0][0], "best tied solution")
        else:
            current_app.logger.debug("Remaining ties, giving up")
    raise NoSolution("Hypotheses exhausted", str(ref))
コード例 #3
0
ファイル: views.py プロジェクト: adsabs/reference_service
def text_resolve(reference, returned_format):
    """

    :param reference:
    :param returned_format:
    :return:
    """
    not_resolved = '0.0 %s' % (19 * '.')
    try:
        resolved = cache_resolved_get(reference)
        if resolved:
            return format_resolved_reference(returned_format,
                                             resolved=resolved,
                                             reference=reference)

        if bool(RE_NUMERIC_VALUE.search(reference)):
            parsed_ref = text_parser(reference)
            if parsed_ref:
                return format_resolved_reference(returned_format,
                                                 resolved=str(solve_reference(Hypotheses(parsed_ref))),
                                                 reference=reference)
            error_comment = 'NoSolution: unable to parse'
            current_app.logger.error('Exception: {error}'.format(error=error_comment))
            return format_resolved_reference(returned_format,
                                             resolved=not_resolved,
                                             reference=reference,
                                             comment=error_comment)
        else:
            error_comment = 'ValueError: reference with no year and volume cannot be resolved.'
            current_app.logger.error('Exception: {error}'.format(error=error_comment))
            return format_resolved_reference(returned_format,
                                             resolved=not_resolved,
                                             reference=reference,
                                             comment=error_comment)
    except (NoSolution, Incomplete, ValueError) as e:
        error_comment = 'Exception: {error}'.format(error=str(e))
        current_app.logger.error(error_comment)
        return format_resolved_reference(returned_format,
                                         resolved=not_resolved,
                                         reference=reference,
                                         comment=error_comment)
    except Exception as e:
        error_comment = 'Exception: {error}'.format(error=str(e))
        current_app.logger.error(error_comment)
        return format_resolved_reference(returned_format,
                                         resolved=not_resolved,
                                         reference=reference,
                                         comment=error_comment)
コード例 #4
0
def solve_reference(ref):
    """
    returns a solution for what record is presumably meant by ref.

    ref is an instance of Reference (or rather, its subclasses).
    If no matching record is found, NoSolution is raised.
    :param ref:
    :return:
    """
    possible_solutions = []
    for hypothesis in Hypotheses.iter_hypotheses(ref):
        try:
            return solve_for_fields(hypothesis)
        except Undecidable, ex:
            possible_solutions.extend(ex.considered_solutions)
        except (NoSolution, Overflow), ex:
            current_app.logger.debug("(%s)" % ex.__class__.__name__)
コード例 #5
0
ファイル: views.py プロジェクト: romanchyla/reference_service
def text_resolve(reference, returned_format):
    """

    :param reference:
    :param returned_format:
    :return:
    """
    try:
        if bool(RE_NUMERIC_VALUE.search(reference)):
            parsed_ref = text_parser(reference)
            result = format_resolved_reference(
                returned_format,
                resolved=str(solve_reference(Hypotheses(parsed_ref))),
                reference=reference)
        else:
            raise ValueError(
                'Reference with no year and volume cannot be resolved.')
    except Exception as e:
        current_app.logger.error('Exception: %s', str(e))
        result = format_resolved_reference(returned_format,
                                           resolved='0.0 %s' % (19 * '.'),
                                           reference=reference)

    return result
コード例 #6
0
def xml_post():
    """

    :return:
    """
    try:
        payload = request.get_json(force=True)  # post data in json
    except:
        payload = dict(request.form)  # post data in form encoding

    if not payload:
        return {'error': 'no information received'}, 400
    if 'parsed_reference' not in payload:
        return {
            'error':
            'no reference found in payload (parameter name is `reference`)'
        }, 400

    parsed_references = payload['parsed_reference']

    current_app.logger.debug(
        'received POST request with {count} references to resolve in xml mode.'
        .format(count=len(parsed_references)))

    returned_format = request.headers.get('Accept', 'text/plain')

    results = []
    for parsed_reference in parsed_references:
        try:
            resolved = str(solve_reference(Hypotheses(parsed_reference)))
            if resolved.startswith('0.0'):
                raise "Not Resolved"
            result = format_resolved_reference(
                returned_format,
                resolved=resolved,
                reference=parsed_reference['refstr'])
        except Exception as e:
            current_app.logger.error('Exception: {error}'.format(error=str(e)))
            # lets attempt to resolve using the text model
            if 'refplaintext' in parsed_reference:
                reference = urllib.parse.unquote(
                    parsed_reference['refplaintext'])
                if bool(RE_NUMERIC_VALUE.search(reference)):
                    current_app.logger.info(
                        'attempting to resolve the reference=`{reference}` in text mode now'
                        .format(reference=reference))
                    try:
                        parsed_ref = text_parser(reference)
                        if parsed_ref:
                            result = format_resolved_reference(
                                returned_format,
                                resolved=str(
                                    solve_reference(Hypotheses(parsed_ref))),
                                reference=reference,
                                cache=True)
                    except Exception as e:
                        current_app.logger.error(
                            'Exception: {error}'.format(error=str(e)))
                        result = format_resolved_reference(returned_format,
                                                           resolved='0.0 %s' %
                                                           (19 * '.'),
                                                           reference=reference)
                        continue
                else:
                    result = format_resolved_reference(
                        returned_format,
                        resolved='0.0 %s' % (19 * '.'),
                        reference=parsed_reference['refstr'])
                    continue
            else:
                result = format_resolved_reference(
                    returned_format,
                    resolved='0.0 %s' % (19 * '.'),
                    reference=parsed_reference['refstr'])
                continue
        finally:
            results.append(result)

    if len(results) == len(parsed_reference):
        if returned_format == 'application/json':
            return return_response({'resolved': results}, 200,
                                   'application/json; charset=UTF8')
        return return_response({'resolved': '\n'.join(results)}, 200,
                               'application/json; charset=UTF8')
    return return_response({'error': 'unable to resolve any references'}, 400,
                           'text/plain; charset=UTF8')