0
votes

I'm trying to use WordNet within PyScript but I can't seem to properly load Wordnet.

At first I tried:

<py-env>
   - nltk
</py-env>
<py-script>
   import nltk
   from nltk.corpus import wordnet as wn
<py-script> 

This gave me a LookupError(resource_not_found), along with the message

Please use the NLTK Downloader to obtain the resource: [31m>>> import nltk >>> nltk.download('wordnet')

I then tried:

<py-script>
   import nltk
   nltk.download('wordnet')
   from nltk.corpus import wordnet as wn
<py-script>

which gave me this message in the console:

writing to py-3f0adca1-a38a-4161-c36f-7e6548260aa5 [nltk_data] Error loading wordnet: <urlopen error unknown url type: [nltk_data] https> true

I looked at the responses here: Pyodide filesystem for NLTK resources : missing files and tried to replicate their code

    from js import fetch
    from pathlib import Path
    import os, sys, io, zipfile
    
    response = await fetch('https://raw.githubusercontent.com/nltk/wordnet/archive/refs/heads/master.zip')
    js_buffer = await response.arrayBuffer()
    py_buffer = js_buffer.to_py()  # this is a memoryview
    stream = py_buffer.tobytes()  # now we have a bytes object

    d = Path("/nltk/wordnet")
    d.mkdir(parents=True, exist_ok=True)

    Path('/nltk/wordnet/master.zip').write_bytes(stream)

    zipfile.ZipFile('/nltk/wordnet/master.zip').extractall(
        path='/nltk/wordnet/'
    )

This is the error message that I got:

APPENDING: True ==> py-2880055f-8922-cb23-34e4-db404fb1d7a4 --> PythonError: Traceback (most recent call last):

File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception

File "/lib/python3.10/asyncio/tasks.py", line 232, in __step result = coro.send(None)

File "/lib/python3.10/site-packages/_pyodide/_base.py", line 500, in eval_code_async await CodeRunner(

File "/lib/python3.10/site-packages/_pyodide/_base.py", line 353, in run_async await coroutine

File "<exec>", line 21, in

File "/lib/python3.10/zipfile.py", line 1258, in init self._RealGetContents()

File "/lib/python3.10/zipfile.py", line 1325, in _RealGetContents raise BadZipFile("File is not a zip file") zipfile.BadZipFile: File is not a zip file

What am I doing wrong? Thanks!