Giter Site home page Giter Site logo

justmars / statute-patterns Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 2.16 MB

Construction of regex patterns for Philippine statutory law.

Home Page: https://lawsql.com

License: BSD 3-Clause "New" or "Revised" License

Python 90.51% Jupyter Notebook 9.49%
legal legal-analytics legal-entity-identifier natural-language-processing regex

statute-patterns's People

Contributors

justmars avatar

Watchers

 avatar

statute-patterns's Issues

Serialize: Resolution of the Court En Banc - XX

Deal with legacy situation.

Bad codification event; see values={'locator': 'Section 1', 'caption': 'Scope.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Modified'}
Bad codification event; see values={'locator': 'A', 'caption': 'Pleadings allowed.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'B', 'caption': 'Verifications.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'Section 5', 'caption': 'Answer.', 'content': 'Within ten (10) days from service of summons,', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'Section 6', 'caption': 'Effect of failure to answer.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'Section 7', 'caption': 'Preliminary conference; appearance of parties.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'Section 8', 'caption': 'Record of preliminary conference.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'Section 9', 'caption': 'Submission of affidavits and position papers.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'Section 10', 'caption': 'Rendition of judgment.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'Section 18', 'caption': 'Referral to Lupon.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'Section 19', 'caption': 'Prohibited pleadings and motions.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'Section 20', 'caption': 'Affidavits.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991'}
Bad codification event; see values={'locator': 'Section 1', 'caption': 'Scope.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Modified', 'material_path': '1.1.2.7.9.3.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'A', 'caption': 'Pleadings allowed.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.4.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'B', 'caption': 'Verifications.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.4.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'Section 5', 'caption': 'Answer.', 'content': 'Within ten (10) days from service of summons,', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.6.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'Section 6', 'caption': 'Effect of failure to answer.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.7.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'Section 7', 'caption': 'Preliminary conference; appearance of parties.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.8.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'Section 8', 'caption': 'Record of preliminary conference.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.9.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'Section 9', 'caption': 'Submission of affidavits and position papers.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.10.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'Section 10', 'caption': 'Rendition of judgment.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.11.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'Section 18', 'caption': 'Referral to Lupon.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.12.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'Section 19', 'caption': 'Prohibited pleadings and motions.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.13.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}
Bad codification event; see values={'locator': 'Section 20', 'caption': 'Affidavits.', 'statute': 'Resolution of the Court En Banc dated 10-15-1991', 'action': 'Originated', 'material_path': '1.1.2.7.9.14.', 'codification_id': 'mv-2022-rule-am-19-10-20-sc-civil-procedure-rules-of-court-v1'}

Handle non-serialized rules

class Base(Enum):
    const = r"((PHIL\.\s+)?CONST(ITUTION|\.?)|(Phil\.\s+)?Const(itution|\.?))"  # can refer to 3
    admin = r"Administrative\s+Code"  # can refer to 3
    elect = r"Election\s+Code"  # can refer to 2
    corp = r"Corporation\s+Code"  # can refer to 2
    agrarian = r"Agrarian\s+Reform\s+Code"  # can refer to 2
    civil = r"Civil\s+Code"  # can refer to 2
    tax = r"(N\.?I\.?R\.?C\.?|National\s+Internal\s+Revenue\s+Code)"  # can refer to 3
    penal = r"Penal\s+Code"  # can refer to 2
    coop = r"Cooperative\s+Code"  # can refer to 2
    insure = r"Insurance\s+Code"  # can refer to 2
    rules = r"Rules\s+of\s+Court"  # can refer to 3

    @property
    def pattern(self) -> Pattern:
        return re.compile(self.value)

    def find(self, raw: str) -> Match | None:
        return self.pattern.search(raw)


NAMED_LAWS = [
    NamedLaw(
        base=Base.const.value,
        name="1987 Constitution",
        year=1987,
        serial=StatuteBase(
            statute_category=StatuteCategory.CONST, statute_serial_id="1987"
        ),
        aliases=[
            add_suffix_PH(rf"1987\s+{Base.const.value}"),
            rf"1987\s+{Base.const.value}",
        ],
    ),
    NamedLaw(
        base=Base.const.value,
        name="1973 Constitution",
        year=1973,
        serial=StatuteBase(
            statute_category=StatuteCategory.CONST, statute_serial_id="1973"
        ),
        aliases=[
            add_suffix_PH(rf"1973\s+{Base.const.value}"),
            rf"1973\s+{Base.const.value}",
        ],
    ),
...

Add fields for sqlpyd use

class Rule(BaseModel):
    """Used to identify the statute based on a category and identifier. The `col` and `index` field attributes are not native to Pydantic; these are populated in anticipation of future use via the `sqlpyd` library."""

    cat: StatuteCategory = Field(
        col=str,
        index=True,
    )
    idx: constr(to_lower=True) = Field(  # type: ignore
        ...,
        regex="[a-z0-9-]+",
        col=str,
        index=True,
    )

This becomes usable in the following context:

class CodeRow(Page, Rule, TableConfig):  # see Rule use here
    __prefix__ = "lex"
    __tablename__ = "codifications"
    __indexes__ = [["statute_category", "statute_serial_id"]]
    statute_id: str | None = Field(
        None, col=str, fk=(StatuteRow.__tablename__, "id")
    )

TableConfig is sqlpyd-native and detects that the fields cat and idx are going to be part of the database table "codifications".

Count number of unique statutes per text

class StatuteCounted(NamedTuple):
    statute: StatuteBase
    mentions: int

    @classmethod
    def extract_statutes(cls, content: str) -> Iterator[str]:
        """Extract iterator of statute serial texts so that these can be counted via `Counter()`"""
        if statutes := StatuteID.get_statute_matches(content):
            for s in statutes:
                if isinstance(s, StatuteBase):  # cat + idx exists
                    if s.is_single:
                        yield f"{s.statute_category} {s.statute_serial_id}"
                    else:  # more than one StatuteBase should exist
                        for dx in s.deconstructed_identifiers:
                            if isinstance(dx, StatuteBase):  # cat + idx exists
                                yield f"{dx.statute_category} {dx.statute_serial_id}"

    @classmethod
    def count_statutes(cls, content: str):
        statutes_detected = list(cls.extract_statutes(content))
        counted = Counter(statutes_detected)
        unique_statutes = iter(set(statutes_detected))
        for i in unique_statutes:
            mentions = counted[i]
            elements = i.split()
            cat = StatuteCategory(elements[0])
            idx = elements[1]
            try:
                base = StatuteBase(statute_category=cat, statute_serial_id=idx)
                yield cls(statute=base, mentions=mentions)
            except Exception as e:
                msg = f"Bad {elements=} in forming statute base; see {e=}"
                logger.error(msg)

Get cat, idx from serial title

Node items have the format:

- locator: x
  action: y
  statute: Republic Act No. 386 # this is a standardized serial title

Extract the cat (ra) and idx (386) values from the serial title.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.