-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #5: added pre-trained codec for XML
- Loading branch information
Showing
8 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import hashlib | ||
import logging | ||
from collections import Counter | ||
|
||
from dahuffman import HuffmanCodec | ||
from train.train_utils import download, CODECS | ||
|
||
_log = logging.getLogger() | ||
|
||
|
||
def main(): | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
# XML data sets from https://www.data.gov/ | ||
urls = [ | ||
"https://data.cityofnewyork.us/api/views/kku6-nxdu/rows.xml", | ||
"https://data.cdc.gov/api/views/bi63-dtpu/rows.xml", | ||
"https://data.cdc.gov/api/views/cjae-szjv/rows.xml", | ||
"https://data.cityofnewyork.us/api/views/25th-nujf/rows.xml", | ||
"https://data.ct.gov/api/views/kbxi-4ia7/rows.xml", | ||
"https://data.cityofchicago.org/api/views/pfsx-4n4m/rows.xml", | ||
"https://data.cdc.gov/api/views/6vp6-wxuq/rows.xml", | ||
"https://www.sba.gov/sites/default/files/data.xml", | ||
"https://data.cdc.gov/api/views/e6fc-ccez/rows.xml", | ||
"https://data.cityofnewyork.us/api/views/jb7j-dtam/rows.xml", | ||
"https://data.cityofnewyork.us/api/views/zt9s-n5aj/rows.xml", | ||
"https://gisdata.nd.gov/Metadata/ISO/xml/metadata_Roads_MileMarkers.xml", | ||
"https://data.cityofchicago.org/api/views/kn9c-c2s2/rows.xml", | ||
"https://data.cityofnewyork.us/api/views/5t4n-d72c/rows.xml", | ||
"https://data.cdc.gov/api/views/6rkc-nb2q/rows.xml", | ||
"https://gisdata.nd.gov/Metadata/ISO/xml/metadata_Airports.xml", | ||
"https://data.sfgov.org/api/views/j4sj-j2nf/rows.xml", | ||
"https://data.kingcounty.gov/api/views/gmen-63jm/rows.xml", | ||
"https://data.mo.gov/api/views/vpge-tj3s/rows.xml", | ||
] | ||
|
||
_log.info('Building frequency tables') | ||
frequencies = Counter() | ||
for url in urls: | ||
path = download(url, 'xml-data/' + hashlib.md5(url.encode('utf-8')).hexdigest() + '.xml') | ||
with path.open('r') as f: | ||
# Only take first N bytes. | ||
# Large files probably have a lot of structural repetition, which skews the frequencies | ||
raw = f.read(100000) | ||
frequencies.update(raw) | ||
|
||
# TODO add more metadata | ||
_log.info(f'Frequencies raw {len(frequencies)}: {frequencies}') | ||
codec = HuffmanCodec.from_frequencies(frequencies) | ||
codec.save(CODECS / "xml.pickle", metadata={"frequencies": frequencies}) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |