Files
epo-api/xml_to_json.py
T

626 lines
30 KiB
Python

import xmltodict
import sys
import os
from datetime import date
import json
# writes the python-dict into a json
def write_to_json(es_dict, id, name):
today = date.today()
path = str(today) + "_patent_jsons"
if not os.path.exists(path):
os.mkdir(path)
try:
with open(path + "/" + id + ".json", "w") as outfile:
json.dump(es_dict, outfile)
except:
print("-Failed to write "+ name)
# takes an string and deletes xml-brackets
def del_Brackets(text):
text = text.replace("<b>", "")
text = text.replace("</b>", "")
text = text.replace("<i>", "")
text = text.replace("</i>", "")
text = text.replace("<u>", "")
text = text.replace("</u>", "")
text = text.replace("<o>", "")
text = text.replace("</o>", "")
text = text.replace("<sup>", "")
text = text.replace("</sup>", "")
text = text.replace("<sup2>", "")
text = text.replace("</sup2>", "")
text = text.replace("<sub>", "")
text = text.replace("</sub>", "")
text = text.replace("<sub2>", "")
text = text.replace("</sub2>", "")
text = text.replace("<smallcaps>", "")
text = text.replace("</smallcaps>", "")
text = text.replace("<br>", "")
text = text.replace("</br>", "\n")
text = text.replace("<br/>", "\n")
text = text.replace("<pre>", "")
text = text.replace("</pre>", "")
while not text.find("<figref") == -1:
p1 = text[:(text.find("<figref"))]
text = text[:(text.find("<figref"))]
text = text[(text.find(">") + 1):]
p2 = text = text[:(text.find("</figref>"))]
text = text = text[(text.find("</figref>") + 9):]
text = p1 + " " + p2 + " " + text
while not text.find("<crossref") == -1:
p1 = text[:(text.find("<crossref"))]
text = text[:(text.find("<crossref"))]
text = text[(text.find(">") + 1):]
p2 = text = text[:(text.find("</crossref>"))]
text = text = text[(text.find("</crossref>") + 11):]
text = p1 + " " + p2 + " " + text
while not text.find("<claim-ref") == -1:
p1 = text[:(text.find("<claim-ref"))]
text = text[:(text.find("<claim-ref"))]
text = text[(text.find(">") + 1):]
p2 = text = text[:(text.find("</claim-ref>"))]
text = text = text[(text.find("</claim-ref>") + 12):]
text = p1 + " " + p2 + " " + text
while not text.find("<img") == -1:
p1 = text[:(text.find("<img"))]
text = text[:(text.find("<img"))]
text = text[(text.find(">") + 1):]
text = p1 + " " + text
return text
# parses the xml-file into elasticsearch
# takes out relevant information
def parse(xml_dict, name, xml_text):
es_dict = {}
# epo tags
es_dict["country"] = xml_dict.get("ep-patent-document").get("@country")
es_dict["doc_number"] = xml_dict.get("ep-patent-document").get("@doc-number")
es_dict["kind"] = xml_dict.get("ep-patent-document").get("@kind")
es_dict["date_publication"] = xml_dict.get("ep-patent-document").get("@date-publ")
if (es_dict["country"] == None or es_dict["doc_number"] == None or es_dict["kind"] == None):
print("Missing required data in " + name + ". Can't parse this file.")
return
id = str(es_dict["country"]) + str(es_dict["doc_number"]) + str(es_dict["kind"])
# SDOBI (B-tags)
sdobi = xml_dict.get("ep-patent-document").get("SDOBI")
es_dict["application_number"] = sdobi.get("B200").get("B210")
es_dict["application_filing_date"] = sdobi.get("B200").get("B220").get("date")
# date tags
if sdobi.get("B200").get("B240") != None:
b240 = sdobi.get("B200").get("B240")
if b240.get("B241") != None:
es_dict["date_of_request_for_examination"] = b240.get("B241").get("date")
if b240.get("B242") != None:
es_dict["date_of_despatch_of_1st_exam_report"] = b240.get("B242").get("date")
if b240.get("B243") != None:
es_dict["date_of_patent_maintained_as_amended"] = b240.get("B243").get("date")
if b240.get("B244") != None:
es_dict["request_for_conversion_to_natl_appl_date"] = b240.get("B244").get("date")
es_dict["request_for_conversion_to_natl_appl_country"] = b240.get("B244").get("ctry")
if b240.get("B245") != None:
es_dict["date_of_suspension_interruption"] = b240.get("B245").get("date")
if b240.get("B245EP") != None:
es_dict["indicates_the_type_of_suspension_or_interruption"] = b240.get("B245EP")
if b240.get("B246") != None:
es_dict["date_of_resumption_of_proceedings"] = b240.get("B246").get("date")
# patent information tags
if sdobi.get("B500") != None:
# patent classification tags from 2005 and earlier
if sdobi.get("B500").get("B510") != None:
es_dict["ipc"] = sdobi.get("B500").get("B510")
if sdobi.get("B500").get("B510").get("B511") != None:
es_dict["section"] = sdobi.get("B500").get("B510").get("B511")
if sdobi.get("B500").get("B510").get("B512") != None:
es_dict["class"] = sdobi.get("B500").get("B510").get("B512")
if sdobi.get("B500").get("B510").get("B513") != None:
es_dict["subclass"] = sdobi.get("B500").get("B510").get("B513")
if sdobi.get("B500").get("B510").get("B514") != None:
es_dict["linked_indexing_code"] = sdobi.get("B500").get("B510").get("B514")
if sdobi.get("B500").get("B510").get("B515") != None:
es_dict["unlinked_indexing_code"] = sdobi.get("B500").get("B510").get("B515")
if sdobi.get("B500").get("B510").get("B517EP") != None:
es_dict["non_obligatory_suppl_class"] = sdobi.get("B500").get("B510").get("B517EP")
# patent classification tags from 2006 and later
# its one string that needs to be splittet into the classification parts
if sdobi.get("B500").get("B510EP") != None:
ip = sdobi.get("B500").get("B510EP").get("classification-ipcr")
texte = []
try:
if ip.get("text") != None:
texte.append(ip.get("text"))
except:
for i in range(len(ip)):
texte.append(ip[i].get("text"))
es_dict["ipc"] = texte
section = []
clas = []
subclass = []
main_group = []
sub_group = []
version= []
level = []
position = []
classification_value = []
action = []
data1 = []
data2 = []
office = []
for text in texte:
section.append(text[0:1])
clas.append(text[1:3])
subclass.append(text[3:4])
main_group.append(text[4:8].replace(" ", ""))
sub_group.append(text[9:15].replace(" ", ""))
version.append(text[19:27])
level.append(text[27:28])
position.append(text[28:29])
classification_value.append(text[29:30])
action.append(text[30:38])
data1.append(text[38:39])
data2.append(text[39:40])
office.append(text[40:42])
es_dict["section"] = section
es_dict["class"] = clas
es_dict["subclass"] = subclass
es_dict["main_group"] = main_group
es_dict["sub_group"] = sub_group
es_dict["version"] = version
es_dict["level"] = level
es_dict["position"] = position
es_dict["classification_value"] = classification_value
es_dict["action"] = action
es_dict["data1"] = data1
es_dict["data2"] = data2
es_dict["office"] = office
# title in en, de and fr
if sdobi.get("B500").get("B540") != None:
if sdobi.get("B500").get("B540").get("B541")[0] == "de":
es_dict["title_de"] = sdobi.get("B500").get("B540").get("B542")[0]
elif sdobi.get("B500").get("B540").get("B541")[1] == "de":
es_dict["title_de"] = sdobi.get("B500").get("B540").get("B542")[1]
elif sdobi.get("B500").get("B540").get("B541")[2] == "de":
es_dict["title_de"] = sdobi.get("B500").get("B540").get("B542")[2]
if sdobi.get("B500").get("B540").get("B541")[0] == "en":
es_dict["title_en"] = sdobi.get("B500").get("B540").get("B542")[0]
elif sdobi.get("B500").get("B540").get("B541")[1] == "en":
es_dict["title_en"] = sdobi.get("B500").get("B540").get("B542")[1]
elif sdobi.get("B500").get("B540").get("B541")[2] == "en":
es_dict["title_en"] = sdobi.get("B500").get("B540").get("B542")[2]
if sdobi.get("B500").get("B540").get("B541")[0] == "fr":
es_dict["title_fr"] = sdobi.get("B500").get("B540").get("B542")[0]
elif sdobi.get("B500").get("B540").get("B541")[1] == "fr":
es_dict["title_fr"] = sdobi.get("B500").get("B540").get("B542")[1]
elif sdobi.get("B500").get("B540").get("B541")[2] == "fr":
es_dict["title_fr"] = sdobi.get("B500").get("B540").get("B542")[2]
# keywords_and_descriptors
if sdobi.get("B500").get("B550") != None:
if sdobi.get("B500").get("B550").get("B552") != None:
es_dict["keywords_and_descriptors"] = sdobi.get("B500").get("B550").get("B552")
# parties concerned with the document
if sdobi.get("B700") != None:
# applicants
if sdobi.get("B700").get("B710") != None:
if sdobi.get("B700").get("B710").get("B711") != None:
try:
if sdobi.get("B700").get("B710").get("B711").get("snm") != None:
if sdobi.get("B700").get("B710").get("B711").get("adr") != None:
name = sdobi.get("B700").get("B710").get("B711").get("snm")
street = sdobi.get("B700").get("B710").get("B711").get("adr").get("str")
city = sdobi.get("B700").get("B710").get("B711").get("adr").get("city")
country = sdobi.get("B700").get("B710").get("B711").get("adr").get("ctry")
applicant = name + ", " + street + ", " + city + ", " + country
es_dict["applicants"] = [applicant]
else:
applicant = sdobi.get("B700").get("B710").get("B711").get("snm")
es_dict["applicants"] = [applicant]
elif sdobi.get("B700").get("B710").get("B711").get("B715EP") != None:
if sdobi.get("B700").get("B710").get("B711").get("B715EP").get("text") != None:
applicant = sdobi.get("B700").get("B710").get("B711").get("B715EP").get("text")
es_dict["applicants"] = [applicant]
except:
applis = []
for a in range(len(sdobi.get("B700").get("B710").get("B711"))):
appli = sdobi.get("B700").get("B710").get("B711")[a]
if appli.get("adr") != None:
name = appli.get("snm")
street = appli.get("adr").get("str")
city = appli.get("adr").get("city")
country = appli.get("adr").get("ctry")
applis.append(name + ", " + street + ", " + city + ", " + country)
else:
applis.append(appli.get("snm"))
es_dict["applicants"] = applis
# inventors
if sdobi.get("B700").get("B720") != None:
if sdobi.get("B700").get("B720").get("B721") != None:
try:
if sdobi.get("B700").get("B720").get("B721").get("snm") != None:
if sdobi.get("B700").get("B720").get("B721").get("adr") != None:
name = sdobi.get("B700").get("B720").get("B721").get("snm")
street = sdobi.get("B700").get("B720").get("B721").get("adr").get("str")
city = sdobi.get("B700").get("B720").get("B721").get("adr").get("city")
country = sdobi.get("B700").get("B720").get("B721").get("adr").get("ctry")
inventor = name + ", " + street + ", " + city + ", " + country
es_dict["inventors"] = [inventor]
else:
inventor = sdobi.get("B700").get("B720").get("B721").get("snm")
es_dict["inventors"] = [inventor]
elif sdobi.get("B700").get("B720").get("B721").get("B725EP") != None:
if sdobi.get("B700").get("B720").get("B721").get("B725EP").get("text") != None:
inventor = sdobi.get("B700").get("B720").get("B721").get("B725EP").get("text")
es_dict["inventors"] = [inventor]
except:
invs = []
for i in range(len(sdobi.get("B700").get("B720").get("B721"))):
inv = sdobi.get("B700").get("B720").get("B721")[i]
if inv.get("adr") != None:
name = inv.get("snm")
street = inv.get("adr").get("str")
city = inv.get("adr").get("city")
country = inv.get("adr").get("ctry")
invs.append(name + ", " + street + ", " + city + ", " + country)
else:
invs.append(inv.get("snm"))
es_dict["inventors"] = invs
# representatives
if sdobi.get("B700").get("B740") != None:
if sdobi.get("B700").get("B740").get("B741") != None:
try:
if sdobi.get("B700").get("B740").get("B741").get("snm") != None:
if sdobi.get("B700").get("B740").get("B741").get("adr") != None:
name = sdobi.get("B700").get("B740").get("B741").get("snm")
street = sdobi.get("B700").get("B740").get("B741").get("adr").get("str")
city = sdobi.get("B700").get("B740").get("B741").get("adr").get("city")
country = sdobi.get("B700").get("B740").get("B741").get("adr").get("ctry")
rep = name + ", " + street + ", " + city + ", " + country
es_dict["representatives"] = [rep]
else:
rep = sdobi.get("B700").get("B740").get("B741").get("snm")
es_dict["representatives"] = [rep]
elif sdobi.get("B700").get("B740").get("B741").get("B745EP") != None:
if sdobi.get("B700").get("B740").get("B741").get("B745EP").get("text") != None:
rep = sdobi.get("B700").get("B740").get("B741").get("B745EP").get("text")
es_dict["representatives"] = [rep]
except:
reps = []
for r in range(len(sdobi.get("B700").get("B740").get("B741"))):
rep = sdobi.get("B700").get("B740").get("B741")[r]
if rep.get("adr") != None:
name = rep.get("snm")
street = rep.get("adr").get("str")
city = rep.get("adr").get("city")
country = rep.get("adr").get("ctry")
reps.append(name + ", " + street + ", " + city + ", " + country)
else:
reps.append(inv.get("snm"))
es_dict["representatives"] = reps
# countries related with the patent
if sdobi.get("B800") != None:
# states
if sdobi.get("B800").get("B840") != None:
states = []
if sdobi.get("B800").get("B840").get("ctry") != None:
for s in range(len(sdobi.get("B800").get("B840").get("ctry"))):
states.append(sdobi.get("B800").get("B840").get("ctry")[s])
es_dict["states"] = states
# extended startes
if sdobi.get("B800").get("B844EP") != None:
extended_states = []
if sdobi.get("B800").get("B844EP").get("B845EP") != None:
try:
if sdobi.get("B800").get("B844EP").get("B845EP").get("ctry") != None:
state = sdobi.get("B800").get("B844EP").get("B845EP").get("ctry")
extended_states.append(state)
except:
for e in range(len(sdobi.get("B800").get("B844EP").get("B845EP"))):
state = sdobi.get("B800").get("B844EP").get("B845EP")[e].get("ctry")
extended_states.append(state)
es_dict["extended_states"] = extended_states
# abstract
abstract = xml_dict.get("ep-patent-document").get("abstract")
if abstract != None:
es_dict["abstract_lang"] = abstract.get("@lang")
p = abstract.get("p")
try:
es_dict["abstract"] = p.get("#text")
except:
text = ""
for i in range(len(p)):
text = text + p[i].get("#text") + " "
es_dict["abstract"] = text
# description
description = xml_dict.get("ep-patent-document").get("description")
if description != None:
es_dict["description_lang"] = description.get("@lang")
# first just header and paragraph as an array (xml-elements are still included in the paragraphs
if description.get("heading") != None:
heading = description.get("heading")
header = []
for i in range(len(heading)):
if heading[i].get("b") != None:
header.append(heading[i].get("b"))
elif heading[i].get("#text") != None:
header.append(heading[i].get("#text"))
#es_dict["description_header"] = header
if description.get("p") != None:
paragraph = description.get("p")
#es_dict["description_paragraph"] = paragraph
# second the sorted description
# filter out xml_description
xml_description = xml_text[(xml_text.find(">", (xml_text.find("<description"))) + 1):(xml_text.find("</description>"))]
description_text = ""
while (len(xml_description) > 0):
if xml_description.startswith("<heading"):
xml_description = xml_description[(xml_description.find(">") + 1):]
if xml_description.startswith("<"):
xml_description = xml_description[(xml_description.find(">") + 1):]
text = xml_description[:xml_description.find("<")]
description_text += text + "\n"
xml_description = xml_description[(xml_description.find("</heading>") + 10):]
elif xml_description.startswith("<p"):
xml_description = xml_description[(xml_description.find(">") + 1):]
p = xml_description[:xml_description.find("</p>")]
xml_description = xml_description[(xml_description.find("</p>") + 4):]
if p.find("<") == -1:
description_text += p + "\n"
else:
while len(p) > 0:
if p.startswith("<"):
if p.startswith("<br"):
p = p[(p.find(">") + 1):]
description_text += "\n"
elif p.startswith("<img"):
p = p[(p.find(">") + 1):]
elif p.startswith("<dl"):
p = p[(p.find(">") + 1):]
list = p[:(p.find("</dl>"))]
p = p[(p.find("</dl>") + 5):]
description_text += "\n"
while (len(list) > 0):
list = list[(list.find(">") + 1):]
dt = list[:(list.find("</dt>"))]
list = list[(list.find("</dt>") + 5):]
list = list[(list.find(">") + 1):]
dd = list[:(list.find("</dd>"))]
list = list[(list.find("</dd>") + 5):]
dt = del_Brackets(dt)
dd = del_Brackets(dd)
description_text += " - " + dt + ": " + dd + "\n"
elif p.startswith("<ol"):
description_text += "\n"
p = p[(p.find(">") + 1):]
list = p[:(p.find("</ol>"))]
p = p[(p.find("</ol>") + 5):]
while (len(list) > 0):
list = list[(list.find(">") + 1):]
text = list[:(list.find("</li>"))]
list = list[(list.find("</li>") + 5):]
text = del_Brackets(text)
description_text += " - " + text + "\n"
elif p.startswith("<ul"):
description_text += "\n"
p = p[(p.find(">") + 1):]
list = p[:(p.find("</ul>"))]
p = p[(p.find("</ul>") + 5):]
while (len(list) > 0):
list = list[(list.find(">") + 1):]
text = list[:(list.find("</li>"))]
list = list[(list.find("</li>") + 5):]
text = del_Brackets(text)
description_text += " - " + text + "\n"
elif p.startswith("<figref"):
p = p[(p.find(">") + 1):]
text = p[:(p.find("</figref>"))]
text = del_Brackets(text)
description_text += " " + text + " "
p = p[(p.find("</figref>") + 9):]
elif p.startswith("<crossref"):
p = p[(p.find(">") + 1):]
text = p[:(p.find("</crossref>"))]
text = del_Brackets(text)
description_text += " " + text + " "
p = p[(p.find("</crossref>") + 11):]
else:
text = p[:(p.find("<"))]
description_text += text
p = p[p.find("<"):]
else:
# something went wrong
# add rest of xml_description to description text
description_text = description_text + xml_description
xml_description = ""
es_dict["description_text"] = description_text
# claims
claims = xml_dict.get("ep-patent-document").get("claims")
if claims != None:
es_dict["claims"] = claims
try:
if claims.get("@lang") != None:
lang = claims.get("@lang")
es_dict["claims_de"] = None
es_dict["claims_en"] = None
es_dict["claims_fr"] = None
xml_claims = xml_text[(xml_text.find(">", (xml_text.find("<claims"))) + 1):(xml_text.find("</claims>"))]
claims_text = ""
counter = 1
while len(xml_claims) > 1:
xml_claims = xml_claims[(xml_claims.find(">") + 1):]
current_claim = xml_claims[:(xml_claims.find("</claim>"))]
xml_claims = xml_claims[(xml_claims.find("</claim>") + 8):]
claims_text += str(counter) + "\n"
counter += 1
current_claim = del_Brackets(current_claim)
current_claim = current_claim[12:]
depth = 0
while len(current_claim) > 0:
if current_claim.find("<claim-text>") == -1:
text = current_claim[:(current_claim.find("</claim-text>"))]
claims_text += text
current_claim = ""
else:
text = current_claim[:(current_claim.find("<claim-text>"))]
current_claim = current_claim[(current_claim.find("<claim-text>") + 12):]
for i in range(depth):
claims_text += " "
while not text.find("</claim-text>") == -1:
depth -= 1
text = text.replace("</claim-text>", "\n", 1)
claims_text += text
depth += 1
claims_text += "\n"
if lang == "de":
es_dict["claims_de"] = claims_text
elif lang == "en":
es_dict["claims_en"] = claims_text
elif lang == "fr":
es_dict["claims_fr"] = claims_text
except:
es_dict["claims_de"] = None
es_dict["claims_en"] = None
es_dict["claims_fr"] = None
xml_claims = xml_text[(xml_text.find("<claims")):(xml_text.rfind("</claims>") + 9)]
while len(xml_claims) > 0:
current_xml_claim = xml_claims[:(xml_text.find("</claims>") + 9)]
xml_claims = xml_claims[(xml_text.find("</claims>") + 9):]
current_xml_claim = current_xml_claim[:-9]
lang = current_xml_claim[:(current_xml_claim.find(">"))]
current_xml_claim = current_xml_claim[(current_xml_claim.find(">") + 1):]
lang = lang[(lang.find("lang=") + 5):]
lang = lang[:4]
lang = lang.replace('''"''', "")
claims_text = ""
counter = 1
while len(current_xml_claim) > 1:
current_xml_claim = current_xml_claim[(current_xml_claim.find(">") + 1):]
current_claim = current_xml_claim[:(current_xml_claim.find("</claim>"))]
current_xml_claim = current_xml_claim[(current_xml_claim.find("</claim>") + 8):]
claims_text += str(counter) + "\n"
counter += 1
current_claim = del_Brackets(current_claim)
current_claim = current_claim[12:]
depth = 0
while len(current_claim) > 0:
if current_claim.find("<claim-text>") == -1:
text = current_claim[:(current_claim.find("</claim-text>"))]
claims_text += text
current_claim = ""
else:
text = current_claim[:(current_claim.find("<claim-text>"))]
current_claim = current_claim[(current_claim.find("<claim-text>") + 12):]
for i in range(depth):
claims_text += " "
while not text.find("</claim-text>") == -1:
depth -= 1
text = text.replace("</claim-text>", "\n", 1)
claims_text += text
depth += 1
claims_text += "\n"
if lang == "de":
es_dict["claims_de"] = claims_text
elif lang == "en":
es_dict["claims_en"] = claims_text
elif lang == "fr":
es_dict["claims_fr"] = claims_text
write_to_json(es_dict, id, name)
# verifies that the .dtd-version from the .xml-file matches the version this script was created with
def check_dtd_version(xml_dict):
dtd_version = "1-5-1" # ep-patent-document-v1-5-1
try:
xml_version = xml_dict.get("ep-patent-document").get("@dtd-version")
xml_version = xml_version[xml_version.find("v") + 1:]
if len(xml_version) > 0:
if int(dtd_version[0]) < int(xml_version[0]):
print("The .xml-file uses a higher .dtd-version than this script does.")
if len(xml_version) > 2:
if int(dtd_version[2]) < int(xml_version[2]):
print("The .xml-file uses a higher .dtd-version than this script does.")
if len(xml_version) > 4:
if int(dtd_version[4]) < int(xml_version[4]):
print("The .xml-file uses a higher .dtd-version than this script does.")
except:
print("Error while checking the .dtd-version.")
# takes the given path
# searches for all files in current directory and all sub-directories
# filters files fpr .xml
# parses .xml-files
def load(path):
for root, dirs, files in os.walk(path):
for file in files:
with open(os.path.join(root, file), "r") as xml_file:
if xml_file.name.endswith('.xml'):
try:
xml_read = xml_file.read()
xml_dict = xmltodict.parse(xml_read)
check_dtd_version(xml_dict)
parse(xml_dict, xml_file.name, xml_read)
except:
print("Failed to parse " + xml_file.name)
# main - entry point
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit("No path given.")
load(sys.argv[1])