def extract():
    """Extract melting points from patents."""
    Paragraph.parsers = [CompoundParser(), ChemicalLabelParser(), MpParser()]
    Table.parsers = []
    patents = []
    for root, dirs, files in os.walk('../examples/mp/grants'):
        for filename in files:
            if not filename.endswith('.xml'):
                continue
            path = os.path.abspath(os.path.join(root, filename))
            size = os.path.getsize(path)
            patents.append((path, filename, size))

    patents = sorted(patents, key=lambda p: p[2])

    for path, filename, size in patents:
        print(path)
        shutil.copyfile(path, '../examples/mp/used/%s' % filename)
        with open(path) as f:
            d = Document.from_file(f)
        if os.path.isfile('../examples/mp/results/%s.json' % filename):
            continue
        records = [r.serialize() for r in d.records if len(r.melting_points) == 1]
        with open('../examples/mp/results/%s.json' % filename, 'w') as fout:
            fout.write(json.dumps(records, ensure_ascii=False, indent=2).encode('utf8'))
Extract melting points from patents.