[PYTHON] Create an easy-to-read pdf of laws and government ordinances using the law api

Introduction

If you want to refer to laws and regulations on the Internet, e-Gov Law Search published by the Ministry of Internal Affairs and Communications. And Law api, but the layout is subtle and the xml style sheet is not provided. Neither is very easy to use.

As an attempt to use the decree api in qiita's past articles,

-(Move / Repost) e-Gov Law API and XML Extraction of Articles Containing Specific Words Using Python -I made my own research tool using the law API [Smart Roppo] -Memo that you want to utilize e-Gov law data

If you want to see the laws and regulations in the online environment, you can refer to them, but there are times when you want to use the laws and regulations in the offline environment or print them out and use them on paper media, so I got it from the law api. I created a script to make the law into a pdf with an appropriate layout as shown below. EXOKk1CUYAAf07_.png In addition, this layout is arranged with reference to the pdf published in Japanese Law PDF + XML PDF and XML of Japanese law. Since the update of the homepage stopped in 2016, I decided to make it myself.

The usage etc. are described in detail below, but if you just want to use it, see [Environment construction (for law students)](# Environment construction (for law students)) and [Usage](# Usage). I think it's enough. [Code explanation](# Code explanation) is also written for reference if you want to handle the law api yourself or modify my code. Also, please note that Caution describes the case where it does not work properly.

Building an environment (for law students)

Basically, it is made to be useful for law learners, but I think that there are not many law learners who are familiar with Python and TeX, so I will explain the environment construction in a little more detail. If you can use Python and TeX, you don't need it, so skip it.

Introduce Python

Download the latest version of Python from the official Python site (https://www.python.org/). キャプチャ.PNG From the Downloads tab in the photo, select the one for your OS (I think it's either Windows or MacOS). If you have any questions in the detailed process, there are many web pages that explain the introduction (for example, https://gammasoft.jp/blog/python-install-and-code-run/), so please refer to that as well.

Once downloaded and installed, Python is ready.

Introduce TeX

Please install TeX Live (see https://texwiki.texjp.org/?TeX%20Live etc.). As for TeX, it seems that more law learners use it than Python, so if you can select LuaLaTeX as the typeset method in your current TeX usage environment, that's okay. After the installation is complete, start TeXworks, select "Settings" from the "Edit" tab, and set the "Default" of the typeset method on the "Typeset" tab to LuaLaTeX. キャプチャ.PNG

This completes the settings for TeX.

Other points that are likely to stumble

I will describe in detail the part of Usage that may stumble.

Code download

Please visit https://github.com/tbyuunagi/make_lawtex and press Clone or download of the photo. キャプチャ00.PNG

Download requests

Open a command prompt for Windows or a terminal for Mac (see https://techacademy.jp/magazine/5318 and https://techacademy.jp/magazine/5155) and enter "pip install requests" Please execute.

Run make_lawtex.py

If it is executed by double-clicking the downloaded make_lawtex.py (as described in [How to use](# Usage)), there is no problem. If you double-click to open Notepad etc., https://boukenki.info/python-py-file-doubleclick-kidou-jikkou-houhou/ etc. for Windows, https: / for Mac Please refer to /yutori-gi.com/pythonscript-run/ etc. and set to open the .py file from Python.

How to use

The code is below. https://github.com/tbyuunagi/make_lawtex

It should work fine if you can use Python and LuaLaTex. We have confirmed the operation on Windows10 and Ubuntu18.04.

Install the Python library requests.

$ pip install requests

When you run make_lawtex.py

Please enter the keyword you want to search

Is printed, so enter part or all of the law name you want to find. Then

1 Civil Code
2 Civil Code Enforcement Law Extract
3 Law concerning special provisions of the Civil Code concerning perfection requirements for movables and transfer of claims
4 Law Concerning Special Exceptions to the Civil Code Concerning Electronic Consumer Contracts
5 Ministerial Ordinance on Supervision of Special Civil Code Corporations under the jurisdiction of the Minister of Internal Affairs and Communications
6 Law concerning special provisions of the Civil Code concerning the period for which inheritance should be approved or abandoned due to the Great East Japan Earthquake
7 Ministerial Ordinance that stipulates the amount specified by the Ministry of Justice Ordinance stipulated in Article 909-2 of the Civil Code
8 Ministerial Ordinance Concerning Notification of Standard Ratio under Article 404, Paragraph 3 of the Civil Code and Article 404, Paragraph 5 of the same Article
which one?

The choices are shown with a number in the form of (for example, when searching for "Civil Code"), so enter the required law by number. This completes the Python process and generates "law name" .xml (obtained from the law api) and "law name" .tex under the laws directory (in the case of civil law, civil law.xml and civil law. tex).

If you typeset the .tex file generated here with LuaLaTeX, you will get a pdf file with the layout shown at the beginning. For example, when using TeXworks after installing TeX Live, the part surrounded by blue in the figure below will be set to LuaLaTeX. Capture.PNG In addition, it is necessary to perform the type set three times in order to display the table of contents on the correct page (see https://cns-guide.sfc.keio.ac.jp/2000/8/3/5.html etc.) .. To be honest, I feel like I'm doing something wrong, but I'm not familiar with TeX, so I don't know. I would be grateful if anyone who knows a more appropriate method could point it out.

Code description

If you want to use the law api for the same purpose, of course, if you want to use the law api for other purposes, I think that it can be diverted to some extent, so I will explain the stumbling part and it was a little complicated. Law API Specifications (Version 1) will be referred to as appropriate, so please prepare this if you want to read in detail. Please think that it refers to this when referring to "specifications" without notice below.

import requests
import os
import xml.etree.ElementTree as ET

The above is the library used.

#Use only when you get the list of laws and save and update the xml file
def  get_lawlist():
    r = requests.get('https://elaws.e-gov.go.jp/api/1/lawlists/1')
    root = ET.fromstring(r.text)
    tree = ET.ElementTree(element=root)
    if not os.path.exists('laws/'):
        os.mkdir('laws')
    tree.write('laws/lawlist.xml', encoding='utf-8', xml_declaration=True)
    return

Access the law name list acquisition API (specifications pages 5 and 10) to acquire the law name list xml file. This xml file is used when searching for the name of a law below. By default

r = requests.get('https://elaws.e-gov.go.jp/api/1/lawlists/1')

However, if you do not need a ministerial ordinance, please refer to section 4.2 of page 10 of the specifications and change the end of the url.

#A function that returns the official name and number of a law after searching for a specific law from the list of law names by keyword.
def search_Laws(key):
    tree = ET.parse('laws/lawlist.xml')
    root = tree.getroot()
    LawName_list = []
    LawNo_list = []
    for child in root[1]:
        if child.tag != 'Category':
            if key in child[0].text:
                LawName_list.append(child[0].text)
                LawNo_list.append(child[1].text)
    return LawName_list, LawNo_list

As commented, it is a function that takes a keyword as an argument and returns a list of laws and regulations that include the keyword. As a precaution when dealing with the law api, as you can see from the contents of the xml obtained from the above-mentioned law name list acquisition API, a certain law is one that we recognize as a name (LawName such as "Civil Law"). In addition, attributes such as Law No. m of the Japanese calendar n year (Law No, such as "Law No. 89 of Meiji 29") are given. Of course, human beings do not know what the law is about by looking at the law number, but in the law api, this law number is a unique key to specify a specific law ( (See get_LawContent ()), so we need to be able to manage these LawNos together with the LawNames that we can easily recognize.

#Input / output
def get_Law(search_Laws):
    LawName_list = search_Laws[0]
    LawNo_list = search_Laws[1]
    if len (LawName_list) == 0:
        print('There are no applicable laws')
        return None
    if len(LawName_list) == 1:
        return LawName_list[0], LawNo_list[0]
    for i,x in enumerate(LawName_list):
        print(i+1,x)
    print('which one?')
    input_num = int(input())-1
    LawName = LawName_list[input_num]
    LawNo = LawNo_list[input_num]
    return LawName, LawNo

This is the input / output part that enumerates and selects laws and regulations that match the keywords. There is no limit to the number of laws listed, so searching by "law" can be a daunting task. If such an operation is unpleasant, please limit it appropriately.

# get_Obtain the content using the result of Law and save it under the relevant law name
def get_LawContent(get_Law):
    LawName = get_Law[0]
    LawNo = get_Law[1]
    r = requests.get('https://elaws.e-gov.go.jp/api/1/lawdata/'+LawNo)
    root = ET.fromstring(r.text)
    tree = ET.ElementTree(element=root)
    if not os.path.exists('laws/'):
        os.mkdir('laws')
    tree.write('laws/' + LawName + '.xml', encoding='utf-8', xml_declaration=True)
    return LawName

Obtain the desired law using the law acquisition API (page 6 of the specifications). As I mentioned a little

r = requests.get('https://elaws.e-gov.go.jp/api/1/lawdata/'+LawNo)

Be aware that what you should specify at the end of the url on the line is a Law No, like Law No.

Also, as shown in Notes, some laws and regulations cannot be obtained by this function because they cannot be obtained by the law api in the first place.

def main():
    get_lawlist()
    Law_Name = search_get_LawContent()
    result = xml_to_tex(Law_Name)
    return result

It is for execution. I have decided to run get_lawlist () every time from the viewpoint of always using the latest list, but the processing is a little slow because lawlist.xml has a reasonable data size. Since get_lawlist () is a process that does not need to be done many times once it is executed, I think that it is okay to comment out this line from the second time onward.

The above is the explanation of the function. You can see what is not explained. If you don't like the generated layout, compare the resulting TeX file with the xml_to_tex () function and make the necessary corrections. It's uselessly long, but the preamble part is all written at the beginning (line 79 of make_lawtex.py), so it should be enough to modify it in relation to the layout.

Notes

Some laws and regulations cannot be obtained by the law api due to data size. Specifically, I would like you to see the restrictions on https://www.e-gov.go.jp/elaws/interface_api/index.html, but what seems to be necessary for legal learning and cannot be obtained is There are the Income Tax Law and the Special Taxation Measures Law. We plan to respond in the near future, but please note that we cannot obtain these laws and regulations at this time.

in conclusion

Redistribution, modification, and self-made remarks are all free, so feel free to use them. If you have any bugs or suggestions for improvement (not only about the code but also about this article), please let us know in the comments here, on github's pull request, or on My Twitter. ..

Recommended Posts

Create an easy-to-read pdf of laws and government ordinances using the law api
Create an application using the Spotify API
Create an API to convert PDF files to TIF images with FastAPI and Docker
Create a REST API using the model learned in Lobe and TensorFlow Serving.
Create an app that works well with people's reports using the COTOHA API
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
Get all songs of Arashi's song information using Spotify API and verify the index
[Python] Get the text of the law from the e-GOV Law API
Send and receive Gmail via the Gmail API using Python
I tried using the API of the salmon data project
Create a shape on the trajectory of an object
I built an application with Lambda that notifies LINE of "likes" using the Qiita API
(Move / Repost) Extraction of articles containing specific words using e-Gov Law API and XML Python
Get the trading price of virtual currency and create a chart with API of Zaif exchange
Get data using Ministry of Internal Affairs and Communications API
Create a real-time auto-reply bot using the Twitter Streaming API
View the contents of the queue using the RabbitMQ Management Web API
[Python] I tried collecting data using the API of wikipedia
The story of creating a database using the Google Analytics API
I made an original program guide using the NHK program guide API.
Get the weather using the API and let the Raspberry Pi speak!
Is the lottery profitable? ~ LOTO7 and the law of large numbers ~
Create an application that just searches using the Google Custom Search API with Python 3.3.1 in Bottle