Skip to content

Commit faf63df

Browse files
committed
Merge remote-tracking branch 'origin/main'
# Conflicts: # src/lexmachina/_async/client.py # src/lexmachina/_sync/client.py
2 parents 6415dca + 9e67fa6 commit faf63df

File tree

11 files changed

+2908
-7
lines changed

11 files changed

+2908
-7
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Don't accidentally track config info
2+
config/config.ini
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This package provides a client to access the Lex Machina API for legal analytics
1010
3. Install package with `pip3 install lexmachina-client` .
1111
1. Create an app and get the client key and secret via the [directions here](https://developer.lexmachina.com/default/docs/generating_oauth_credentials).
1212

13-
1. In your project directory create a directory `config` and inside that create a file named config.ini . Populate using the below values and the key and secret from above. The below values for URLS should be used if you are using Lex Machina's production API. If you are testing this out with the [sandbox API](https://developer.lexmachina.com/default/documentation/lexmachina-sandbox) then you should use `base_url = https://api.sandbox.lexmachina.com`
13+
1. In your project directory create a directory `config` and inside that create a file named config.ini . Populate using the below values and the key and secret from above. The below values for URLS should be used if you are using Lex Machina's production API.
1414

1515
```
1616
[URLS]

examples/judges_for_party.ipynb

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "initial_id",
7+
"metadata": {
8+
"ExecuteTime": {
9+
"end_time": "2023-09-19T23:52:07.036312Z",
10+
"start_time": "2023-09-19T23:52:07.031644Z"
11+
}
12+
},
13+
"outputs": [],
14+
"source": [
15+
"from lexmachina import LexMachinaAsyncClient\n",
16+
"from lexmachina import StateCaseQueryRequest\n",
17+
"import asyncio\n",
18+
"from typing import Optional"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"id": "6b2892d11fa60d6b",
25+
"metadata": {
26+
"ExecuteTime": {
27+
"end_time": "2023-09-19T23:51:47.558425Z",
28+
"start_time": "2023-09-19T23:51:47.556581Z"
29+
}
30+
},
31+
"outputs": [],
32+
"source": [
33+
"client = LexMachinaAsyncClient(config_file_path='../config/config.ini')"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"id": "55dbdc2784b68c34",
39+
"metadata": {},
40+
"source": [
41+
"In order to get the judges for a specific party, here we first locate the party id.\n",
42+
"Here we are using a very simplistic method where we take the most frequently occurring party returned from party search to be the correct one. \n",
43+
"That can be revisited, or else you can look up the party in the LexMachina product ahead of time."
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 3,
49+
"id": "da21ff85b4cbe623",
50+
"metadata": {
51+
"ExecuteTime": {
52+
"end_time": "2023-09-20T00:04:26.323450Z",
53+
"start_time": "2023-09-20T00:04:26.316594Z"
54+
}
55+
},
56+
"outputs": [],
57+
"source": [
58+
"async def num_lex_cases_for_party(party_id: int): \n",
59+
" query = StateCaseQueryRequest()\n",
60+
" query.include_parties(party_id)\n",
61+
" query.include_state('CA')\n",
62+
" cases = await client.query_state_cases_case(query=query, options={'pageThrough': True}, page_size=100)\n",
63+
" return len(cases)\n",
64+
"\n",
65+
"async def get_party_id(name: str) -> int:\n",
66+
" candidates = await client.search_parties(name, page_size=10)\n",
67+
" candidates = candidates['parties']\n",
68+
" candidates = [party['partyId'] for party in candidates]\n",
69+
" num_cases = await asyncio.gather(*[num_lex_cases_for_party(cand) for cand in candidates])\n",
70+
" most_freq = sorted(zip(candidates, num_cases), key=lambda tup: tup[1], reverse=True)[0]\n",
71+
" return most_freq[0]"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 4,
77+
"id": "789d408e46cdaa43",
78+
"metadata": {
79+
"ExecuteTime": {
80+
"end_time": "2023-09-20T00:04:35.142423Z",
81+
"start_time": "2023-09-20T00:04:29.356413Z"
82+
}
83+
},
84+
"outputs": [
85+
{
86+
"name": "stdout",
87+
"output_type": "stream",
88+
"text": [
89+
"39480\n"
90+
]
91+
}
92+
],
93+
"source": [
94+
"party_id = await get_party_id('liberty mutual')\n",
95+
"print(party_id)"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"id": "9ba3bd35857c0cc",
101+
"metadata": {},
102+
"source": [
103+
"Next, we'll gather information about the cases this party was in, filtering by role if desired.\n",
104+
"From the cases, we'll aggregate the judges presiding over these cases, ultimately selecting those who did so the most often."
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": 5,
110+
"id": "667cb87c79770626",
111+
"metadata": {
112+
"ExecuteTime": {
113+
"end_time": "2023-09-20T03:36:25.287922Z",
114+
"start_time": "2023-09-20T03:36:25.281554Z"
115+
}
116+
},
117+
"outputs": [],
118+
"source": [
119+
"async def lex_judges_for_party(party_id: int, role: Optional[str] = None, state: str = 'CA'):\n",
120+
" case_infos = await lex_cases_for_party_role(party_id, role, state)\n",
121+
" case_ids = list(lex_case['stateCaseId'] for lex_case in case_infos)\n",
122+
" print(f'there are {len(case_ids)} cases in {state} where {party_id} has the role: {role}')\n",
123+
" cases = await asyncio.gather(*[client.get_state_cases(case) for case in case_ids])\n",
124+
" return [j for lex_case in cases for j in lex_case['judges']]\n",
125+
" \n",
126+
" \n",
127+
"async def lex_cases_for_party_role(party_id: int, role: Optional[str] = None, state: str = 'CA'):\n",
128+
" query = StateCaseQueryRequest()\n",
129+
" query.include_state(state)\n",
130+
" if not role:\n",
131+
" query.include_parties(party_id)\n",
132+
" elif role.lower() == 'defendant':\n",
133+
" query.parties_include_defendant(party_id)\n",
134+
" elif role.lower() == 'plaintiff':\n",
135+
" query.parties_include_plaintiff(party_id)\n",
136+
" return await client.query_state_cases_case(query=query, options={'pageThrough': True}, page_size=100)\n",
137+
"\n"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 6,
143+
"id": "8c381f1e9dc78f23",
144+
"metadata": {
145+
"ExecuteTime": {
146+
"end_time": "2023-09-20T03:36:47.840656Z",
147+
"start_time": "2023-09-20T03:36:27.444839Z"
148+
}
149+
},
150+
"outputs": [
151+
{
152+
"name": "stdout",
153+
"output_type": "stream",
154+
"text": [
155+
"there are 628 cases in CA where 39480 has the role: defendant\n"
156+
]
157+
}
158+
],
159+
"source": [
160+
"from collections import Counter\n",
161+
"\n",
162+
"judge_counts = Counter()\n",
163+
"judge_id_to_name = dict()\n",
164+
"all_judges = await lex_judges_for_party(party_id=party_id, role='defendant')\n",
165+
"for i, judge_info in enumerate(all_judges):\n",
166+
" judge_name = judge_info['name']\n",
167+
" judge_id = judge_info['stateJudgeId']\n",
168+
" judge_id_to_name[judge_id] = judge_name\n",
169+
" judge_counts[judge_id] += 1 "
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": 7,
175+
"id": "3f940548b7fe8c69",
176+
"metadata": {
177+
"ExecuteTime": {
178+
"end_time": "2023-09-20T16:10:43.299350Z",
179+
"start_time": "2023-09-20T16:10:43.278083Z"
180+
}
181+
},
182+
"outputs": [
183+
{
184+
"name": "stdout",
185+
"output_type": "stream",
186+
"text": [
187+
"Judge Amy Nicole Carter: 17 cases\n",
188+
"Judge Katherine A Bacal: 15 cases\n",
189+
"Judge Gregory W Pollack: 13 cases\n",
190+
"Judge Timothy B Taylor: 12 cases\n",
191+
"Judge Richard E L Strauss: 10 cases\n",
192+
"Judge Joel R Wohlfeil: 10 cases\n",
193+
"Judge David Justin Cowan: 10 cases\n",
194+
"Judge Nathan R Scott: 10 cases\n",
195+
"Judge Mark Chong Kim: 10 cases\n",
196+
"Judge Linda S Marks: 10 cases\n",
197+
"Judge Michelle Williams Court: 9 cases\n",
198+
"Judge Robert J Moss: 9 cases\n",
199+
"Judge Richard S Whitney: 9 cases\n",
200+
"Judge Peter A Hernandez: 9 cases\n",
201+
"Judge Deirdre Hughes Hill: 9 cases\n",
202+
"Judge Barbara Ann Meiers: 9 cases\n",
203+
"Judge Stephen Ira Goorvitch: 8 cases\n",
204+
"Judge John S Meyer: 8 cases\n",
205+
"Judge Barbara Marie Scheper: 8 cases\n",
206+
"Judge Gerrit Wardell Wood: 8 cases\n"
207+
]
208+
}
209+
],
210+
"source": [
211+
"for judge_id, ct in judge_counts.most_common(20):\n",
212+
" print(f'Judge {judge_id_to_name[judge_id]}: {ct} cases')"
213+
]
214+
},
215+
{
216+
"cell_type": "code",
217+
"execution_count": null,
218+
"id": "9894bc396e6c6ec8",
219+
"metadata": {},
220+
"outputs": [],
221+
"source": []
222+
}
223+
],
224+
"metadata": {
225+
"kernelspec": {
226+
"display_name": "Python 3 (ipykernel)",
227+
"language": "python",
228+
"name": "python3"
229+
},
230+
"language_info": {
231+
"codemirror_mode": {
232+
"name": "ipython",
233+
"version": 3
234+
},
235+
"file_extension": ".py",
236+
"mimetype": "text/x-python",
237+
"name": "python",
238+
"nbconvert_exporter": "python",
239+
"pygments_lexer": "ipython3",
240+
"version": "3.9.11"
241+
}
242+
},
243+
"nbformat": 4,
244+
"nbformat_minor": 5
245+
}

0 commit comments

Comments
 (0)