File size: 2,578 Bytes
2d741b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""Check local RDRSegmenter resources and obtain a portable Windows JRE if needed."""
from pathlib import Path
import os,sys,json,urllib.request,hashlib,zipfile,platform,shutil
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT))
from runtime_setup import activate_java

def download(url,path):
    path.parent.mkdir(parents=True,exist_ok=True)
    request=urllib.request.Request(url,headers={'User-Agent':'ToxicScope-setup/1.0'})
    with urllib.request.urlopen(request,timeout=120) as r,path.open('wb') as out:
        shutil.copyfileobj(r,out)

def main():
    base='https://raw.githubusercontent.com/vncorenlp/VnCoreNLP/master/'
    for name in ['VnCoreNLP-1.2.jar','models/wordsegmenter/vi-vocab','models/wordsegmenter/wordsegmenter.rdr','LICENSE.md']:
        path=ROOT/'resources/vncorenlp'/name
        if not path.exists():
            print('Downloading VnCoreNLP:',name,flush=True);download(base+name,path)
    if not activate_java():
        if platform.system()!='Windows':
            raise RuntimeError('Install Java 17 (JRE/JDK), then run this script again. https://adoptium.net/installation')
        if platform.machine().lower() not in ('amd64','x86_64'):
            raise RuntimeError('This package targets Windows x64. Install the matching Java JRE manually.')
        print('Downloading a portable Temurin 17 JRE from Adoptium (no admin required)...',flush=True)
        url='https://api.adoptium.net/v3/assets/latest/17/hotspot?architecture=x64&image_type=jre&os=windows&vendor=eclipse'
        with urllib.request.urlopen(url,timeout=90) as response:assets=json.load(response)
        package=assets[0]['binary']['package'];target=ROOT/'runtime/java.zip'
        download(package['link'],target)
        if hashlib.sha256(target.read_bytes()).hexdigest()!=package['checksum']:
            target.unlink();raise RuntimeError('Java checksum mismatch. Retry the setup.')
        dest=(ROOT/'runtime/java').resolve();dest.mkdir(parents=True,exist_ok=True)
        with zipfile.ZipFile(target) as archive:
            for name in archive.namelist():
                if not (dest/name).resolve().is_relative_to(dest):raise RuntimeError('Invalid Java archive path.')
            archive.extractall(dest)
        target.unlink()
        if not activate_java():raise RuntimeError('Cannot locate portable Java after extracting.')
    from preprocessing import phobert_text
    print('RDRSegmenter:',phobert_text('Kiểm tra hệ thống ToxicScope hoạt động tốt.'))
    print('Resources ready.',flush=True)

if __name__=='__main__':main()