1084 lines
55 KiB
Python
1084 lines
55 KiB
Python
import xmltodict
|
|
import sys
|
|
import os
|
|
from datetime import date
|
|
import json
|
|
from io import StringIO
|
|
from lxml import etree
|
|
import zipfile
|
|
import fnmatch
|
|
import logging
|
|
import shutil
|
|
|
|
# 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", encoding='utf-8') as outfile:
|
|
json.dump(es_dict, outfile, ensure_ascii=False)
|
|
except:
|
|
print("-Failed to write "+ name)
|
|
|
|
# parses a xpath-tree
|
|
def parse_Element(tree, i, o, current_text, list_depth, additional_node, section_name):
|
|
return_text = current_text
|
|
|
|
add = ""
|
|
for a_n in range(len(additional_node)):
|
|
add += "/node()[" + str(additional_node[a_n] + 1) + "]"
|
|
|
|
if (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("figref") != -1):
|
|
comb = ""
|
|
ts = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "//text()")
|
|
for t in ts:
|
|
comb += t
|
|
return_text += comb
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("crossref") != -1):
|
|
comb = ""
|
|
ts = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "//text()")
|
|
for t in ts:
|
|
comb += t
|
|
return_text += comb
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("claim-ref") != -1):
|
|
return_text += tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "//text()")[0]
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("patcit") != -1):
|
|
pass # skip patcit
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("nplcit") != -1):
|
|
pass # skip nplcit
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("table-external-doc") != -1):
|
|
pass # skip table-external-doc
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("dl") != -1):
|
|
try:
|
|
list_elements = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "/node()")
|
|
return_text += "\n"
|
|
list_depth += 1
|
|
for p in range(len(list_elements)):
|
|
if (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "/node()[" + str(p + 1) + "]")[0]).find("dt") != -1):
|
|
dts = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]//text()")
|
|
comb = ""
|
|
for dt in dts:
|
|
comb += dt
|
|
for current_depth in range(list_depth):
|
|
return_text += " "
|
|
return_text += "- " + dt + ": "
|
|
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]")[0]).find("dd") != -1):
|
|
dds = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()")
|
|
dd_text = ""
|
|
|
|
try:
|
|
for current_dd in range(len(dds)):
|
|
if (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()[" + str(current_dd + 1) + "]")[0]).find("<Element") == 0):
|
|
# element within the current list-dd-tag
|
|
try:
|
|
additional_node.append(p)
|
|
dd_text = parse_Element(tree, i, o, dd_text, list_depth, additional_node, section_name)
|
|
del additional_node[-1]
|
|
except:
|
|
print("An Error occured while parsing a nested list within a dl.")
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()[" + str(current_dd + 1) + "]")[0]).find("<!--") == 0):
|
|
pass # skip comments
|
|
else:
|
|
# else plain text -> add to _text
|
|
dd_text += tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()[" + str(current_dd + 1) + "]")[0]
|
|
return_text += "\n"
|
|
except:
|
|
print("An Error occured while parsing the dd-tag from a dl.")
|
|
return_text += dd_text + "\n"
|
|
list_depth -= 1
|
|
except:
|
|
print("An Error occured while parsing a dl.")
|
|
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("ol") != -1):
|
|
try:
|
|
list_elements = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "/node()")
|
|
return_text += "\n"
|
|
list_depth += 1
|
|
for p in range(len(list_elements)):
|
|
if (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "/node()[" + str(p + 1) + "]")[0]).find("li") != -1):
|
|
lis = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()")
|
|
for current_depth in range(list_depth):
|
|
return_text += " "
|
|
return_text += "- "
|
|
|
|
try:
|
|
for current_li in range(len(lis)):
|
|
if (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()[" + str(current_li + 1) + "]")[0]).find("<Element") == 0):
|
|
# element within the current list-tag
|
|
try:
|
|
additional_node.append(p)
|
|
return_text = parse_Element(tree, i, o, return_text, list_depth, additional_node, section_name)
|
|
del additional_node[-1]
|
|
except:
|
|
print("An Error occured while parsing a nested list within a ol.")
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()[" + str(current_li + 1) + "]")[0]).find("<!--") == 0):
|
|
pass # skip comments
|
|
else:
|
|
# else plain text -> add to _text
|
|
return_text += tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()[" + str(current_li + 1) + "]")[0]
|
|
return_text += "\n"
|
|
except:
|
|
print("An Error occured while parsing the li-tag from a ol.")
|
|
list_depth -= 1
|
|
except:
|
|
print("An Error occured while parsing a ol.")
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("ul") != -1):
|
|
try:
|
|
list_elements = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "/node()")
|
|
return_text += "\n"
|
|
list_depth += 1
|
|
for p in range(len(list_elements)):
|
|
if (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "/node()[" + str(p + 1) + "]")[0]).find("li") != -1):
|
|
lis = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()")
|
|
for current_depth in range(list_depth):
|
|
return_text += " "
|
|
return_text += "- "
|
|
|
|
try:
|
|
for current_li in range(len(lis)):
|
|
if (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()[" + str(current_li + 1) + "]")[0]).find("<Element") == 0):
|
|
# element within the current list-tag
|
|
try:
|
|
additional_node.append(p)
|
|
return_text = parse_Element(tree, i, o, return_text, list_depth, additional_node, section_name)
|
|
del additional_node[-1]
|
|
except:
|
|
print("An Error occured while parsing a nested list within a ul.")
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()[" + str(current_li + 1) + "]")[0]).find("<!--") == 0):
|
|
pass # skip comments
|
|
else:
|
|
# else plain text -> add to _text
|
|
return_text += tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()" + add + "[" + str(p + 1) + "]/node()[" + str(current_li + 1) + "]")[0]
|
|
return_text += "\n"
|
|
except:
|
|
print("An Error occured while parsing the li-tag from a ul.")
|
|
list_depth -= 1
|
|
except:
|
|
print("An Error occured while parsing a ul.")
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("img") != -1):
|
|
pass # skip img
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("chemistry") != -1):
|
|
# try to get chemistry/formula-text and add it to the text
|
|
# if this text does not exsist -> skip (pass)
|
|
# if an error occures -> skip (pass)
|
|
try:
|
|
chemistry_texts = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "/formula-text//text()")
|
|
if len(chemistry_texts) > 0:
|
|
ct = ""
|
|
for chemistry_text in chemistry_texts:
|
|
ct += math_text
|
|
return_text += ct
|
|
except:
|
|
pass
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("maths") != -1):
|
|
# try to get math/formula-text and add it to the text
|
|
# if this text does not exsist -> skip (pass)
|
|
# if an error occures -> skip (pass)
|
|
try:
|
|
math_texts = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "/formula-text//text()")
|
|
if len(math_texts) > 0:
|
|
mt = ""
|
|
for math_text in math_texts:
|
|
mt += math_text
|
|
return_text += mt
|
|
except:
|
|
pass
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("tables") != -1):
|
|
pass # skip tables
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("bio-deposit") != -1):
|
|
pass # skip bio-deposit
|
|
elif (str(tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add)[0]).find("claim-text") != -1):
|
|
ct = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "/node()")
|
|
|
|
for p in range(len(ct)):
|
|
claim_text = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "/node()[" + str(p + 1) + "]")
|
|
|
|
if str(claim_text[0]).find("<Element") == 0:
|
|
additional_node.append(p)
|
|
return_text = parse_Element(tree, i, o, return_text, 0, additional_node, section_name)
|
|
del additional_node[-1]
|
|
elif str(claim_text[0]).find("<!--") == 0:
|
|
pass # skip comments
|
|
else:
|
|
return_text += str(claim_text[0])
|
|
else:
|
|
p_texts_tags = tree.xpath("/ep-patent-document/" + section_name + "/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]" + add + "//text()")
|
|
comb = ""
|
|
for p in range(len(p_texts_tags)):
|
|
comb += p_texts_tags[p]
|
|
if not comb == "":
|
|
return_text += comb
|
|
|
|
return return_text
|
|
|
|
# parses the xml-file into elasticsearch
|
|
# takes out relevant information
|
|
def parse(xml_dict, filename, tree, wanted_ipcs):
|
|
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 " + filename + ". 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
|
|
# ipc
|
|
if sdobi.get("B500").get("B510") != None:
|
|
# es_dict["ipc"] = [sdobi.get("B500").get("B510")]
|
|
# es_dict["ipc_text"] = sdobi.get("B500").get("B510")
|
|
classis = []
|
|
if sdobi.get("B500").get("B510").get("B511") != None:
|
|
try:
|
|
if sdobi.get("B500").get("B510").get("B511") != None:
|
|
classis.append(sdobi.get("B500").get("B510").get("B511"))
|
|
except:
|
|
for i in range(len(sdobi.get("B500").get("B510").get("B511"))):
|
|
classis.append(sdobi.get("B500").get("B510").get("B511")[i])
|
|
if sdobi.get("B500").get("B510").get("B512") != None:
|
|
b512 = sdobi.get("B500").get("B510").get("B512")
|
|
if isinstance(b512, str):
|
|
classis.append(b512)
|
|
elif isinstance(b512, list):
|
|
for i in range(len(b512)):
|
|
classis.append(b512[i])
|
|
|
|
es_dict["ipc"] = classis
|
|
text_combi = ""
|
|
for text in classis:
|
|
if text is not None:
|
|
text_combi += text + " - "
|
|
if text_combi != "":
|
|
text_combi = text_combi[:-3]
|
|
|
|
es_dict["ipc_text"] = text_combi
|
|
|
|
# patent classification tags from 2006 and later
|
|
# its one string that needs to be splittet into the classification parts
|
|
# ipc
|
|
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
|
|
|
|
text_combi = ""
|
|
for text in texte:
|
|
text_combi += text + " - "
|
|
text_combi = text_combi[:-3]
|
|
|
|
es_dict["ipc_text"] = text_combi
|
|
|
|
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
|
|
|
|
# check for wanted ipcs
|
|
if wanted_ipcs != []:
|
|
# check if filtered ipc is a wanted_ipc
|
|
ipcs_unadjusted = es_dict["ipc"]
|
|
# adjust ipcs
|
|
adjusted_ipcs = []
|
|
for iu in ipcs_unadjusted:
|
|
ai = iu[0: iu.find(" ")]
|
|
ai = ai.replace(" ", "")
|
|
adjusted_ipcs.append(ai)
|
|
# check if adjusted ipc is in wanted_ipcs
|
|
is_inside = False
|
|
for ai in adjusted_ipcs:
|
|
if ai in wanted_ipcs:
|
|
is_inside = True
|
|
break
|
|
# if ipcs are not wanted -> stop parsing and go to next xml-file
|
|
if not is_inside:
|
|
return
|
|
|
|
# patent classification
|
|
# cpc
|
|
if sdobi.get("B500").get("B520EP") != None:
|
|
if sdobi.get("B500").get("B520EP").get("classification-cpc") != None:
|
|
cp = sdobi.get("B500").get("B520EP").get("classification-cpc")
|
|
|
|
texte = []
|
|
|
|
try:
|
|
if cp.get("text") != None:
|
|
texte.append(ip.get("text"))
|
|
except:
|
|
for i in range(len(cp)):
|
|
texte.append(cp[i].get("text"))
|
|
|
|
es_dict["cpc"] = texte
|
|
|
|
text_combi = ""
|
|
for text in texte:
|
|
text_combi += text + " - "
|
|
text_combi = text_combi[:-3]
|
|
|
|
es_dict["cpc_text"] = text_combi
|
|
|
|
# title in en, de and fr
|
|
if sdobi.get("B500").get("B540") != None:
|
|
l = len(sdobi.get("B500").get("B540").get("B541"))
|
|
if l > 0:
|
|
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")[0] == "en":
|
|
es_dict["title_en"] = sdobi.get("B500").get("B540").get("B542")[0]
|
|
elif sdobi.get("B500").get("B540").get("B541")[0] == "fr":
|
|
es_dict["title_fr"] = sdobi.get("B500").get("B540").get("B542")[0]
|
|
if l > 1:
|
|
if 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")[1] == "en":
|
|
es_dict["title_en"] = sdobi.get("B500").get("B540").get("B542")[1]
|
|
elif sdobi.get("B500").get("B540").get("B541")[1] == "fr":
|
|
es_dict["title_fr"] = sdobi.get("B500").get("B540").get("B542")[1]
|
|
if l > 2:
|
|
if sdobi.get("B500").get("B540").get("B541")[2] == "de":
|
|
es_dict["title_de"] = sdobi.get("B500").get("B540").get("B542")[2]
|
|
elif sdobi.get("B500").get("B540").get("B541")[2] == "en":
|
|
es_dict["title_en"] = sdobi.get("B500").get("B540").get("B542")[2]
|
|
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")
|
|
|
|
if name == None:
|
|
name = ""
|
|
if street == None:
|
|
street = ""
|
|
if city == None:
|
|
city = ""
|
|
if country == None:
|
|
country = ""
|
|
|
|
applicant = name + ", " + street + ", " + city + ", " + country
|
|
es_dict["applicants"] = [applicant]
|
|
es_dict["applicants_name"] = [name]
|
|
es_dict["applicants_name_text"] = name
|
|
es_dict["applicants_adr"] = ["" + street + ", " + city + ", " + country]
|
|
else:
|
|
applicant = sdobi.get("B700").get("B710").get("B711").get("snm")
|
|
es_dict["applicants"] = [applicant]
|
|
es_dict["applicants_name"] = [applicant]
|
|
es_dict["applicants_name_text"] = applicant
|
|
es_dict["applicants_adr"] = [None]
|
|
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]
|
|
es_dict["applicants_name"] = [applicant]
|
|
es_dict["applicants_name_text"] = applicant
|
|
es_dict["applicants_adr"] = [None]
|
|
except:
|
|
applis = []
|
|
names = []
|
|
adrs = []
|
|
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")
|
|
|
|
if name == None:
|
|
name = ""
|
|
if street == None:
|
|
street = ""
|
|
if city == None:
|
|
city = ""
|
|
if country == None:
|
|
country = ""
|
|
|
|
applis.append(name + ", " + street + ", " + city + ", " + country)
|
|
names.append(name)
|
|
adrs.append("" + street + ", " + city + ", " + country)
|
|
else:
|
|
applis.append(appli.get("snm"))
|
|
names.append(appli.get("snm"))
|
|
adrs.append(None)
|
|
es_dict["applicants"] = applis
|
|
es_dict["applicants_name"] = names
|
|
|
|
t = ""
|
|
for name in names:
|
|
if name == None:
|
|
continue
|
|
t += name + " - "
|
|
t = t[:-3]
|
|
es_dict["applicants_name_text"] = t
|
|
|
|
es_dict["applicants_adr"] = adrs
|
|
# 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")
|
|
|
|
if name == None:
|
|
name = ""
|
|
if street == None:
|
|
street = ""
|
|
if city == None:
|
|
city = ""
|
|
if country == None:
|
|
country = ""
|
|
|
|
inventor = name + ", " + street + ", " + city + ", " + country
|
|
es_dict["inventors"] = [inventor]
|
|
es_dict["inventors_name"] = [name]
|
|
es_dict["inventors_name_text"] = name
|
|
es_dict["inventors_adr"] = ["" + street + ", " + city + ", " + country]
|
|
else:
|
|
inventor = sdobi.get("B700").get("B720").get("B721").get("snm")
|
|
es_dict["inventors"] = [inventor]
|
|
es_dict["inventors_name"] = [inventor]
|
|
es_dict["inventors_name_text"] = inventor
|
|
es_dict["inventors_adr"] = [None]
|
|
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]
|
|
es_dict["inventors_name"] = [inventor]
|
|
es_dict["inventors_name_text"] = inventor
|
|
es_dict["inventors_adr"] = [None]
|
|
except:
|
|
invs = []
|
|
names = []
|
|
adrs = []
|
|
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")
|
|
|
|
if name == None:
|
|
name = ""
|
|
if street == None:
|
|
street = ""
|
|
if city == None:
|
|
city = ""
|
|
if country == None:
|
|
country = ""
|
|
|
|
invs.append(name + ", " + street + ", " + city + ", " + country)
|
|
names.append(name)
|
|
adrs.append("" + street + ", " + city + ", " + country)
|
|
else:
|
|
invs.append(inv.get("snm"))
|
|
names.append(inv.get("snm"))
|
|
adrs.append(None)
|
|
es_dict["inventors"] = invs
|
|
es_dict["inventors_name"] = names
|
|
|
|
t = ""
|
|
for name in names:
|
|
if name == None:
|
|
continue
|
|
t += name + " - "
|
|
t = t[:-3]
|
|
es_dict["inventors_name_text"] = t
|
|
|
|
es_dict["inventors_adr"] = adrs
|
|
# 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")
|
|
|
|
if name == None:
|
|
name = ""
|
|
if street == None:
|
|
street = ""
|
|
if city == None:
|
|
city = ""
|
|
if country == None:
|
|
country = ""
|
|
|
|
rep = name + ", " + street + ", " + city + ", " + country
|
|
es_dict["representatives"] = [rep]
|
|
es_dict["representatives_name"] = [name]
|
|
es_dict["representatives_name_text"] = name
|
|
es_dict["representatives_adr"] = ["" + street + ", " + city + ", " + country]
|
|
else:
|
|
rep = sdobi.get("B700").get("B740").get("B741").get("snm")
|
|
es_dict["representatives"] = [rep]
|
|
es_dict["representatives_name"] = [rep]
|
|
es_dict["representatives_name_text"] = rep
|
|
es_dict["representatives_adr"] = [None]
|
|
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]
|
|
es_dict["representatives_name"] = [rep]
|
|
es_dict["representatives_name_text"] = rep
|
|
es_dict["representatives_adr"] = [None]
|
|
except:
|
|
reps = []
|
|
names = []
|
|
adrs = []
|
|
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")
|
|
|
|
if name == None:
|
|
name = ""
|
|
if street == None:
|
|
street = ""
|
|
if city == None:
|
|
city = ""
|
|
if country == None:
|
|
country = ""
|
|
|
|
reps.append(name + ", " + street + ", " + city + ", " + country)
|
|
names.append(name)
|
|
adrs.append("" + street + ", " + city + ", " + country)
|
|
else:
|
|
reps.append(rep.get("snm"))
|
|
names.append(rep.get("snm"))
|
|
adrs.append(None)
|
|
es_dict["representatives"] = reps
|
|
es_dict["representatives_name"] = names
|
|
|
|
t = ""
|
|
for name in names:
|
|
if name == None:
|
|
continue
|
|
t += name + " - "
|
|
t = t[:-3]
|
|
es_dict["representatives_name_text"] = t
|
|
|
|
es_dict["representatives_adr"] = adrs
|
|
|
|
# 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
|
|
|
|
t = ""
|
|
for state in states:
|
|
if state == None:
|
|
continue
|
|
t += state + " - "
|
|
t = t[:-3]
|
|
es_dict["states_text"] = t
|
|
|
|
# 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
|
|
|
|
t = ""
|
|
for extended_state in extended_states:
|
|
if extended_state == None:
|
|
continue
|
|
t += extended_state + " - "
|
|
t = t[:-3]
|
|
es_dict["extended_states_text"] = t
|
|
|
|
# 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)):
|
|
if p[i].get("#text") is not None:
|
|
text = text + p[i].get("#text") + " "
|
|
es_dict["abstract"] = text
|
|
|
|
"""
|
|
the following parts description and claims getting parsed with xpath (lxml) instead of a dictionary
|
|
"""
|
|
|
|
# description
|
|
description = xml_dict.get("ep-patent-document").get("description")
|
|
if description != None:
|
|
es_dict["description_lang"] = description.get("@lang")
|
|
# first just header as an array
|
|
if description.get("heading") != None:
|
|
headers = []
|
|
heading = tree.xpath("/ep-patent-document/description/heading/node()")
|
|
|
|
for i in range(len(heading)):
|
|
header = tree.xpath("/ep-patent-document/description/heading[" + str(i + 1) + "]//text()")
|
|
comb = ""
|
|
|
|
for o in range(len(header)):
|
|
comb += header[o]
|
|
|
|
headers.append(comb)
|
|
|
|
es_dict["description_header"] = headers
|
|
# second all paragraphs as an array (lists, tables and so on are just included as text but not sorted as a list for example)
|
|
if description.get("p") != None:
|
|
paragraphs = []
|
|
p = tree.xpath("/ep-patent-document/description/p/node()")
|
|
|
|
for i in range(len(p)):
|
|
paragraph = tree.xpath("/ep-patent-document/description/p[" + str(i + 1) + "]//text()")
|
|
comb = ""
|
|
|
|
for o in range(len(paragraph)):
|
|
comb += paragraph[o]
|
|
|
|
if not comb == "":
|
|
paragraphs.append(comb)
|
|
|
|
es_dict["description_paragraph"] = paragraphs
|
|
|
|
# third the sorted description
|
|
"""
|
|
header 1
|
|
paragraph 1
|
|
header 2
|
|
paragraph 2
|
|
header 3
|
|
paragraph 4
|
|
paragraph 5
|
|
and so on
|
|
|
|
if a list appears i a paragraph it gets sorted into the text
|
|
example:
|
|
text
|
|
- list point 1
|
|
- list point 2
|
|
text
|
|
"""
|
|
# filter out xml_description
|
|
description_text = ""
|
|
nodes = tree.xpath("/ep-patent-document/description/node()")
|
|
|
|
for i in range(len(nodes)):
|
|
# check if node is heading
|
|
if (str(tree.xpath("/ep-patent-document/description/node()[" + str(i + 1) + "]")[0]).find("heading") != -1):
|
|
header = tree.xpath("/ep-patent-document/description/node()[" + str(i + 1) + "]//text()")
|
|
comb = ""
|
|
|
|
for o in range(len(header)):
|
|
comb += header[o]
|
|
description_text += "\n\n" + comb + "\n\n"
|
|
# check if node is p (else skip this node)
|
|
elif (str(tree.xpath("/ep-patent-document/description/node()[" + str(i + 1) + "]")[0]).find("p") != -1):
|
|
p_nodes = tree.xpath("/ep-patent-document/description/node()[" + str(i + 1) + "]/node()")
|
|
|
|
for o in range(len(p_nodes)):
|
|
if (str(tree.xpath("/ep-patent-document/description/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]")[0]).find("<Element") == 0):
|
|
# element within the p-tag
|
|
description_text = parse_Element(tree, i, o, description_text, 0, [], "description")
|
|
elif (str(tree.xpath("/ep-patent-document/description/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]")[0]).find("<!--") == 0):
|
|
pass # skip comments
|
|
else:
|
|
# else plain text -> add to description_text
|
|
description_text += tree.xpath("/ep-patent-document/description/node()[" + str(i + 1) + "]/node()[" + str(o + 1) + "]")[0]
|
|
description_text += "\n"
|
|
es_dict["description_text"] = description_text
|
|
|
|
# claims
|
|
claims = xml_dict.get("ep-patent-document").get("claims")
|
|
if claims != None:
|
|
es_dict["claims_de"] = None
|
|
es_dict["claims_en"] = None
|
|
es_dict["claims_fr"] = None
|
|
|
|
claims_all = tree.xpath("/ep-patent-document/claims")
|
|
|
|
for i in range(len(claims_all)):
|
|
lang = tree.xpath("/ep-patent-document/claims[" + str(i + 1) + "]/@lang")[0]
|
|
|
|
claims = tree.xpath("/ep-patent-document/claims[" + str(i + 1) + "]/node()")
|
|
|
|
current_text = ""
|
|
counter = 0
|
|
|
|
for o in range(len(claims)):
|
|
claim = tree.xpath("/ep-patent-document/claims[" + str(i + 1) + "]/node()[" + str(o + 1) + "]")
|
|
|
|
if (claim[0] != "\n"):
|
|
counter += 1
|
|
current_text += "\n\n" + str(counter) + "\n"
|
|
|
|
claim_inner = tree.xpath("/ep-patent-document/claims[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()")
|
|
|
|
for p in range(len(claim_inner)):
|
|
claim_text_outer = tree.xpath("/ep-patent-document/claims[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()[" + str(p + 1) + "]")
|
|
|
|
if claim_text_outer[0] != "\n":
|
|
claim_text_inner = tree.xpath("/ep-patent-document/claims[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()[" + str(p + 1) + "]/node()")
|
|
|
|
for k in range(len(claim_text_inner)):
|
|
claim_text = tree.xpath("/ep-patent-document/claims[" + str(i + 1) + "]/node()[" + str(o + 1) + "]/node()[" + str(p + 1) + "]/node()[" + str(k + 1) + "]")
|
|
|
|
if str(claim_text[0]).find("<Element") == 0:
|
|
additional_node = []
|
|
additional_node.append(k)
|
|
current_text = parse_Element(tree, o, p, current_text, 0, additional_node,"claims[" + str(i + 1) + "]")
|
|
del additional_node[-1]
|
|
elif str(claim_text[0]).find("<!--") == 0:
|
|
pass # skip comments
|
|
else:
|
|
current_text += str(claim_text[0])
|
|
|
|
if lang == "de":
|
|
es_dict["claims_de"] = current_text
|
|
elif lang == "en":
|
|
es_dict["claims_en"] = current_text
|
|
elif lang == "fr":
|
|
es_dict["claims_fr"] = current_text
|
|
|
|
###
|
|
# Search report
|
|
if xml_dict.get("ep-patent-document").get("search-report-data") != None:
|
|
srep = xml_dict.get("ep-patent-document").get("search-report-data")
|
|
|
|
# srep could be a list here
|
|
# get the right srep - element of them both
|
|
# get srep part with the wanted informations not the doc-pages
|
|
if type(srep) is list:
|
|
if srep[0].get("doc-page") != None:
|
|
srep = srep[1]
|
|
else:
|
|
srep = srep[0]
|
|
|
|
# srep is never a list now
|
|
# check if srep has the wanted informations (if it was a list before always has, but if it wasn't a list -> needs to be checked)
|
|
if srep.get("doc-page") != None:
|
|
pass
|
|
else:
|
|
# get informations
|
|
es_dict["srep_lang"] = srep.get("@lang")
|
|
es_dict["srep_date_produced"] = srep.get("@date-produced")
|
|
es_dict["srep_type"] = srep.get("@srep-type")
|
|
es_dict["srep_status"] = srep.get("@status")
|
|
es_dict["srep_office"] = srep.get("@srep-office")
|
|
|
|
# srep-info
|
|
if srep.get("srep-info") != None:
|
|
srep_info = srep.get("srep-info")
|
|
|
|
if type(srep_info) is list:
|
|
if srep_info[0].get("doc-page") != None:
|
|
srep_info = srep_info[1]
|
|
else:
|
|
srep_info = srep_info[0]
|
|
|
|
if srep_info.get("doc-page") != None:
|
|
pass
|
|
else:
|
|
es_dict["srep_file_reference_id"] = srep_info.get("file-reference-id")
|
|
|
|
if srep_info.get("srep-established") != None:
|
|
es_dict["srep_established"] = srep_info.get("srep-established").get("@srep-established")
|
|
|
|
if srep_info.get("srep-abstract") != None:
|
|
es_dict["srep_abs_approval"] = srep_info.get("srep-abstract").get("@abs-approval")
|
|
if srep_info.get("srep-abstract").get("abstract") != None:
|
|
p = srep_info.get("srep-abstract").get("abstract").get("p")
|
|
try:
|
|
es_dict["srep_abstract"] = p.get("#text")
|
|
except:
|
|
text = ""
|
|
for i in range(len(p)):
|
|
text = text + p[i].get("#text") + " "
|
|
es_dict["srep_abstract"] = text
|
|
|
|
if srep_info.get("srep-invention-title") != None:
|
|
es_dict["srep_invention_title"] = srep_info.get("srep-invention-title").get("invention-title")
|
|
es_dict["srep_invention_title_approval"] = srep_info.get("srep-invention-title").get("@title-approval")
|
|
|
|
if srep_info.get("srep-unity-of-invention") != None:
|
|
if srep_info.get("srep-unity-of-invention").get("p") != None:
|
|
unity = srep_info.get("srep-unity-of-invention").get("p")
|
|
unity_list = []
|
|
|
|
if type(unity) is list:
|
|
for element in unity:
|
|
p = element.get("#text")
|
|
p = p.replace("<br/>", "\n")
|
|
unity_list.append(p)
|
|
else:
|
|
p = unity.get("#text")
|
|
p = p.replace("<br/>", "\n")
|
|
unity_list.append(p)
|
|
es_dict["srep_unity_of_invention"] = unity_list
|
|
|
|
if srep_info.get("srep-unity-of-invention").get("srep-search-fees") != None:
|
|
if srep_info.get("srep-unity-of-invention").get("srep-search-fees").get("srep-fee-1") != None:
|
|
es_dict["srep_unity_of_invention_fee_1"] = "yes"
|
|
else:
|
|
es_dict["srep_unity_of_invention_fee_1"] = "no"
|
|
|
|
if srep_info.get("srep-unity-of-invention").get("srep-search-fees").get("srep-fee-2") != None:
|
|
es_dict["srep_unity_of_invention_fee_2"] = "yes"
|
|
else:
|
|
es_dict["srep_unity_of_invention_fee_2"] = "no"
|
|
|
|
if srep_info.get("srep-unity-of-invention").get("srep-search-fees").get("srep-fee-3") != None:
|
|
nums = srep_info.get("srep-unity-of-invention").get("srep-search-fees").get("srep-fee-3").get("claim-num")
|
|
nums_list = []
|
|
if type(nums) is list:
|
|
for element in nums:
|
|
nums_list.append(element)
|
|
else:
|
|
nums_list.append(nums)
|
|
es_dict["srep_unity_of_invention_fee_3"] = nums_list
|
|
else:
|
|
es_dict["srep_unity_of_invention_fee_3"] = "no"
|
|
|
|
if srep_info.get("srep-unity-of-invention").get("srep-search-fees").get("srep-fee-4") != None:
|
|
nums = srep_info.get("srep-unity-of-invention").get("srep-search-fees").get("srep-fee-4").get("claim-num")
|
|
nums_list = []
|
|
if type(nums) is list:
|
|
for element in nums:
|
|
nums_list.append(element)
|
|
else:
|
|
nums_list.append(nums)
|
|
es_dict["srep_unity_of_invention_fee_4"] = nums_list
|
|
else:
|
|
es_dict["srep_unity_of_invention_fee_4"] = "no"
|
|
|
|
if srep_info.get("srep-protest-fees") != None:
|
|
es_dict["srep_protest_fees"] = srep_info.get("srep-protest-fees").get("@srep-protest")
|
|
|
|
write_to_json(es_dict, id, filename)
|
|
|
|
|
|
# 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.")
|
|
|
|
# reads and parses the xml files in the given directory and subdirectories
|
|
# (zips get unzipped -> parsed -> and unzipped file gets deleted again [zip-file remains untouched])
|
|
def load_parse(path, logger, counter, passes, failes, isMainParse, wanted_ipcs):
|
|
if isMainParse:
|
|
print('********PARSING: XMLs********')
|
|
for root, dirs, files in os.walk(path):
|
|
for file in files:
|
|
if file.endswith('.xml') or file.endswith('.zip'):
|
|
if file.endswith('.xml') and "TOC" not in file and "index" not in file and "SL000001" not in file and "INDEX" not in file:
|
|
with open(os.path.join(root, file), "r", encoding='utf-8') as xml_file:
|
|
if xml_file.name.endswith('.xml') and "TOC" not in xml_file.name and "index" not in xml_file.name and "SL000001" not in xml_file.name and "INDEX" not in xml_file.name:
|
|
print("Parsing file: " + xml_file.name)
|
|
counter+=1
|
|
try:
|
|
xml_read = xml_file.read()
|
|
xml_dict = xmltodict.parse(xml_read)
|
|
check_dtd_version(xml_dict)
|
|
|
|
tree = etree.parse(os.path.join(root, file))
|
|
|
|
parse(xml_dict, xml_file.name, tree, wanted_ipcs)
|
|
passes += 1
|
|
except Exception as e:
|
|
print("Failed to parse " + xml_file.name)
|
|
print(e)
|
|
|
|
logger.error("Failed to parse " + xml_file.name)
|
|
logger.error(e)
|
|
|
|
# copy failed file into new directory
|
|
path = "failed_xmls"
|
|
if not os.path.exists(path):
|
|
os.mkdir(path)
|
|
|
|
try:
|
|
shutil.copyfile((os.path.join(root, file)), (path + "/" + file))
|
|
except:
|
|
print("Failed to copy file " + xml_file.name + "into failed_xmls - folder after the parsing failed.")
|
|
logger.error("Failed to copy file " + xml_file.name + "into failed_xmls - folder after the parsing failed.")
|
|
|
|
failes +=1
|
|
elif file.endswith('.zip'):
|
|
zip_path = os.path.join(root, file)[:-4]
|
|
try:
|
|
print(f'Extracting zipfile: {file} to {zip_path}')
|
|
zipfile.ZipFile(os.path.join(root, file)).extractall(os.path.join(root, os.path.splitext(file)[0]))
|
|
counter, passes, failes = load_parse(zip_path, logger, counter, passes,failes, False, wanted_ipcs)
|
|
shutil.rmtree(zip_path)
|
|
except zipfile.BadZipfile:
|
|
print(f'Cannot extract {file}: Not a valid zipfile (BadZipfile Exception)')
|
|
logger.error(f'Cannot extract {file}: Not a valid zipfile (BadZipfile Exception)')
|
|
if isMainParse:
|
|
print(f"xml total count {counter}, total passes {round(passes/counter,2)}%, total failes {round(failes/counter,2)}%")
|
|
logger.info(f"xml total count {counter}, total passes {round(passes/counter,2)}%, total failes {round(failes/counter,2)}%")
|
|
else:
|
|
return counter, passes, failes
|
|
|
|
# main
|
|
if __name__ == '__main__':
|
|
logging.basicConfig(filename='app.log', level=logging.DEBUG, format='%(asctime)s %(levelname)s %(name)s %(message)s')
|
|
logger=logging.getLogger(__name__)
|
|
try:
|
|
if len(sys.argv) != 2 and len(sys.argv) != 3:
|
|
sys.exit("No path given.")
|
|
|
|
wanted_ipcs = []
|
|
if len(sys.argv) == 3:
|
|
wanted_ipcs = sys.argv[2].split(",")
|
|
|
|
load_parse(sys.argv[1], logging.getLogger("load_parse"), 0, 0, 0, True, wanted_ipcs)
|
|
except Exception as err:
|
|
logger.error(err)
|
|
|
|
"""
|
|
start normal:
|
|
python xml_to_json.py <path-to-xmls>
|
|
|
|
start with wanted ipcs
|
|
python xml_to_json.py <path-to-xmls> <wanted ipc 1>,<wanted ipc 2>,<wanted ipc 3>
|
|
|
|
-> list all wanted ipcs with a comma separated
|
|
-> ipcs-format: E06B3/54
|
|
-> whole ipc: E06B 3/54 19680901AFI20140203BHEP -> wanted ipc: E06B3/54
|
|
""" |