Example #1
0
 def test_citation_matching_issue621(self):
     """Make sure that a citation like 1 Wheat 9 doesn't match 9 Wheat 1"""
     # The fixture contains a reference to 9 F. 1, so we expect no results.
     citation_str = '1 F. 9 (1795)'
     citation = get_citations(citation_str)[0]
     results = match_citation(citation)
     self.assertEqual([], results)
Example #2
0
 def test_citation_matching_issue621(self):
     """Make sure that a citation like 1 Wheat 9 doesn't match 9 Wheat 1"""
     # The fixture contains a reference to 9 F. 1, so we expect no results.
     citation_str = '1 F. 9 (1795)'
     citation = get_citations(citation_str)[0]
     results = match_citation(citation)
     self.assertEqual([], results)
Example #3
0
def update_document(opinion, index=True):
    """Get the citations for an item and save it and add it to the index if
    requested."""
    citations = get_document_citations(opinion)

    # List used so we can do one simple update to the citing opinion.
    opinions_cited = set()
    for citation in citations:
        matches = match_citations.match_citation(citation, citing_doc=opinion)

        # TODO: Figure out what to do if there's more than one
        if len(matches) == 1:
            match_id = matches[0]['id']
            try:
                matched_opinion = Opinion.objects.get(pk=match_id)

                # Increase citation count for matched cluster if it hasn't
                # already been cited by this opinion.
                if matched_opinion not in opinion.opinions_cited.all():
                    matched_opinion.cluster.citation_count += 1
                    matched_opinion.cluster.save(index=index)

                # Add citation match to the citing opinion's list of cases it
                # cites. opinions_cited is a set so duplicates aren't an issue
                opinions_cited.add(matched_opinion.pk)

                # URL field will be used for generating inline citation html
                citation.match_url = matched_opinion.cluster.get_absolute_url()
                citation.match_id = matched_opinion.pk
            except Opinion.DoesNotExist:
                # No Opinions returned. Press on.
                continue
            except Opinion.MultipleObjectsReturned:
                # Multiple Opinions returned. Press on.
                continue
        else:
            # No match found for citation
            #create_stub([citation])
            pass

    # Only update things if we found citations
    if citations:
        opinion.html_with_citations = create_cited_html(opinion, citations)

        # Nuke existing citations
        OpinionsCited.objects.filter(citing_opinion_id=opinion.pk).delete()

        # Create the new ones.
        OpinionsCited.objects.bulk_create([
            OpinionsCited(citing_opinion_id=opinion.pk, cited_opinion_id=pk)
            for pk in opinions_cited
        ])

    # Update Solr if requested. In some cases we do it at the end for
    # performance reasons.
    opinion.save(index=index)
Example #4
0
def update_document(opinion, index=True):
    """Get the citations for an item and save it and add it to the index if
    requested."""
    citations = get_document_citations(opinion)

    # List used so we can do one simple update to the citing opinion.
    opinions_cited = set()
    for citation in citations:
        matches = match_citations.match_citation(citation, citing_doc=opinion)

        # TODO: Figure out what to do if there's more than one
        if len(matches) == 1:
            match_id = matches[0]["id"]
            try:
                matched_opinion = Opinion.objects.get(pk=match_id)

                # Increase citation count for matched cluster if it hasn't
                # already been cited by this opinion.
                if matched_opinion not in opinion.opinions_cited.all():
                    matched_opinion.cluster.citation_count += 1
                    matched_opinion.cluster.save(index=index)

                # Add citation match to the citing opinion's list of cases it
                # cites. opinions_cited is a set so duplicates aren't an issue
                opinions_cited.add(matched_opinion.pk)

                # URL field will be used for generating inline citation html
                citation.match_url = matched_opinion.cluster.get_absolute_url()
                citation.match_id = matched_opinion.pk
            except Opinion.DoesNotExist:
                # No Opinions returned. Press on.
                continue
            except Opinion.MultipleObjectsReturned:
                # Multiple Opinions returned. Press on.
                continue
        else:
            # No match found for citation
            # create_stub([citation])
            pass

    # Only update things if we found citations
    if citations:
        opinion.html_with_citations = create_cited_html(opinion, citations)

        # Nuke existing citations
        OpinionsCited.objects.filter(citing_opinion_id=opinion.pk).delete()

        # Create the new ones.
        OpinionsCited.objects.bulk_create(
            [OpinionsCited(citing_opinion_id=opinion.pk, cited_opinion_id=pk) for pk in opinions_cited]
        )

    # Update Solr if requested. In some cases we do it at the end for
    # performance reasons.
    opinion.save(index=index)
