NLP | Distributed Portion with Execnet

NLP | Python Methods and Functions | String Variables

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.tag, nltk.chunk

import pickle

from nltk.corpus import treebank_chunk


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

chunker = pickle. dumps ( (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 ()


 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 module is a little more complex than the 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.

Get Solution for free from DataCamp guru