generated from freelawproject/new-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtest_AnnotateTest.py
305 lines (293 loc) · 13.6 KB
/
test_AnnotateTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import re
from pathlib import Path
from unittest import TestCase
from eyecite import annotate_citations, clean_text, get_citations
from eyecite.models import Document
from eyecite.utils import maybe_balance_style_tags
class AnnotateTest(TestCase):
def test_annotate(self):
def straighten_quotes(text):
return text.replace("’", "'")
def lower_annotator(before, text, after):
return before + text.lower() + after
self.maxDiff = None
test_pairs = (
# single cite
("1 U.S. 1", "<0>1 U.S. 1</0>", []),
# cite with extra text
("foo 1 U.S. 1 bar", "foo <0>1 U.S. 1</0> bar", []),
# cite with punctuation
("foo '1 U.S. 1' bar", "foo '<0>1 U.S. 1</0>' bar", []),
# cite with missing page number (original underscores should be
# rendered in annotated text even though the missing page number
# has been normalized to None within the citation object)
("foo 1 U.S. ____ bar", "foo <0>1 U.S. ____</0> bar", []),
# law cite
(
"foo. Mass. Gen. Laws ch. 1, § 2. bar",
"foo. <0>Mass. Gen. Laws ch. 1, § 2</0>. bar",
[],
),
# journal cite
(
"foo. 1 Minn. L. Rev. 2. bar",
"foo. <0>1 Minn. L. Rev. 2</0>. bar",
[],
),
# Id. cite
(
"1 U.S. 1. Foo. Id. Bar. Id. at 2.",
"<0>1 U.S. 1</0>. Foo. <1>Id.</1> Bar. <2>Id. at 2</2>.",
[],
),
# Supra cite
(
"1 U.S. 1. Foo v. Bar, supra at 2.",
"<0>1 U.S. 1</0>. Foo v. Bar, <1>supra at 2</1>.",
[],
),
# Reference cite
(
"Foo v. Bar 1 U.S. 1. In Foo at 2.",
"Foo v. Bar <0>1 U.S. 1</0>. In <1>Foo at 2</1>.",
[],
),
# whitespace and html -- no unbalanced tag check
(
"<body>foo <i>1 <b>U.S.</b></i> 1 bar</body>",
"<body>foo <i><0>1 <b>U.S.</b></i> 1</0> bar</body>",
["html", "inline_whitespace"],
),
# whitespace and html -- unbalanced tags are repaired
(
"foo <i>1 U.S.</i> 1; 2 <i>U.S.</i> 2",
"foo <0><i>1 U.S.</i> 1</0>; <1>2 <i>U.S.</i> 2</1>",
["html", "inline_whitespace"],
{"unbalanced_tags": "skip"},
),
# whitespace and html -- wrap unbalanced tags
(
"<i>1 U.S.</i> 1; 2 <i>U.S.</i> 2",
"<i><0>1 U.S.</0></i><0> 1</0>; <1>2 <i>U.S.</i> 2</1>",
["html", "inline_whitespace"],
{"unbalanced_tags": "wrap"},
),
# tighly-wrapped html -- skip unbalanced tags (issue #54)
(
"foo <i>Ibid.</i> bar",
"foo <i><0>Ibid.</0></i> bar",
["html", "inline_whitespace"],
{"unbalanced_tags": "skip"},
),
# whitespace containing linebreaks
("1\nU.S. 1", "<0>1\nU.S. 1</0>", ["all_whitespace"]),
# multiple Id. tags
(
"1 U.S. 1. Id. 2 U.S. 2. Id.",
"<0>1 U.S. 1</0>. <1>Id.</1> <2>2 U.S. 2</2>. <3>Id.</3>",
[],
),
# replacement in cleaners
(
"1 Abbott’s Pr.Rep. 1",
"<0>1 Abbott’s Pr.Rep. 1</0>",
[straighten_quotes],
),
# custom annotator
(
"1 U.S. 1",
"<0>1 u.s. 1</0>",
[],
{"annotator": lower_annotator},
),
# solvable unbalanced <em> tag. Need the FullCaseCitation first
# so the ReferenceCitation can be found
# from https://www.courtlistener.com/api/rest/v4/opinions/8496639/
# source: Opinion.xml_harvard
(
" partially secured by a debtor’s principal residence was not "
"con-firmable. <em>Nobelman v. Am. Sav. Bank, </em>"
"508 U.S. 324, 113 S.Ct. 2106, 124 L.Ed.2d 228 (1993). That "
"plan proposed to bifurcate the claim and... pay the unsecured"
"... only by a lien on the debtor’s principal residence.” "
"<em>Nobelman </em>at 332, 113 S.Ct. 2106. Section 1123(b)(5) "
"codifies the <em>Nobelman </em>decision in individual debtor "
"chapter 11 cases.",
" partially secured by a debtor’s principal residence was not con-firmable. <em>Nobelman v. Am. Sav. Bank, </em><a href='something'>508 U.S. 324</a>, <a href='something'>113 S.Ct. 2106</a>, <a href='something'>124 L.Ed.2d 228</a> (1993). That plan proposed to bifurcate the claim and... pay the unsecured... only by a lien on the debtor’s principal residence.” <em>Nobelman </em>at 332, <a href='something'>113 S.Ct. 2106</a>. Section 1123(b)(5) codifies the <em><a href='something'>Nobelman</a> </em>decision in individual debtor chapter 11 cases.",
["html", "all_whitespace"],
{
"annotate_anchors": True,
"unbalanced_tags": "skip",
"use_markup": True,
},
),
# solvable unbalanced <i> tag
# from https://www.courtlistener.com/api/rest/v4/opinions/2841253/
# source: Opinion.html
(
"he has not agreed so to submit.’” <i>Howsam v. Dean"
" Witter Reynolds, Inc.</i>, 537 U.S. 79, 83, 123 S. Ct."
" 588, 591 (2002) (combined mandamus and"
" interlocutory appeal) (citing <i>Howsam</i> at 84, 123"
" S. Ct. at 592)",
"he has not agreed so to submit.’” <i>Howsam v. Dean"
" Witter Reynolds, Inc.</i>, <a href='something'>537 U.S."
" 79</a>, 83, <a href='something'>123 S. Ct. 588</a>, 591"
" (2002) (combined mandamus and interlocutory appeal)"
" (citing <a href='something'><i>Howsam</i> at 84</a>, <a"
" href='something'>123 S. Ct. at 592</a>)",
["html", "all_whitespace"],
{
"annotate_anchors": True,
"unbalanced_tags": "skip",
"use_markup": True,
},
),
# The next 2 examples could be resolved if we increased the
# character tolerance or admitted the full case name instead of
# just one of the parties
(
# https://www.courtlistener.com/api/rest/v4/opinions/1535649/
# source: xml_harvard
"See also Styler v. Tall Oaks, Inc. (In re Hatch),"
" 93 B.R. 263, 267 (Bankr.D. Utah 1988),"
" <em> rev'd </em> 114 B.R. 747 (D.Utah 1989)."
"</p>... The court makes no"
" determination as to whe Fifth Amendment to the"
" constitution of the United States.” <em> Styler v."
" Tall Oaks, Inc. (In re Hatch), </em> at 748."
"</p>",
"See also Styler v. Tall Oaks, Inc. (In re Hatch),"
" <a href='something'>93 B.R. 263</a>, 267"
" (Bankr.D. Utah 1988), <em> rev'd </em> <a"
" href='something'>114 B.R. 747</a> (D.Utah 1989)."
"</p>... The court makes no"
" determination as to whe Fifth Amendment to the"
" constitution of the United States.” <em> Styler v."
" Tall Oaks, Inc. (In re Hatch), </em> at 748."
"</p>",
["html", "all_whitespace"],
{
"annotate_anchors": True,
"unbalanced_tags": "skip",
"use_markup": True,
},
),
(
# https://www.courtlistener.com/api/rest/v4/opinions/1985850/
# source: html_lawbox
"to act rationally. <i>See, e.g., </i><i>State v."
" Wingler,</i> 25 <i>N.J.</i> 161, 175, 135 <i>A.</i>2d"
" 468 (1957); <i>citing, ... have been applied.'"
" [<i>State v. Wingler</i> at 175, 135 <i>A.</i>2d"
" 468, <i>citing, </i><i>Minnesota ex rel.</i>",
"to act rationally. <i>See, e.g., </i><i>State v."
" Wingler,</i> <a href='something'>25 <i>N.J.</i>"
" 161</a>, 175, <a href='something'>135 <i>A.</i>2d"
" 468</a> (1957); <i>citing, ... have been applied.'"
" [<i>State v. Wingler</i> at 175, <a"
" href='something'>135 <i>A.</i>2d 468</a>, <i>citing,"
" </i><i>Minnesota ex rel.</i>",
["html", "all_whitespace"],
{
"annotate_anchors": True,
"unbalanced_tags": "skip",
"use_markup": True,
},
),
)
for source_text, expected, clean_steps, *annotate_kwargs in test_pairs:
annotate_kwargs = annotate_kwargs[0] if annotate_kwargs else {}
with self.subTest(
source_text,
clean_steps=clean_steps,
annotate_args=annotate_kwargs,
):
if annotate_kwargs.pop("use_markup", False):
get_citations_args = {"markup_text": source_text}
else:
get_citations_args = {"plain_text": source_text}
document = Document(
**get_citations_args, clean_steps=clean_steps
)
cites = get_citations(
**get_citations_args, clean_steps=clean_steps
)
annotations = [
(c.span(), f"<{i}>", f"</{i}>")
for i, c in enumerate(cites)
]
if annotate_kwargs.pop("annotate_anchors", False):
annotations = [
(c.span(), "<a href='something'>", "</a>")
for c in cites
]
annotated = annotate_citations(
document.plain_text,
annotations,
source_text=source_text,
**annotate_kwargs,
)
self.assertEqual(annotated, expected)
def test_tag_balancing(self):
"""Test trickier tag balancing cases"""
string = "Something; In <em>Nobelman </em> at 332, 113 S.Ct. 2106 (2010); Something else"
span_text = "Nobelman </em> at 332, 113 S.Ct. 2106 (2010)"
start, end = re.search(re.escape(span_text), string).span()
_, _, balanced = maybe_balance_style_tags(start, end, string)
self.assertTrue(balanced.startswith("<em>"))
def test_long_diff(self):
"""Does diffing work across a long text with many changes?"""
opinion_text = (
Path(__file__).parent / "assets" / "opinion.txt"
).read_text()
cleaned_text = clean_text(opinion_text, ["all_whitespace"])
annotated_text = annotate_citations(
cleaned_text, [((902, 915), "~FOO~", "~BAR~")], opinion_text
)
self.assertIn("~FOO~539\n U. S. 306~BAR~", annotated_text)
def test_span_with_pincite(self):
test_pairs = [
# full case citaiton pin cite
(
"foo, Foo v. Bar, 550 U. S. 500, at 554-555, and so on ",
["550 U. S. 500, at 554-555"],
),
# short case citaiton pin cite
(
"foo, Twombly, 550 U. S., at 554-555. ",
["550 U. S., at 554-555"],
),
# id pin cite span
(
"Twombly, 550 U. S., at 554-555. ... etitive entry,’ ” id., at 551, beca",
["550 U. S., at 554-555", "id., at 551"],
),
# reference citation pin cite span
(
"compelling Amick v. Liberty Mut. Ins. Co., 455 A.2d 793 (R.I. 1983). In that case ... Stats, do. See Amick at 795",
["455 A.2d 793", "Amick at 795"],
),
# capture supra pin cite span in html
(
"Bell Atlantic Corp. </em>v. <em>Twombly, </em>550 U. S. 544 (2007), which discussed... apellate court’s core competency <em>Twombly, </em>550 U. S., at 557. Evaluating... In <em>Twombly</em>, supra, at 553-554, the ",
["550 U. S. 544", "550 U. S., at 557", "supra, at 553-554"],
),
# can we wrap the full pincite that appears before
# https://www.courtlistener.com/opinion/8524158/in-re-cahill/
(
"principal residence.” Nobelman at 332, 113 S.Ct. 2106",
["Nobelman at 332, 113 S.Ct. 2106"],
),
]
for source_text, expected in test_pairs:
plain_text = clean_text(source_text, ["all_whitespace", "html"])
citation = get_citations(plain_text)
for citation in citation:
start, end = citation.span_with_pincite()
pin_cite_span = plain_text[start:end]
self.assertEqual(
pin_cite_span,
expected.pop(0),
msg="Failed to extract pin cite span",
)