Example #5
0
def get_query_citation(cd):
    """Extract citations from the query string and return them, or return
    None
    """
    if not cd.get('q'):
        return None
    citations = get_citations(cd['q'], html=False)

    matches = None
    if len(citations) == 1:
        # If it's not exactly one match, user doesn't get special help.
        matches = match_citation(citations[0])
        if len(matches) >= 1:
            # Just return the first result if there is more than one. This
            # should be rare, and they're ordered by relevance.
            return matches.result.docs[0]

    return matches
Example #6
0
def get_query_citation(cd):
    """Extract citations from the query string and return them, or return
    None
    """
    if not cd.get('q'):
        return None
    citations = get_citations(cd['q'], html=False)

    matches = None
    if len(citations) == 1:
        # If it's not exactly one match, user doesn't get special help.
        matches = match_citation(citations[0])
        if len(matches) >= 1:
            # Just return the first result if there is more than one. This
            # should be rare, and they're ordered by relevance.
            return matches.result.docs[0]

    return matches
Example #7
0
def get_query_citation(cd):
    """Extract citations from the query string and return them, or return
    None
    """
    if not cd.get("q"):
        return None
    citations = get_citations(
        cd["q"], html=False, do_post_citation=False, do_defendant=False
    )

    matches = None
    if len(citations) == 1:
        # If it's not exactly one match, user doesn't get special help.
        matches = match_citation(citations[0])
        if len(matches) == 1:
            # If more than one match, don't show the tip
            return matches.result.docs[0]

    return matches
Example #8
0
def get_query_citation(cd: Dict[str, Any]) -> Optional[List[Citation]]:
    """Extract citations from the query string and return them, or return
    None
    """
    if not cd.get("q"):
        return None
    citations = get_citations(cd["q"],
                              do_post_citation=False,
                              do_defendant=False)

    citations = [c for c in citations if isinstance(c, Citation)]

    matches = None
    if len(citations) == 1:
        # If it's not exactly one match, user doesn't get special help.
        matches = match_citation(citations[0])
        if len(matches) == 1:
            # If more than one match, don't show the tip
            return matches.result.docs[0]

    return matches
Example #9
0
def find_citations_for_opinion_by_pks(self, opinion_pks, index=True):
    """Find citations for search.Opinion objects.

    :param opinion_pks: An iterable of search.Opinion PKs
    :param index: Whether to add the item to Solr
    :return: None
    """
    opinions = Opinion.objects.filter(pk__in=opinion_pks)
    for opinion in opinions:
        citations = get_document_citations(opinion)

        # List used so we can do one simple update to the citing opinion.
        opinions_cited = set()
        for citation in citations:
            try:
                matches = match_citations.match_citation(
                    citation, citing_doc=opinion)
            except ResponseNotReady as e:
                # Threading problem in httplib, which is used in the Solr query.
                raise self.retry(exc=e, countdown=2)

            # TODO: Figure out what to do if there's more than one
            if len(matches) == 1:
                match_id = matches[0]['id']
                try:
                    matched_opinion = Opinion.objects.get(pk=match_id)

                    # Increase citation count for matched cluster if it hasn't
                    # already been cited by this opinion.
                    if matched_opinion not in opinion.opinions_cited.all():
                        matched_opinion.cluster.citation_count += 1
                        matched_opinion.cluster.save(index=index)

                    # Add citation match to the citing opinion's list of cases
                    # it cites. opinions_cited is a set so duplicates aren't an
                    # issue
                    opinions_cited.add(matched_opinion.pk)

                    # URL field will be used for generating inline citation
                    # html
                    citation.match_url = matched_opinion.cluster.get_absolute_url()
                    citation.match_id = matched_opinion.pk
                except Opinion.DoesNotExist:
                    # No Opinions returned. Press on.
                    continue
                except Opinion.MultipleObjectsReturned:
                    # Multiple Opinions returned. Press on.
                    continue
            else:
                # No match found for citation
                # create_stub([citation])
                pass

        # Only update things if we found citations
        if citations:
            opinion.html_with_citations = create_cited_html(opinion, citations)

            # Nuke existing citations
            OpinionsCited.objects.filter(citing_opinion_id=opinion.pk).delete()

            # Create the new ones.
            OpinionsCited.objects.bulk_create([
                OpinionsCited(citing_opinion_id=opinion.pk,
                              cited_opinion_id=pk) for
                pk in opinions_cited
            ])

        # Update Solr if requested. In some cases we do it at the end for
        # performance reasons.
        opinion.save(index=index)