11import requests
22import os
33from tqdm import tqdm
4- import pickle
54import zipfile
65
76OPENHOWNET_DATA_URL = "https://thunlp.oss-cn-qingdao.aliyuncs.com/OpenHowNet/resources.zip"
7+ OPENHOWNET_RESOURCE_PATH = "~/.openhownet"
88
99
1010def get_resource (path , mode = 'r' , encoding = 'utf-8' ):
1111 '''Open the resource file.
1212 '''
1313 try :
14- package_directory = os .path .dirname (os .path .abspath (__file__ ))
1514 if 'b' in mode :
16- file = open (os .path .join (package_directory , path ), mode )
15+ file = open (os .path .join (OPENHOWNET_RESOURCE_PATH , path ), mode )
1716 else :
18- file = open (os .path .join (package_directory , path ),
17+ file = open (os .path .join (OPENHOWNET_RESOURCE_PATH , path ),
1918 mode , encoding = encoding )
2019 return file
2120 except FileNotFoundError as e :
@@ -40,8 +39,7 @@ def download_file(url, dest_file=None):
4039 if not dest_file :
4140 dest_file = os .path .basename (url )
4241
43- package_directory = os .path .dirname (os .path .abspath (__file__ ))
44- dest_path = os .path .join (package_directory , dest_file )
42+ dest_path = os .path .join (OPENHOWNET_RESOURCE_PATH , dest_file )
4543
4644 with open (dest_path , 'wb' ) as f :
4745 for x in tqdm (iterable = req .iter_content (1024 ), total = round (total_size , 2 ), unit = 'KB' , desc = dest_file ):
@@ -54,11 +52,10 @@ def download():
5452 '''Download the HowNet resource file.
5553 The HowNet resource file is openhownet_data.zip.
5654 '''
57- package_directory = os .path .dirname (os .path .abspath (__file__ ))
58- if not os .path .exists (package_directory + '/resources/' ):
59- os .mkdir (package_directory + '/resources' )
55+ if not os .path .exists (os .path .join (OPENHOWNET_RESOURCE_PATH , 'resources' )):
56+ os .mkdir (os .path .join (OPENHOWNET_RESOURCE_PATH , 'resources' ))
6057 data_zip_path = download_file (
6158 OPENHOWNET_DATA_URL , dest_file = "resources/resources.zip" )
6259 with zipfile .ZipFile (data_zip_path , 'r' ) as zip_ref :
63- zip_ref .extractall (os .path .join (package_directory , 'resources' ))
64- os .remove (data_zip_path )
60+ zip_ref .extractall (os .path .join (OPENHOWNET_RESOURCE_PATH , 'resources' ))
61+ os .remove (data_zip_path )
0 commit comments