Crush 1L Year With AI-Powered Tools: Lesson 6.

legaltextai
4 min readAug 21, 2023

--

The Power of Automated Case Search and Summarization.

In the grand halls of legal academia, especially your first year in law school, you will be tasked with dissecting countless legal cases, each filled with intricate details and nuanced arguments. Your professors will expect you to master these cases, and it may seem an insurmountable task.

But fret not! For I shall guide you in creating a digital assistant that will help you search and summarize legal cases with ease. This assistant will not replace your intellect but rather enhance it, allowing you to delve deeper into the legal world.

Step 1: Setting Up Your Workspace

First, you must prepare your digital workspace. If you haven’t already for my previous lessons, install Anaconda and open Jupyter notebook, a platform where you can write and run Python code.

# Let's install and import the necessary tools
!pip install bs4 requests anthropic
import os
from bs4 import BeautifulSoup
import requests
import anthropic

Step 2: Acquiring the Keys to Knowledge

You will need an API key from Anthropic.

I highly recommend Anthropic, based on my personal experience using it to analyze legal data. If you haven’t received its API key yet, feel free to use OpenAI, applying the same logic.

anthropic = Anthropic(
# defaults to os.environ.get("ANTHROPIC_API_KEY")
api_key="your_key_here",
)

Step 3: Searching for a Legal Case

Now, we craft a function to search for legal cases on Google Scholar. This function will accept your query and return the title, link, and citation of the top search result.

Make sure your use of Google Scholar is for educational purposes only

def search_legal_cases(query, num_results=10):
url = "https://scholar.google.com/scholar"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.3"
}

params = {
"q": query,
"hl": "en",
"num": num_results,
"as_sdt": "4", # This parameter filters the search results to legal cases
}

response = requests.get(url, proxies=proxies, headers=headers, params=params)
soup = BeautifulSoup(response.text, "html.parser")

results = []
for result in soup.find_all("div", class_="gs_ri"):
title = result.find("h3", class_="gs_rt").text
base_url = "https://scholar.google.com"
link = base_url + result.find("a")["href"]
citation = result.find("div", class_="gs_a").text.replace(" - Google Scholar", "")
results.append((title, link, citation))

return results

Step 4. Extracting the Text of the Decision

Next, we write a function to extract the text of the decision from the web link of the top search result mentioned above.

def extract_text_from_link(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.3"
}

response = requests.get(url, headers=headers, proxies=proxies)
soup = BeautifulSoup(response.content, "html.parser")

text = soup.get_text(separator="\n")
return text

Step 5. Summarizing the Text of the Decision Using IRAC Framework.

Finally, we create a function that takes the full text of the case and generates its summary using the IRAC framework.

def get_summary(text):
prompt = f'''{HUMAN_PROMPT}
You are a law professor specialized in legal writing and legal research.
Summarize the case in {text} according to the following requirements:
Facts (name of the case and its parties, what happened factually).
Procedural history (what happened in the past procedurally, what were prior judgements).
Issues (what is in dispute).
Holding (the applied rule of law).
Rationale (reasons for the holding).
Decision (what did the court decide, e.g. affirmed, overruled).
Other opinions (if there are any dissenting or concurring opinions, summarize majority opinion, dissenting opinion and concurring opinion).
{AI_PROMPT}'''
completion = anthropic.completions.create(
model="claude-2",
max_tokens_to_sample=300,
prompt=prompt,
)
response = completion.completion
return response

Now, let’s test all of these functions through a single query. Feel free to replace ‘brown v board, supreme’ with another broadly defined search query, e.g., ‘google v oracle , appeal’, or ‘470 U.S. 298’, or any other case that you are studying for any of your classes.

search_query = "brown v board, supreme" 
results = search_legal_cases(search_query)
summary = get_summary(text)
print(summary)
"Here is a summary of the key points in Brown v. Board of Education:

Facts: This consolidated case involves black students in Kansas, South Carolina, Virginia, and Delaware who were denied admission to public schools attended by white students under laws permitting or requiring racial segregation.

Procedural History: The plaintiffs sued in federal district courts seeking admittance to the white schools. The district courts relied on the "separate but equal" doctrine from Plessy v. Ferguson and denied relief, finding the schools were substantially equal. The Supreme Court granted direct review.

Issue: Does the racial segregation of public schools deprive black students of the equal protection of the laws under the Fourteenth Amendment?

Holding: Yes. Separate educational facilities are inherently unequal. Racial segregation in public education violates the Equal Protection Clause.

Rationale: Segregating black students solely due to their race generates a feeling of inferiority that affects their motivation to learn. Public education is extremely important, so unequal access violates equal protection.

Decision: Reversed and remanded. Racial segregation in public schools is unconstitutional nationwide. Courts should craft decrees to admit students to public schools on a racially nondiscriminatory basis.

Concurring Opinions: None. All justices joined the unanimous opinion.

Dissenting Opinions: None."

If you want to see this code in action, check out its working prototype on the Streamlit platform.

I hope that this code serves you well and aids you in your noble pursuit of legal wisdom. Farewell for now, my dear friend, and may your endeavors be fruitful and your intellect ever sharp.

--

--

legaltextai

Gentle introduction of AI tools into the legal education and practice.