Change language

NLP | Distributed Portion with Execnet

| |

How does it work?

  • Use a pickled tagger.
  • First, select the default chunker used by nltk. chunk.ne_chunk () although any chunker will do.
  • Then create a gateway for the remote_chunk module, get the channel, and send the pickled tagger and chunker.
  • Then get the pickled tree, which can be selected and inspected to see the result. Finally, log out of the gateway:

Code: how it works

# import libraries

import execnet, remote_chunk

import nltk.data, nltk.tag, nltk.chunk

import pickle

from nltk.corpus import treebank_chunk

 

tagger = pickle.dumps (nltk.data.load (nltk.tag. _POS_TAGGER))

chunker = pickle. dumps (

  nltk.data.load (nltk.chunk._MULTICLASS_NE_CHUNKER))

gw = execnet.makegateway ()

 

channel = gw.remote_exec (remote_chunk)

channel.send (tagger)
channel.send (chunker)

channel.send (treebank_chunk.sents () [ 0 ])

 

chunk_tree = pickle.loads (channel.receive ())

 

print (chunk_tree)

gw.exit ()

Output:

 Tree (’S’, [Tree (’ PERSON ’, [(’ Pierre’, ’NNP’)]), Tree (’ ORGANIZATION’, [(’Vinken’,’ NNP’)]), (’,’, ’,’), (’61’,’ CD’), (’years’,’ NNS’), (’old’,’ JJ’), (’,’, ’,’), (’will’,’ MD’), (’join’,’ VB’), (’the’,’ DT’), (’board’,’ NN’), (’as’,’ IN’), (’a’,’ DT’), (’nonexecutive’,’ JJ’), (’director’,’ NN’), (’Nov.’,’ NNP’), (’29’,’ CD’), (’.’,’ .’)]) 

The link is slightly different this time as shown in the picture below —

  • The remote_chunk.py module is a little more complex than the remote_tag.py module.
  • In addition to receiving the pickled tagger, it also expects to receive pickled block that implements the ChunkerIinterface.
  • Once it has both the tagger and the block, it expects to receive any number of tokenized sentences that it tags and parses in a tree. This tree is then etched and sent back through the channel:

Code: Explaining the above work

import pickle

 

if __ name__ = = ’__channelexec__’ :

  tagger = pickle.loads (channel.receive ())

chunker = pickle. loads (channel.receive ())

 

for sentence in channel:

chunk_tree = chunker.parse (tagger.tag (sent))

channel.send (pickle.dumps (chunk_tree))

The only external dependency of the remote_chunk module is the pickle module which is part of the Python standard library. You do not need to import any NLTK modules in order to use the tagger or block, because all the necessary data is collected and transmitted over the channel.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically