Distributed word scoring can be performed using Redis and Execnet together. For each word in movie_reviews corpus, FreqDist and ConditionalFreqDist are used to calculate information gain.
Using >RedisHashFreqDist and a RedisConditionalHashFreqDist, same thing can be performed with Redis. The scores are then stored in a RedisOrderedDict. In order to get a better performance out of Redis, Execnet is used to distribute the counting.
Redis and execnet are used to together to do distributed word scoring. The information gain of each word in the is calculated using a FreqDist and ConditionalFreqDist. Now with Redis, the same thing can be performed using a RedisHashFreqDist and a RedisConditionalHashFreqDist, and then store the scores in a RedisOrderedDict. Execnet can be used to distribute the counting in order to get a better performance out of Redis. Instance of redis-server must be running on localhost after the installation of Redis and execnet.
- For each label in the movie_reviews corpus (which only has pos and neg labels), start by getting a list of tuples – labels and words.
- Then from the dist_featx module, get the word_scores using score_words().
- Total number of words are 39, 764 and word_scores function is an instance of RedisOrderedDict.
- Then get the top 1, 000 words and inspect the top five using the keys() method to see what they are.
- Delete the keys in Redis after getting all the required from word_scores as there is no more need of the data.
Categories : ['neg', 'pos'] Length : 39767 Top Words : [b'bad', b', ', b'and', b'?', b'movie'] [1, 1, 1, 1]
The score_words() is a function from dist_featx. But it is expected to wait for a while as it takes some time to complete. The overhead of using execnet and Redis means it will take significantly longer than a nondistributed, in-memory version of the function.
How it works?
The dist_featx.py module contains the score_words() function, which does the following :
- Opens gateways and channels.
- Sends initialization data to each channel.
- For counting, it sends each (label, words) tuple over a channel.
- Sends a done message to each channel.
- Waits for a done reply back.
- closes the channels and gateways.
- Based on the counts calculates the score of each word.
- Store the score in a RedisOrderedDict.
Score all the words and store the results, once the counting is finished. The code is given below :
A different scoring method should be used if there are more than two labels. To compare two labels, the scoring method will only be accurate. The requirements will dictate how you store word scores.
There are two kinds of data that can be received over the channel after having the instance –
- A done message : It signals that there is no more data coming in over the channel.
Reply back with another done message, finally exit the loop to close the channel.
- A 2-tuple of (label, words): It is used to iterate over to increment counts in both the RedisHashFreqDist and RedisConditionalHashFreqDist
- NLP | Storing an ordered dictionary in Redis
- NLP | Storing Frequency Distribution in Redis
- NLP | Storing Conditional Frequency Distribution in Redis
- NLP | Distributed chunking with Execnet
- NLP | Distributed Tagging with Execnet - Part 2
- NLP | Parallel list processing with execnet
- NLP | Distributed Tagging with Execnet - Part 1
- ML | Models Score and Error
- ML | Fowlkes-Mallows Score
- Bag of words (BoW) model in NLP
- Possible Words using given characters in Python
- NLP | Filtering Insignificant Words
- Count words in a given string
- Python | Stemming words with NLTK
- Python | Extract words from given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.