AI 허브 바로가기
국가법령정보 Open API 바로가기
국가법령정보 쓰는 방법 (수완동날다람쥐님의 멋진 블로그) 바로가기
판례 데이터에서 중요한 단어를 고른 후 빈칸을 뚫어
빈칸 맞추기 게임을 웹서비스로 제공한다.
표제어 사전은 AI hub에서 정리해준 법령용어 json 파일과,
국가법령정보 공동활용 웹사이트에서 제공하는 Open API를 활용하기로 했다.
(1) 데이터 불러오기
AI hub의 경우에는 회원가입을 한 뒤
데이터를 다운 받는 일은 끝이다.
국가법령정보 웹사이트는 신청을 해야한다.
로그인>>사용자 가입을 하는데
인적사항 이외에 섭
~례인 사례 API, 법률용어 API는 전부 신청했으며
(2) 데이터 가공하기
아래는 사전의 형태를 보기 쉽게 2개 단어만 떼어온 test_vocab.json파일이다.
잘 살펴보면 딕셔너리로 해석된 전체 json 안에 단어마다 key:딕셔너리가 저장되어있으며
key가 단어를 가리키는 uri, 짝지어진 딕셔너리의 key에는 4가지 uri json 오브젝트가 또 있다.
짝지 json 오브젝트는 name, related, type, label으로 네 종류이다.
{
"http://www.aihub.or.kr/kb/law/Term/납세대상자" : {
"http://www.aihub.or.kr/kb/law/name" : [ {
"type" : "literal" ,
"value" : "납세대상자"
}
] ,
"http://www.aihub.or.kr/kb/law/related" : [ {
"type" : "uri" ,
"value" : "http://www.aihub.or.kr/kb/law/Term/평균신고"
}],
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type" : [ {
"type" : "uri" ,
"value" : "http://www.aihub.or.kr/kb/law/Term"
}
] ,
"http://www.w3.org/2000/01/rdf-schema#label" : [ {
"type" : "literal" ,
"value" : "납세대상자"
}
]
}
,
"http://www.aihub.or.kr/kb/law/Term/촬영후" : {
"http://www.aihub.or.kr/kb/law/name" : [ {
"type" : "literal" ,
"value" : "촬영후"
}
] ,
"http://www.aihub.or.kr/kb/law/related" : [ {
"type" : "uri" ,
"value" : "http://www.aihub.or.kr/kb/law/Term/촬영"
}
] ,
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type" : [ {
"type" : "uri" ,
"value" : "http://www.aihub.or.kr/kb/law/Term"
}
] ,
"http://www.w3.org/2000/01/rdf-schema#label" : [ {
"type" : "literal" ,
"value" : "촬영후"
}
]
}
}
당장은 uri의 내용이 확인되지 않으므로, 단어만 긁어모아 사전을 구성한다.
나중에 데이터를 활용할 때 pandas를 쓰려고 toPandas() 함수를 추가했는데, 사실상 파서를 file descriptor를 초기화해주며 선언한 뒤 toPandas()만 실행하는 것으로 표제어 사전 생성은 마칠 수 있다.
import pandas as pd
import json
class ParseLemmJson():
def __init__(self,fd):
self.fd=fd
def open(self):
#표제어 파일 열기
with open(self.fd,'r',encoding='UTF-8') as f:
#json인식하기
try:
json_file=json.loads(f.read())
print('json file opened')
#json 내부 iterate하기
#keyword마다 iterate 되고 있다
term=[]
uris=[]
for item in json_file.items():
#tuple인 item 내부에는 Term, 그리고 uri 묶음이 있다
term.append(item[0].split('/')[-1])
print("The term of json object:",term[-1], item[-1].keys()) #Term과 나머지(uri 덩이)
uris.append(item[-1])#tuple of relevant uri
return term, uris #[단어], [(관련uri튜플)]
except json.decoder.JSONDecodeError:
print("String could not be converted to JSON")
print("failed to open file")
return -1
def toPandas(self):
term,uris=self.open()
return pd.DataFrame(term,columns=['vocab'])