Selaa lähdekoodia

added parser for preliminary response

Harsh Parikh 2 vuotta sitten
vanhempi
commit
036e7aaa31

+ 49 - 0
server_documents/preliminary_response/main.py

@@ -0,0 +1,49 @@
+"""This module is used to parse PDF for expert reports.
+"""
+from collections import defaultdict
+import os
+import tika
+from tika import parser
+import pandas as pd
+from pandas import DataFrame
+from utils import (
+    plaintiff_extraction,
+    defendent_extraction,
+    case_number_extraction,
+    patent_extraction,
+    email_extraction,
+    address_extraction,
+)
+
+
+tika.initVM()
+
+
+def main(path: str) -> DataFrame:
+    """
+    The functions iterates through all the given files and gathers the data in the \
+    form of a dataframe
+    """
+    data = defaultdict(list)
+    required_files = [file for file in os.listdir(path) if file.find(".pdf") != -1]
+    for file in required_files:
+        print(file)
+        content = parser.from_file(file)["content"]
+        data["file"].append(file)
+        data["email"].append(email_extraction(content))
+        data["plaintiff"].append(plaintiff_extraction(content))
+        data["defendant"].append(defendent_extraction(content))
+        data["case_number"].append(case_number_extraction(content))
+        data["patents"].append(patent_extraction(content))
+        data["address"].append(address_extraction(content))
+
+    data_expert = pd.DataFrame(data)
+    data_expert.to_csv("required_data.csv")
+    return data_expert
+
+
+if __name__ == "__main__":
+    HOME_DIR = os.path.expanduser("~")
+    BASE_DIR = "Code/pdf_parser/server_documents/preliminary_response"
+    PATH = os.path.join(HOME_DIR, BASE_DIR)
+    print(main(PATH))

+ 83 - 0
server_documents/preliminary_response/utils.py

@@ -0,0 +1,83 @@
+import re
+
+def email_extraction(content: str) -> str:
+    """
+    Extracts emails from a document.
+    """
+    regex = r"(\w+\-)?\w+@[a-z]+\.[a-z]{2,3}"
+    result = []
+    emails = re.compile(regex, re.IGNORECASE)
+    for email in emails.finditer(content):
+        result.append(email.group())
+    return result
+
+def telephone_number_extraction(content: str) -> str:
+    """
+    Extracts telephone number[s?] from a document
+    """
+    regex = r"\(?\d{3}\)?[\-|\s]\d{3}[\s|\-]\d{4}"
+    numbers = re.compile(regex, re.IGNORECASE)
+    result = []
+    for number in numbers.finditer(content):
+        result.append(number.group())
+    return result
+
+def address_extraction(content: str) -> str:
+    """
+    extracts address from the documents.
+    """
+    regex_address = r"\w+.*\n\w+.*\n\w+.*\w{2,4}\s\d{5}"
+    try:
+        return re.search(regex_address, content).group(0)
+    except:
+        return "None"
+
+def case_number_extraction(content: str) -> str:
+    """
+    Extracts the case number from the documents.
+    """
+    regex = r"Case\sNo\.\s(\d\:\d{2}\-\w{2}\-\d{5}\-\w{3})"
+    results = set()
+    case_number = re.compile(regex, re.IGNORECASE)
+    for current in case_number.finditer(content):
+        results.add(current.groups()[0])
+    return list(results)
+
+def plaintiff_extraction(content: str) -> str:
+    """
+    Extracts the plaintiff from the document
+    """
+    regex = r"(\w.*)\n\s?\n?\s?(Plaintiffs?|Petitioner)"
+    try:
+        return re.search(regex, content).group(1)
+    except:
+        return "None"
+
+def defendent_extraction(content: str) -> str:
+    """
+    Extracts the defendant from the document
+    """
+    regex = r"(\w.*?)\n\s?\n?\s?\s?(Defendants|Patent\sOwners?)"
+    try:
+        return re.search(regex, content).group(1)
+    except:
+        return "None"
+
+def patent_extraction(content: str) -> str:
+    """
+    Extracts patent numbers from the document
+    """
+    regex = r"\d{1,3}\,\d{1,3}\,\d{3}\,?"
+    result = set()
+    patent = re.compile(regex, re.IGNORECASE)
+    for current in patent.finditer(content):
+        result.add(current.group().replace(",", ""))
+    return list(result)
+
+def acronym_extraction(content: str) -> str:
+    regex = r"\(\“([A-Z]{3,4})\”\)"
+    results = set()
+    acronym = re.compile(regex, re.IGNORECASE)
+    for current in acronym.finditer(content):
+        results.add(current.group(1))
+    return list(results)