<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Japneet Singh Chawla</title>
    <description>The latest articles on DEV Community by Japneet Singh Chawla (@japneet121).</description>
    <link>https://dev.to/japneet121</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F29755%2F72fdc56c-57fb-42fc-be26-bd44320547e4.jpeg</url>
      <title>DEV Community: Japneet Singh Chawla</title>
      <link>https://dev.to/japneet121</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/japneet121"/>
    <language>en</language>
    <item>
      <title>Instagram Bot using Python</title>
      <dc:creator>Japneet Singh Chawla</dc:creator>
      <pubDate>Tue, 30 Jun 2020 21:56:29 +0000</pubDate>
      <link>https://dev.to/japneet121/instagram-bot-using-python-4a54</link>
      <guid>https://dev.to/japneet121/instagram-bot-using-python-4a54</guid>
      <description>&lt;p&gt;Growing on Instagram is a difficult and time-consuming job if you are already not a famous personality.&lt;br&gt;
I started my food blogging page &lt;a href="https://www.instagram.com/_.interwined_dodos._/"&gt;@&lt;em&gt;.interwined_dodos.&lt;/em&gt;&lt;/a&gt; (do check it out and follow me there) and faced these challenges in growing. Some of the very common challenges I have faced were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interacting with all community throughout the day is a difficult job&lt;/li&gt;
&lt;li&gt;Instagram blocks the like, comment, follow and unfollow actions if you are over interactive in a short span of time
*Following and engaging with new people of same or different community(again a time-consuming job)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Being a lazy yet ambitious person, I wanted to grow on Instagram but didn't want to spend much time on it.&lt;br&gt;
Here is when my developer mind kicked in and I thought of automating this stuff and my journey of this automation lead me to blogs related to selenium and then to my destination &lt;a href="https://github.com/timgrossmann/InstaPy"&gt;Instapy&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;InstaPy is a tool developed to automate Instagram tasks using &lt;a href="https://www.selenium.dev/"&gt;Selenium&lt;/a&gt; and the brain. It has a large variety of actions that can be performed on Instagram by just a few lines of code. Some popular actions are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smart liking&lt;/li&gt;
&lt;li&gt;Smart Commenting&lt;/li&gt;
&lt;li&gt;Following&lt;/li&gt;
&lt;li&gt;Unfollowing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I said smart liking and commenting because there are lots of configuration options that are available in the InstaPy to give it a human-like touch. Also, it is carefully crafted so that you don't get blocked by Instagram (but it can happen and you can get blocked for months too if you are not careful).&lt;/p&gt;

&lt;p&gt;Frankly speaking, I even got blocked by Instagram for manually performing the actions and I am using this bot from past 2 days and things are fine as of now.&lt;br&gt;
Let's get started.....&lt;/p&gt;
&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Running InstaPy needs Firefox browser installed so install the version as per your operating system from this link.&lt;/p&gt;

&lt;p&gt;I will prefer version 0.6.8 because there are some issues with comments in the latest version 0.6.9, so do a quick pip install to install the package&lt;br&gt;
pip install instapy==0.6.8&lt;/p&gt;

&lt;p&gt;The Code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="s"&gt;""" Quickstart script for InstaPy usage """&lt;/span&gt;
&lt;span class="c1"&gt;# imports
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;instapy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;InstaPy&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;instapy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;smart_run&lt;/span&gt;
&lt;span class="c1"&gt;# login credentials
&lt;/span&gt;&lt;span class="n"&gt;insta_username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;lt;- enter username here
&lt;/span&gt;&lt;span class="n"&gt;insta_password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;lt;- enter password here
# get an InstaPy session!
# set headless_browser=True to run InstaPy in the background
&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;InstaPy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;insta_username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;insta_password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="n"&gt;headless_browser&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;smart_run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;""" Activity flow """&lt;/span&gt;
    &lt;span class="c1"&gt;# general settings
&lt;/span&gt;    &lt;span class="c1"&gt;#sets the percentage of people you want to follow
&lt;/span&gt;    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_do_follow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;percentage&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;#sets the percentage of posts you want to comment
&lt;/span&gt;    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_do_comment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;percentage&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;#list of random comments you want to post
&lt;/span&gt;    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_comments&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s"&gt;"hey @_.interwined_dodos._, have a look"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Great content @_.interwined_dodos._ have a look"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;":heart_eyes: :heart_eyes: :heart_eyes: @_.interwined_dodos._"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="c1"&gt;#setting quotas for the daily and hourly action(I said it's smart)
&lt;/span&gt;    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_quota_supervisor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enabled&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;peak_comments_daily&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;peak_comments_hourly&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;peak_likes_daily&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;peak_likes_hourly&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;sleep_after&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'likes'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'follows'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="c1"&gt;#again some set of configuration which figures out whom to follow
&lt;/span&gt;    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_relationship_bounds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enabled&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                    &lt;span class="n"&gt;delimit_by_numbers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                    &lt;span class="n"&gt;max_followers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4590&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                    &lt;span class="n"&gt;min_followers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                    &lt;span class="n"&gt;min_following&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;77&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;#tags to get posts from and amout is the actions you want 
&lt;/span&gt;    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;like_by_tags&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s"&gt;'nailsofinstagram'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"nails"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it...&lt;br&gt;
With these few lines your bot is ready to roll&lt;/p&gt;

&lt;p&gt;Run the Bot&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python &amp;lt;your_file_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I never thought that creating bot will be this easy. I hope this help many lazy people like me :P&lt;/p&gt;

&lt;p&gt;Also, have look at my other blogs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://techscouter.blogspot.com/2020/05/extracting-text-from-pdf-for-nlp-tasks.html"&gt;Extracting text from PDF&lt;/a&gt;&lt;br&gt;
&lt;a href="https://techscouter.blogspot.com/2018/05/celery-with-heavy-workloads.html"&gt;Using Celery for Heavy Loads&lt;/a&gt;&lt;br&gt;
&lt;a href="https://techscouter.blogspot.com/2017/09/spidering-web-with-python.html"&gt;Scraping the Web&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>instagram</category>
      <category>automation</category>
      <category>bots</category>
    </item>
    <item>
      <title>GloVe for Word Vectorization</title>
      <dc:creator>Japneet Singh Chawla</dc:creator>
      <pubDate>Tue, 22 May 2018 04:00:23 +0000</pubDate>
      <link>https://dev.to/japneet121/glove-for-word-vectorization-1ak0</link>
      <guid>https://dev.to/japneet121/glove-for-word-vectorization-1ak0</guid>
      <description>&lt;p&gt;&lt;a href="https://3.bp.blogspot.com/-U8Cjp06jluc/Wt7rRjQ3KKI/AAAAAAAAbjE/DGD3_b3UOHYyUjsA4DbxJsxXgqG6jX_YwCLcBGAs/s1600/Text-to-vector-3_normal.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F3.bp.blogspot.com%2F-U8Cjp06jluc%2FWt7rRjQ3KKI%2FAAAAAAAAbjE%2FDGD3_b3UOHYyUjsA4DbxJsxXgqG6jX_YwCLcBGAs%2Fs640%2FText-to-vector-3_normal.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
What is GloVe?&lt;/h2&gt;

&lt;p&gt;GloVe stands for global vectors for word representation. It is an unsupervised learning algorithm developed by Stanford for generating word embeddings by aggregating global word-word co-occurrence matrix from a corpus. The resulting embeddings show interesting linear substructures of the word in vector space.&lt;/p&gt;

&lt;p&gt;Examples for linear substructures are:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://3.bp.blogspot.com/-TcJsh6VB75E/Wt7xk0VBQxI/AAAAAAAAbjY/7_pS22MdugQBQ9vOBJUV2hsm2b9bcVbZQCEwYBhgL/s1600/vec2.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F3.bp.blogspot.com%2F-TcJsh6VB75E%2FWt7xk0VBQxI%2FAAAAAAAAbjY%2F7_pS22MdugQBQ9vOBJUV2hsm2b9bcVbZQCEwYBhgL%2Fs640%2Fvec2.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://4.bp.blogspot.com/-8v4NzcL-vvs/Wt7xk6PbLZI/AAAAAAAAbjU/XBmQq_Jnb-UgW6FeoSZ6kQdrejwdjNAhQCLcBGAs/s1600/vec1.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F4.bp.blogspot.com%2F-8v4NzcL-vvs%2FWt7xk6PbLZI%2FAAAAAAAAbjU%2FXBmQq_Jnb-UgW6FeoSZ6kQdrejwdjNAhQCLcBGAs%2Fs640%2Fvec1.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These results are pretty impressive. This type of representation can be very useful in many machine learning algorithms.&lt;/p&gt;

&lt;p&gt;To read more about Word Vectorization you can read my other &lt;a href="http://techscouter.blogspot.in/2017/10/word-vectorization.html" rel="noopener noreferrer"&gt;article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this blog post, we will be learning about GloVe implementation in python. So, let's get started.&lt;/p&gt;

&lt;h2&gt;
Let's create the Embeddings&lt;/h2&gt;

&lt;h3&gt;
Installing Glove-Python&lt;/h3&gt;

The GloVe is implementation in python is available in library &lt;b&gt;glove-python.&lt;/b&gt;




&lt;pre&gt;
&lt;span&gt;pip install glove_python&lt;/span&gt;





&lt;br&gt;
&lt;br&gt;
&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;


&lt;h3&gt;



&lt;h3&gt;
Text Preprocessing&lt;/h3&gt;

In this step, we will pre-process the text like removing the stop words, lemmatize the words etc.



You can perform different steps based on your requirements.








I will use nltk stopword corpus for stop word removal and nltk word lemmatization for finding lemmas.



I order to use nltk corpus you will need to download it using the following commands.



&lt;b&gt;
&lt;/b&gt;



&lt;b&gt;Downloading the corpus&lt;/b&gt;

&lt;b&gt;
&lt;/b&gt;


&lt;pre&gt;import nltk
nltk.download()
#this will open a GUI from which you can download the corpus
&lt;/pre&gt;








&lt;b&gt;Input initialization&lt;/b&gt;




&lt;pre&gt;#list of sentences to be vectorized
lines=["Hello this is a tutorial on how to convert the word in an integer format",
"this is a beautiful day","Jack is going to office"]&lt;/pre&gt;


&lt;b&gt;
&lt;/b&gt;



&lt;b&gt;Removing the Stop Words&lt;/b&gt;



&lt;b&gt;
&lt;/b&gt;


&lt;pre&gt;from nltk.corpus import stopwords
stop_words=set(stopwords.words('english'))

lines_without_stopwords=[]
#stop words contain the set of stop words
for line in lines:
 temp_line=[]
 for word in lines:
  if word not in stop_words:
   temp_line.append (word)
 string=' '
 lines_without_stopwords.append(string.join(temp_line))

lines=lines_without_stopwords
&lt;/pre&gt;



&lt;b&gt;
&lt;/b&gt;



&lt;b&gt;Lemmatization&lt;/b&gt;

&lt;b&gt;
&lt;/b&gt;


&lt;pre&gt;#import WordNet Lemmatizer from nltk
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()


lines_with_lemmas=[]
#stop words contain the set of stop words
for line in lines:
 temp_line=[]
 for word in lines:
  temp_line.append (wordnet_lemmatizer.lemmatize(word))
 string=' '
 lines_with_lemmas.append(string.join(temp_line))
lines=lines_with_lemmas


&lt;/pre&gt;











Now we have done the basic preprocessing of the text. Any other preprocessing stuff can be achieved similarly.






&lt;/h3&gt;

&lt;h3&gt;
Preparing Input&lt;/h3&gt;
&lt;h3&gt;





We have out input in the form of an array of lines. In order for the model to process the data, we need covert our input to an array of array of words ( :\ ).

&lt;b&gt;
&lt;/b&gt;

&lt;u&gt;Our Input&lt;/u&gt;


lines=["Hello this is a tutorial on how to convert the word in an integer format",

"this is a beautiful day","Jack is going to office"]


&lt;b&gt;
&lt;/b&gt;

&lt;u&gt;New Input&lt;/u&gt;


lines=[['Hello', 'this','tutorial', 'on', 'how','convert' ,'word',' integer','format'],

['this' ,'beautiful', 'day'],['Jack','going' , 'office']





&lt;pre&gt;new_lines=[]
for line in lines:
 new_lines=line.split('')
#new lines has the new format
lines=new_lines
&lt;/pre&gt;
&lt;h3&gt;
Building a Glove model&lt;/h3&gt;



&lt;/h3&gt;

&lt;pre&gt;#importing the glove library
from glove import Corpus, Glove

# creating a corpus object
corpus = Corpus() 

#training the corpus to generate the co occurence matrix which is used in GloVe
corpus.fit(lines, window=10)

#creating a Glove object which will use the matrix created in the above lines to create embeddings
#We can set the learning rate as it uses Gradient Descent and number of components

glove = Glove(no_components=5, learning_rate=0.05)
 
glove.fit(corpus.matrix, epochs=30, no_threads=4, verbose=True)
glove.add_dictionary(corpus.dictionary)
glove.save('glove.model')&lt;/pre&gt;







Creating a glove model uses the co-occurrence matrix generated by the Corpus object to create the embeddings.

The &lt;b&gt;corpus.fit&lt;/b&gt; takes two arguments:

&lt;ul&gt;
&lt;li&gt;lines - this is the 2D array we created after the  pre-processing&lt;/li&gt;
&lt;li&gt;window - this is the distance between two words algo should consider to find some relationship between them&lt;/li&gt;
&lt;/ul&gt;

Parameters of Glove:



&lt;ul&gt;
&lt;li&gt;no_of_components - This is the dimension of the output vector generated by the GloVe&lt;/li&gt;
&lt;li&gt;learning_rate - Algo uses gradient descent so learning rate defines the rate at which the algo reaches towards the minima (lower the rate more time it takes to learn but reaches the minimum value)&lt;/li&gt;
&lt;/ul&gt;

Parameters of glove.fit :



&lt;ul&gt;
&lt;li&gt;cooccurence_matrix: the matrix of word-word co-occurrences&lt;/li&gt;
&lt;li&gt;epochs: this defines the number of passes algo makes through the data set&lt;/li&gt;
&lt;li&gt;no_of_threads: number of threads used by the algo to run&lt;/li&gt;
&lt;/ul&gt;



After the training glove object has the word vectors for the lines we have provided. But the dictionary still resides in the corpus object. We need to add the dictionary to the glove object to make it complete.





&lt;pre&gt;glove.add_dictionary(corpus.dictionary)
&lt;/pre&gt;

&lt;p&gt;This line does the dictionary addition in the glove object. After this, the object is ready to provide you with the embeddings.&lt;/p&gt;

&lt;pre&gt;print glove.word_vectors[glove.dictionary['samsung']]
OUTPUT:
&lt;pre&gt;[ 0.04521741  0.02455266 -0.06374787 -0.07107575  0.04608054]&lt;/pre&gt;
&lt;/pre&gt;



&lt;p&gt;This will print the embeddings for the word "samsung".&lt;/p&gt;

&lt;h2&gt;
End Notes &lt;/h2&gt;

&lt;p&gt;We have learned how to generate the vectors for the text data which is very useful for creating many Machine Learning models and techniques like SVM, KNN, K-Means, Logistic Classifiers, Sentiment Analysis, Document Classification etc.&lt;/p&gt;

&lt;p&gt;More can be learned about the GloVe on Stanford's &lt;a href="https://nlp.stanford.edu/projects/glove/" rel="noopener noreferrer"&gt;website&lt;/a&gt;. &lt;/p&gt;



</description>
      <category>wordvectorization</category>
      <category>python</category>
      <category>glove</category>
    </item>
    <item>
      <title>Optimizing Celery for Workloads</title>
      <dc:creator>Japneet Singh Chawla</dc:creator>
      <pubDate>Mon, 21 May 2018 02:37:45 +0000</pubDate>
      <link>https://dev.to/japneet121/optimizing-celery-for-workloads-57i9</link>
      <guid>https://dev.to/japneet121/optimizing-celery-for-workloads-57i9</guid>
      <description>&lt;h2&gt;
Introduction&lt;/h2&gt;

Firstly, a brief background about myself. I am working as a Software Engineer in one of the Alternate Asset Management Organization (Handling 1.4 Trillion with our product suite) responsible for maintaining and developing a product &lt;b&gt;ALT Data Analyzer. &lt;/b&gt;My work is focused on making the engine run and feed the machines with their food.





This article explains the problems we faced with scaling up our architecture and solution we followed.
 I am dividing the blog in the following different sections:

&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Product Brief&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Current Architecture&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Problem With Data loads and Sleepless Nights&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Solutions Tried&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;The Final Destination&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;span&gt;
&lt;/span&gt;


&lt;h2&gt;
&lt;span&gt;Product Brief&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;The idea of building this product was to give users an aggregated view of the WEB around a company. By WEB I mean the data that is flowing freely over all the decentralized nodes of the internet. We try to capture all the financial, technical and fundamental data for the companies, pass that data through our massaging and analyzing pipes and provide an aggregated view on the top of this data. Our dashboards can be one stop for all the information around a company and can aid an investor in his analysis. &lt;/p&gt;

&lt;p&gt;Some of the Data Sources that we support as of now are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stock News from multiple sources&lt;/li&gt;
&lt;li&gt;RSS feed &lt;/li&gt;
&lt;li&gt;Twitter&lt;/li&gt;
&lt;li&gt;Reddit&lt;/li&gt;
&lt;li&gt;Yahoo News&lt;/li&gt;
&lt;li&gt;Yahoo Finance&lt;/li&gt;
&lt;li&gt;Earning Calenders&lt;/li&gt;
&lt;li&gt;Report Filings&lt;/li&gt;
&lt;li&gt;Company Financials&lt;/li&gt;
&lt;li&gt;Stock Prices&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
Current Architecture&lt;/h2&gt;


&lt;a href="https://4.bp.blogspot.com/-zrXUbB9-EVA/WvaSLHGo0fI/AAAAAAAAbmU/jwHR-jSLH7MALNt8Q7ghWotC5H9dhQvcgCLcBGAs/s1600/internet.jpeg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F4.bp.blogspot.com%2F-zrXUbB9-EVA%2FWvaSLHGo0fI%2FAAAAAAAAbmU%2FjwHR-jSLH7MALNt8Q7ghWotC5H9dhQvcgCLcBGAs%2Fs640%2Finternet.jpeg"&gt;&lt;/a&gt;

&lt;h3&gt;
Broad View&lt;/h3&gt;

We knew that problem that we are solving has to deal with the cruel decentralized Internet. And we need to divide the large task of getting the data from the web and analyzing it into small tasks.







&lt;a href="https://1.bp.blogspot.com/-tPW3nCkXYps/WvaVW4XlHoI/AAAAAAAAbmg/_jpZxvZMOA8M-CpZ0fuffHzeU9IzaYbFgCLcBGAs/s1600/croped1.jpeg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F1.bp.blogspot.com%2F-tPW3nCkXYps%2FWvaVW4XlHoI%2FAAAAAAAAbmg%2F_jpZxvZMOA8M-CpZ0fuffHzeU9IzaYbFgCLcBGAs%2Fs400%2Fcroped1.jpeg"&gt;&lt;/a&gt;

Fig 1





On exploring different projects and technologies and analyzing the community support we came to a decision to use &lt;b&gt;Python&lt;/b&gt; as our language of choice and &lt;b&gt;Celery&lt;/b&gt; as our commander. 





Python is a pretty vast language backed by a large set of libraries. Since inception, It has gained a lot of popularity among the developer and Data Science communities. One of the major reason to use python as a backend is the project Celery. 





Its website defines celery as An asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well. To more about celery you can visit its website &lt;a href="http://www.celeryproject.org/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.





By now we were clear of how we want to proceed. We wanted to divide the process in Fig 1 into granular units (in celery terminology task). Keeping this as the baseline we identified all the individual units which can work independently in the system.

This gave rise to a new look to Fig 1





&lt;a href="https://2.bp.blogspot.com/-ShzlN3uYpA4/Wvacz_r0wtI/AAAAAAAAbmw/2q0sPcEN4FM3qTkEPOMTqq_zQyL5CID8wCLcBGAs/s1600/cropped1.jpeg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F2.bp.blogspot.com%2F-ShzlN3uYpA4%2FWvacz_r0wtI%2FAAAAAAAAbmw%2F2q0sPcEN4FM3qTkEPOMTqq_zQyL5CID8wCLcBGAs%2Fs640%2Fcropped1.jpeg"&gt;&lt;/a&gt;

Fig 2





Were are using MongoDB in a replication cluster as a Database engine and Redis DB for queuing and communication between independent celery workers.

The following figure describes the communication of celery works with the help of broker(Redis)





&lt;a href="https://3.bp.blogspot.com/-S1r1aSUiOcc/Wvae5cP0BqI/AAAAAAAAbnE/QNNKir9dgUEGEpDk00KWfOOqLfVxKlAoQCLcBGAs/s1600/workers.jpeg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F3.bp.blogspot.com%2F-S1r1aSUiOcc%2FWvae5cP0BqI%2FAAAAAAAAbnE%2FQNNKir9dgUEGEpDk00KWfOOqLfVxKlAoQCLcBGAs%2Fs640%2Fworkers.jpeg"&gt;&lt;/a&gt;








&lt;h2&gt;
 &lt;span&gt;Problem With Data loads and Sleepless Nights&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;Everything was working fine with the development workloads, we had deployed around a cluster of 5 machines in the celery cluster (i7 processors with 16 gigs of memory each) with each machine running some celery workers. &lt;/p&gt;

&lt;p&gt;But this was the calm before the storm. As we planned to increase our workloads and configure more tasks the system started to fade and run out of memory very frequently. We were now generating around 10 million tasks a day. Celery workers were not able to process the tasks at this speed and this was building up tasks in the queues which were slowing down the Redis broker as it was now storing a large number of tasks, this was, in turn, making the whole system run out of memory and gradually to a halt.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://3.bp.blogspot.com/-yFpJdkfSkB0/WvgHuFRr77I/AAAAAAAAbno/p4siRfrXTVIDEypgbo5qd9LfiKRjoW-qACLcBGAs/s1600/calm.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F3.bp.blogspot.com%2F-yFpJdkfSkB0%2FWvgHuFRr77I%2FAAAAAAAAbno%2Fp4siRfrXTVIDEypgbo5qd9LfiKRjoW-qACLcBGAs%2Fs640%2Fcalm.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our beautifully designed ship was sinking and we had to do something to save it.&lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;br&gt;
&lt;/span&gt;&lt;/p&gt;

&lt;h2&gt;
&lt;span&gt;Solutions Tried&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;As we faced this issue the first approach we thought, as any other developer would have applied is to add more nodes to the cluster. While discussing this solution there was an argument that if the loads increases again with time, will we be adding more nodes again and again.&lt;/p&gt;

&lt;p&gt;So, we skipped this solution and agreed to dig deep into the problem. &lt;/p&gt;

&lt;p&gt;On deep analysis we realized that a large number of tasks were populated by the publishers and these tasks were getting stored in the Redis queues, with the data loads the Redis eventually slows down the rate at which the tasks were being sent to the consumers making the consumers idle.&lt;/p&gt;

&lt;p&gt;So our system was stuck in an infinite loop. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redis sends tasks at high speed but consumers cannot consume at that rate.&lt;/li&gt;
&lt;li&gt;Redis slows down and lowers the rate at which it sends tasks and now consumers stay idle most of the time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In both the situations, the common name that was in doubt was the Redis DB. On researching and exploring the RedisDB we found that it is a single-threaded system and performs all the tasks in a round robin fashion. Also, it was saving the tasks received by the systems in memory which was increasing the memory consumption and the under heavy loads that single thread was busy performing the persistence and other tasks that it slows down the polling process initiated by the consumers. So, we found the reasons for both the problems that we were facing.&lt;/p&gt;

&lt;p&gt;To handle the heavy workload one of the choices was to shard the Redis server into a cluster. So we created a Redis partition set (tutorials can be seen &lt;a href="https://redis.io/topics/partitioning" rel="noopener noreferrer"&gt;here&lt;/a&gt;). We created a cluster of three nodes with each node handling an equal number of keys through a consistent hashing technique. &lt;/p&gt;

&lt;p&gt;We plugged this cluster with the celery app and the workers were throwing an exception "MOVED to server 192.168.12.12"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://2.bp.blogspot.com/-zw8o3gX19lI/WvibxsT5RII/AAAAAAAAboE/nEeZLU_ia0kqszxTpylxt2FoYDDyfrKfgCEwYBhgL/s1600/fr_201691716455.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F2.bp.blogspot.com%2F-zw8o3gX19lI%2FWvibxsT5RII%2FAAAAAAAAboE%2FnEeZLU_ia0kqszxTpylxt2FoYDDyfrKfgCEwYBhgL%2Fs400%2Ffr_201691716455.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On googling we found that Redis cluster is not yet supported by celery. On one hand we thought we had a solution but on the other, that was not yet supported by the underlying framework :(&lt;/p&gt;

&lt;p&gt;Now the exploration began again to get a solution to our problem and we thought of using a proxy server in front of Redis cluster &lt;a href="https://github.com/twitter/twemproxy" rel="noopener noreferrer"&gt;Twemproxy&lt;/a&gt;. But this time we first choose to check the compatibility with our framework and boom...... we cannot be more wiser in taking this path.&lt;/p&gt;

&lt;p&gt;Proxy was not yet supported by Celery.&lt;/p&gt;

&lt;p&gt;Now frustrated with this incompatibility issue we tried to figure out what all things are compatible with the framework. Some of the compatible brokers were&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SQS&lt;/li&gt;
&lt;li&gt;Redis&lt;/li&gt;
&lt;li&gt;RabbitMQ&lt;/li&gt;
&lt;li&gt;Zookeeper&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A straightaway thought was to try a different broker so, we began to explore these options. Following table proved useful in narrowing down our research&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Status&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Monitoring&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Remote Control&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;RabbitMQ&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Stable&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Redis&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Stable&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Amazon SQS&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Stable&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Zookeeper&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Experimental&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Redis is what we have already tried so we went for the second option that is stable and provides more features i.e RabbitMQ.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Spoiler:&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;By now we knew that RabbitMQ is one the best choice for the brokers and is used by wide variety of clients in production and Redis is the best choice in terms of result backend (intermediate results that are stored by a task in Celery chains and chords).&lt;/p&gt;

&lt;p&gt;We did necessary changes to the system and tried to run the system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://3.bp.blogspot.com/-dTzKGioKH8c/Wvig1fw85LI/AAAAAAAAboU/3bugIbOyg6IrPyDrVIVrGqMfr6DC-tJ9QCLcBGAs/s1600/fc6ba9d5149c057a2582a7f07523abd2.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F3.bp.blogspot.com%2F-dTzKGioKH8c%2FWvig1fw85LI%2FAAAAAAAAboU%2F3bugIbOyg6IrPyDrVIVrGqMfr6DC-tJ9QCLcBGAs%2Fs200%2Ffc6ba9d5149c057a2582a7f07523abd2.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time system nearly went into an irrecoverable state consuming all the memory and we had to restart the servers.&lt;/p&gt;

&lt;p&gt;While all this was happening we were monitoring the RabbitMQ through its admin tool and found a fishy thing. It was creating a large number of queues nearly as many as the number of tasks.&lt;/p&gt;

&lt;p&gt;On googling we found that the queues were created to store the result of the intermediate tasks and this was consuming too much space in the memory as all this storage was in memory.&lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;br&gt;
&lt;/span&gt;&lt;/p&gt;

&lt;h2&gt;
&lt;span&gt;The Final Destination&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;Collecting all the clues from our exploration we decided to use both the systems i.e Redis and RabbitMQ for the work that they are best at.&lt;/p&gt;

&lt;p&gt;We deployed Redis as a result backend to store the intermediate results and RabbitMQ as a broker for maintaining communication and passing tasks (remember the spoiler above).&lt;/p&gt;



&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-zFlxvWKDZh8/WvijzlNdrfI/AAAAAAAAbog/ZOD6D9Hq9_8i5wL5zq3BK60BCfqCR-bSQCLcBGAs/s1600/fg.jpeg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F1.bp.blogspot.com%2F-zFlxvWKDZh8%2FWvijzlNdrfI%2FAAAAAAAAbog%2FZOD6D9Hq9_8i5wL5zq3BK60BCfqCR-bSQCLcBGAs%2Fs640%2Ffg.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this architecture, we were able to run the system under the workload on over 10 million tasks a day which can be scaled easily.&lt;/p&gt;

&lt;p&gt;Hope this helps someone going through the same problem.&lt;/p&gt;

&lt;h2&gt;
Acknowledgments&lt;/h2&gt;

&lt;p&gt;We took hints from the Celery documentation, Redis documentation, Stackoverflow threads, Github Issue page of Celery, Case studies by Instagram and Trivago.&lt;/p&gt;



</description>
      <category>celery</category>
      <category>python</category>
      <category>distributedcomputing</category>
      <category>redis</category>
    </item>
    <item>
      <title>Word Vectorization -Word2Vec</title>
      <dc:creator>Japneet Singh Chawla</dc:creator>
      <pubDate>Tue, 31 Oct 2017 04:42:44 +0000</pubDate>
      <link>https://dev.to/japneet121/word-vectorization--word2vec-177</link>
      <guid>https://dev.to/japneet121/word-vectorization--word2vec-177</guid>
      <description>

&lt;h2&gt;
&lt;span id="goog_114634844"&gt;&lt;/span&gt;&lt;span id="goog_114634845"&gt;&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://3.bp.blogspot.com/-EzGSSDTyxq4/Wff7sYIW3iI/AAAAAAAAB5c/NRsslGTJlb8qZBxZKVZGFI0lsOS0eu1sACLcBGAs/s1600/cropped.png"&gt;&lt;img height="251" src="https://res.cloudinary.com/practicaldev/image/fetch/s--zOpFwWoq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://3.bp.blogspot.com/-EzGSSDTyxq4/Wff7sYIW3iI/AAAAAAAAB5c/NRsslGTJlb8qZBxZKVZGFI0lsOS0eu1sACLcBGAs/s640/cropped.png" width="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;

Introduction&lt;/h2&gt;

&lt;p&gt;Machine Learning has become the hottest topic in Data Industry with increasing demand for professionals who can work in this domain.&lt;/p&gt;

&lt;p&gt;There is large amount of textual data present in internet and giant servers around the world.&lt;/p&gt;

&lt;p&gt;Just for some facts&lt;/p&gt;

&lt;p&gt;&lt;span id="docs-internal-guid-152fa68b-635d-a06a-c668-fdf727374bb6"&gt;&lt;/span&gt;&lt;/p&gt;

&lt;ul&gt;&lt;span id="docs-internal-guid-152fa68b-635d-a06a-c668-fdf727374bb6"&gt;
&lt;li&gt;
&lt;span&gt;1,209,600 new data producing social media users each day.&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span&gt;656 million tweets per day!&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span&gt;More than 4 million hours of content uploaded to Youtube every day, with users watching 5.97 billion hours of Youtube videos each day.&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span&gt;67,305,600 Instagram posts uploaded each day&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span&gt;There are over 2 billion monthly active Facebook users, compared to 1.44 billion at the start of 2015 and 1.65 at the start of 2016.&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span&gt;Facebook has 1.32 billion daily active users on average as of June 2017&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span&gt;4.3 BILLION Facebook messages posted daily!&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span&gt;5.75 BILLION Facebook likes every day.&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span&gt;22 billion texts sent every day.&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span&gt;5.2 BILLION daily Google Searches in 2017.&lt;/span&gt;
&lt;/li&gt;
&lt;/span&gt;&lt;/ul&gt;
&lt;span id="docs-internal-guid-152fa68b-635d-a06a-c668-fdf727374bb6"&gt; &lt;/span&gt;
&lt;h2&gt;
Need for Vectorization&lt;/h2&gt;

&lt;a href="https://2.bp.blogspot.com/-g-5dcPNryNE/Wff5k_l_JCI/AAAAAAAAB5U/D4W2kDJ5MjEgDoU0I2WBhy7fsFpnkRz_QCEwYBhgL/s1600/vector_space1.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--chkP6p7Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://2.bp.blogspot.com/-g-5dcPNryNE/Wff5k_l_JCI/AAAAAAAAB5U/D4W2kDJ5MjEgDoU0I2WBhy7fsFpnkRz_QCEwYBhgL/s1600/vector_space1.png"&gt;&lt;/a&gt;

&lt;span&gt;&lt;span&gt;The amount of textual data is massive, and the problem with textual data is that it needs to be represented in a format that can be mathematically used in solving some problem.&lt;/span&gt;&lt;/span&gt;

&lt;span&gt;&lt;span&gt;In simple words, we need to get an integer representation of a word. &lt;/span&gt;&lt;/span&gt;&lt;span&gt;There are simple to complex ways to solve this problem. &lt;/span&gt;

&lt;span&gt;
&lt;/span&gt; 
&lt;h3&gt;
&lt;span&gt;One of the easiest ways to solve the problem is creating a simple &lt;b&gt;word to integer mapping&lt;/b&gt;.&lt;/span&gt;
&lt;/h3&gt;



&lt;pre&gt;#list of sentences to be vectorized
line="Hello this is a tutorial on how to convert the word in an integer format"

#dictionary to hold the words
word_list={}

#initialize the counter for assigning to different word
counter=0

#iterate over the words
for word in line:
 #check if the word is in dict
 if word not in word_list:
  word_list[word]=counter
  #update the counter
  counter+=1
  

&lt;/pre&gt;




&lt;p&gt;This will return us the dictionary of words with the corresponding integer representations.&lt;/p&gt;

&lt;h3&gt;
Another way to get these numbers is by using TD-IDF&lt;/h3&gt;


TF-IDF stands for term frequency-inverse document frequency which assigns some weight to the word based on the number of occurrences in the document also taking into consideration the frequency of the word in all the documents. This approach is better than the previous approach as it lowers the weight of the words that occur too often in all the sentences like 'a', 'the', 'as' etc and increases the weight of the words that can be important in a sentence.

This is useful in the scenarios where we want to get the important words from all the documents .

This approach is also used in topic modelling.  




&lt;h3&gt;
The third approach and the one on which this article will be focussing is Word2VEC&lt;/h3&gt;

&lt;p&gt;Word2vec is a group of related models that are used to produce so-called word embeddings. These models are shallow, two-layer neural networks, that are trained to reconstruct linguistic contexts of words.&lt;/p&gt;

&lt;p&gt;After training, word2vec models can be used to map each word to a vector of typically several hundred elements, which represent that word's relation to other words. This vector is the neural network's hidden layer.&lt;/p&gt;

&lt;p&gt;Word2vec relies on either skip-grams or continuous bag of words (CBOW) to create neural word embeddings. It was created by a team of researchers led by Tomas Mikolov at Google. The algorithm has been subsequently analyzed and explained by other researchers.&lt;/p&gt;

&lt;p&gt;Luckily if you want to use this model in your work you don't have to write these algorithms.&lt;br&gt;
&lt;a href="https://radimrehurek.com/gensim/"&gt;Gensim&lt;/a&gt; is one the library in Python that has some of the awesome features required for text processing and Natural Language Processing. In the rest of the article, we will learn to use this awesome library for word vectorization.&lt;/p&gt;


&lt;a href="https://2.bp.blogspot.com/-TCovoMhSwL4/Wff5lGQa45I/AAAAAAAAB5U/hK1zJ-FDlhElm5YqFxwrkEmO-KcfpSUIQCEwYBhgL/s1600/man_woman.jpg"&gt;&lt;img height="310" src="https://res.cloudinary.com/practicaldev/image/fetch/s--RmxLrxwD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://2.bp.blogspot.com/-TCovoMhSwL4/Wff5lGQa45I/AAAAAAAAB5U/hK1zJ-FDlhElm5YqFxwrkEmO-KcfpSUIQCEwYBhgL/s400/man_woman.jpg" width="400"&gt;&lt;/a&gt;
&lt;h4&gt;
Installing Gensim&lt;/h4&gt;


&lt;pre&gt;&lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;upgrade&lt;/span&gt; &lt;span class="n"&gt;gensim&lt;/span&gt;&lt;/pre&gt;



&lt;br&gt;



&lt;span&gt;
&lt;/span&gt; It has three major dependencies

&lt;ul&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;NumPy&lt;/li&gt;
&lt;li&gt;SciPy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make sure you install the dependencies before installing gensim.&lt;/p&gt;

&lt;p&gt;Lets' get our hands dirty on the code.&lt;/p&gt;

&lt;h4&gt;
Text Preprocessing:&lt;/h4&gt;

&lt;p&gt;In this step, we will pre-process the text like removing the stop words, lemmatize the words etc.&lt;/p&gt;

&lt;p&gt;You can perform different steps based on your requirements.&lt;/p&gt;

&lt;p&gt;I will use nltk stopword corpus for stop word removal and nltk word lemmatization for finding lemmas.&lt;/p&gt;

&lt;p&gt;I order to use nltk corpus you will need to download it using the following commands.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;br&gt;
&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Downloading the corpus&lt;/b&gt;&lt;br&gt;
&lt;b&gt;&lt;br&gt;
&lt;/b&gt;&lt;/p&gt;

&lt;pre&gt;import nltk
nltk.download()
#this will open a GUI from which you can download the corpus
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Input initialization&lt;/b&gt;&lt;/p&gt;

&lt;pre&gt;#list of sentences to be vectorized
lines=["Hello this is a tutorial on how to convert the word in an integer format",
"this is a beautiful day","Jack is going to office"]&lt;/pre&gt;

&lt;b&gt;
&lt;/b&gt;

&lt;b&gt;Removing the Stop Words&lt;/b&gt;

&lt;b&gt;
&lt;/b&gt;

&lt;pre&gt;from nltk.corpus import stopwords
stop_words=set(stopwords.words('english'))

lines_without_stopwords=[]
#stop words contain the set of stop words
for line in lines:
 temp_line=[]
 for word in lines:
  if word not in stop_words:
   temp_line.append (word)
 string=' '
 lines_without_stopwords.append(string.join(temp_line))

lines=lines_without_stopwords
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;&lt;br&gt;
&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Lemmatization&lt;/b&gt;&lt;br&gt;
&lt;b&gt;&lt;br&gt;
&lt;/b&gt;&lt;/p&gt;

&lt;pre&gt;#import WordNet Lemmatizer from nltk
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()


lines_with_lemmas=[]
#stop words contain the set of stop words
for line in lines:
 temp_line=[]
 for word in lines:
  temp_line.append (wordnet_lemmatizer.lemmatize(word))
 string=' '
 lines_with_lemmas.append(string.join(temp_line))
lines=lines_with_lemmas


&lt;/pre&gt;

&lt;p&gt;Now we have done the basic preprocessing of the text. Any other preprocessing stuff can be achieved similarly.&lt;/p&gt;

&lt;h3&gt;
Preparing Input&lt;/h3&gt;


&lt;br&gt;



We have out input in the form of array of lines. In order for model to process the data we need covert our input to an array of array of words ( :\ ).

&lt;b&gt;
&lt;/b&gt;

&lt;u&gt;&lt;i&gt;Our Input&lt;/i&gt;&lt;/u&gt;


lines=["Hello this is a tutorial on how to convert the word in an integer format",

"this is a beautiful day","Jack is going to office"]



&lt;b&gt;
&lt;/b&gt;

&lt;u&gt;&lt;i&gt;New Input&lt;/i&gt;&lt;/u&gt;


lines=[['Hello', 'this','tutorial', 'on', 'how','convert' ,'word',' integer','format'],

['this' ,'beautiful', 'day'],['Jack','going' , 'office']







&lt;pre&gt;new_lines=[]
for line in lines:
 new_lines=line.split('')
#new lines has the new format
lines=new_lines
&lt;/pre&gt;


&lt;h3&gt;
Building the WORD2VEC Model&lt;/h3&gt;


Building a model with gensim is just a piece of cake .





&lt;pre&gt;#import the gensim package
model = gensim.models.Word2Vec(lines, min_count=1,size=2)

&lt;/pre&gt;







Here important is to understand the hyperparameters that can be used to train the model.

Word2vec model constructor is defined as:





&lt;pre&gt;gensim.models.word2vec.Word2Vec(sentences=None, size=100, 
alpha=0.025, window=5, min_count=5, max_vocab_size=None, 
sample=0.001, seed=1, workers=3, min_alpha=0.0001, sg=0, 
hs=0, negative=5, cbow_mean=1, hashfxn=&amp;lt;built-in function hash&amp;gt;, 
iter=5, null_word=0, trim_rule=None, sorted_vocab=1,
batch_words=10000, compute_loss=False)&lt;/pre&gt;











&lt;i&gt;sentence=&lt;/i&gt; This is the input provided in the form of a list





size= This defines the size of the vector we want to convert the word ('Hello'=[ ? , ? , ? ] if size=3)





alpha= It is the initial learning rate (will linearly drop to min_alpha as training progresses).





window= It is the maximum distance between the current and predicted word within a sentence.






min_count= ignore all words with total frequency lower than this.





max_vocab_size = limit RAM during vocabulary building; if there are more unique words than this, then prune the infrequent ones. Every 10 million word types need about 1GB of RAM. Set to None for no limit (default).





sample = threshold for configuring which higher-frequency words are randomly downsampled;

default is 1e-3, useful range is (0, 1e-5).





workers = use this many worker threads to train the model (=faster training with multicore machines).





hs = if 1, hierarchical softmax will be used for model training. If set to 0 (default), and negative is non-zero, negative sampling will be used.





negative = if &amp;gt; 0, negative sampling will be used, the int for negative specifies how many “noise words should be drawn (usually between 5-20). Default is 5. If set to 0, no negative sampling is used.





cbow_mean = if 0, use the sum of the context word vectors. If 1 (default), use the mean. Only applies when cbow is used.





hashfxn = hash function to use to randomly initialize weights, for increased training reproducibility. The default is Python’s rudimentary built-in hash function.





iter = number of iterations (epochs) over the corpus. Default is 5.





trim_rule = vocabulary trimming rule specifies whether certain words should remain in the vocabulary, be trimmed away, or handled using the default (discard if word count &amp;lt; min_count). Can be None (min_count will be used), or a callable that accepts parameters (word, count, min_count) and returns either utils.RULE_DISCARD, utils.RULE_KEEP or utils.RULE_DEFAULT. Note: The rule, if given, is only used to prune vocabulary during build_vocab() and is not stored as part of the model.





sorted_vocab = if 1 (default), sort the vocabulary by descending frequency before assigning word indexes.





batch_words = target size (in words) for batches of examples passed to worker threads (and thus cython routines). Default is 10000. (Larger batches will be passed if individual texts are longer than 10000 words, but the standard cython code truncates to that maximum.)


&lt;h3&gt;
Using the model&lt;/h3&gt;


&lt;pre&gt;#saving the model persistence
model.save('model.bin')

#loading the model
model = gensim.models.KeyedVectors.load_word2vec_format('model.bin', binary=True) 

#getting the most similar words
model.most_similar(positive=['beautiful', 'world'], negative=['convert'], topn=1)

#finding the odd one out
model.doesnt_match("bullish holding stock".split())

#getting the vector for any word
model[word]

#finding the similarity between words
model.similarity('woman', 'man')


&lt;/pre&gt;











For more details, you can read the documentation of the word2vec  gensim &lt;span id="goog_193205381"&gt;&lt;/span&gt;&lt;span id="goog_193205382"&gt;&lt;/span&gt;&lt;a href="https://radimrehurek.com/gensim/models/word2vec.html"&gt;here&lt;/a&gt;.





</description>
      <category>word2vec</category>
      <category>gensim</category>
      <category>textanalytics</category>
      <category>wordvectorization</category>
    </item>
    <item>
      <title>Spidering the WEB </title>
      <dc:creator>Japneet Singh Chawla</dc:creator>
      <pubDate>Sun, 17 Sep 2017 11:37:28 +0000</pubDate>
      <link>https://dev.to/japneet121/spidering-the-web</link>
      <guid>https://dev.to/japneet121/spidering-the-web</guid>
      <description>&lt;h2&gt;
&lt;/h2&gt;

&lt;h2&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://2.bp.blogspot.com/-wufi5ZCBw_E/Wbtjh0rRpFI/AAAAAAAAByU/O2ph9Y0cSJcRnIi23pNT4PkiFcifJ5RZQCLcBGAs/s1600/web-scraping-services.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F2.bp.blogspot.com%2F-wufi5ZCBw_E%2FWbtjh0rRpFI%2FAAAAAAAAByU%2FO2ph9Y0cSJcRnIi23pNT4PkiFcifJ5RZQCLcBGAs%2Fs1600%2Fweb-scraping-services.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
Introduction&lt;/h2&gt;

&lt;p&gt;We will be talking about &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spidering/Scraping&lt;/li&gt;
&lt;li&gt;How to do it elegantly in python&lt;/li&gt;
&lt;li&gt;Limitations and restriction &lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;In the previous posts, I shared some of the methods of text mining and analytics but one of the major and most important tasks before analytics is getting the data which we want to analyze.&lt;/p&gt;

&lt;p&gt;Text data is present all over in forms of blogs, articles, news, social feeds, posts etc and most of it is distributed to users in the form of API's, RSS feeds, Bulk downloads and Subscriptions.&lt;/p&gt;

&lt;p&gt;Some sites do not provide any means of pulling the data programmatically, this is where scrapping comes into the picture.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Note: &lt;/b&gt;Scraping information from the sites which are not free or is not publically available can have serious consequences.&lt;/p&gt;

&lt;p&gt;Web Scraping is a technique of getting a web page in the form of HTML and parsing it to get the desired information.&lt;/p&gt;

&lt;p&gt;HTML is very complex in itself due to loose rules and a large number of attributes. Information can be scraped in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manually filtering using regular expressions&lt;/li&gt;
&lt;li&gt;Python's way -Beautiful Soup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this post, we will be discussing beautiful soup's way of scraping.&lt;/p&gt;

&lt;h2&gt;
Beautiful Soup&lt;/h2&gt;

&lt;p&gt;As per the definition in its &lt;a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;"&lt;a href="http://www.crummy.com/software/BeautifulSoup/" rel="noopener noreferrer"&gt;Beautiful Soup&lt;/a&gt;&lt;span&gt; is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching and modifying the parse tree. It commonly saves programmers hours or days of work.&lt;/span&gt;"&lt;/p&gt;

&lt;p&gt;If you have ever tried something like parsing texts and HTML documents that you will understand how brilliantly this module is built and really save a lot of programmers work and time.&lt;/p&gt;

&lt;p&gt;Let's start with beautiful soup&lt;/p&gt;

&lt;h3&gt;
Installation&lt;/h3&gt;

&lt;p&gt;I hope python is installed in your system. To install Beautiful Soup you can use pip&lt;/p&gt;

&lt;p&gt;pip install beautifulsoup4&lt;/p&gt;

&lt;h3&gt;
Getting Started&lt;/h3&gt;

&lt;p&gt;&lt;b&gt;Problem 1: Getting all the links from a page.&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;For this problem, we will use a sample HTML string which has some links and our goal is to get all the links&lt;/p&gt;

&lt;pre&gt;&lt;span&gt;html_doc&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;"""&lt;/span&gt;&lt;span&gt;
&lt;/span&gt;&lt;span&gt;&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;h1&amp;gt;Sample Links&amp;lt;/h1&amp;gt;
&amp;lt;br&amp;gt;
&amp;lt;a href="https://www.google.com"&amp;gt;Google&amp;lt;/a&amp;gt;
&amp;lt;br&amp;gt;
&amp;lt;a href="https://www.apple.com"&amp;gt;Apple&amp;lt;/a&amp;gt;
&amp;lt;br&amp;gt;
&amp;lt;a href="https://www.yahoo.com"&amp;gt;Yahoo&amp;lt;/a&amp;gt;
&amp;lt;br&amp;gt;
&amp;lt;a href="https://www.msdn.com"&amp;gt;MSDN&amp;lt;/a&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;span&gt;
&lt;/span&gt;&lt;span&gt;"""&lt;/span&gt;&lt;/pre&gt;











&lt;pre&gt;#to import the package 
from bs4 import BeautifulSoup


#creating an object of BeautifulSoup and pass 2 parameters
#1)the html t be scanned
#2)the parser to be used(html parser ,lxml parser etc)
soup=BeautifulSoup(html_doc,"html.parser")


#to find all the anchor tags in the html string
#findAll returns a list of tags in thi scase anchors(to get first one we can use find )
anchors=soup.findAll('a')

#getting links from anchor tags
for a in anchor:
    print a.get('href') #get is used to get the attributes of a tags element
    #print a['href'] can also be used to access the attribute of a tag

&lt;/pre&gt;

&lt;p&gt;This is it, just 5-6 lines to get any tag from the the html and iterating over it, finding some attriutes.&lt;br&gt;
Can you this of doing this with the help of regular expressions. It will be one heck of a job doing it with RE. We can think of how well the module is coded to perform all this functions.&lt;/p&gt;

&lt;p&gt;Talking about the parsers (one we have passed while creating a Beautiful Soup object), we have multiple choices if parsers.&lt;/p&gt;

&lt;p&gt;This table summarizes the advantages and disadvantages of each parser library:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;span&gt;Parser&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span&gt;Typical usage&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span&gt;Advantages&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span&gt;Disadvantages&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;span&gt;Python’s html.parser&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;BeautifulSoup(markup,&lt;/span&gt; &lt;span&gt;"html.parser")&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Batteries included&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Decent speed&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Lenient (as of Python 2.7.3 and 3.2.)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Not very lenient (before Python 2.7.3 or 3.2.2)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;span&gt;lxml’s HTML parser&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;BeautifulSoup(markup,&lt;/span&gt; &lt;span&gt;"lxml")&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Very fast&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Lenient&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;External C dependency&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;span&gt;lxml’s XML parser&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span&gt;&lt;code&gt;&lt;span class="pre"&gt;BeautifulSoup(markup,&lt;/span&gt; &lt;span class="pre"&gt;"lxml-xml")&lt;/span&gt;&lt;/code&gt;&lt;code&gt;&lt;span class="pre"&gt;BeautifulSoup(markup,&lt;/span&gt; &lt;span class="pre"&gt;"xml")&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Very fast&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;The only currently supported XML parser&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;External C dependency&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;span&gt;html5lib&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;BeautifulSoup(markup,&lt;/span&gt; &lt;span&gt;"html5lib")&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Extremely lenient&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Parses pages the same way a web browser does&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Creates valid HTML5&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Very slow&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;External Python dependency&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
Other methods and Usage&lt;/h3&gt;

&lt;p&gt;Beautiful soup is a vast library and can do things which are too difficult in just a single line.&lt;br&gt;
Some of the methods for searching tags in HTML are:&lt;/p&gt;

&lt;pre&gt;
&lt;span&gt;#finding by ID&lt;/span&gt;

&lt;span&gt;soup.find(id='abc')&lt;/span&gt;

&lt;span&gt;
&lt;/span&gt;

&lt;span&gt;#finding through a regex&lt;/span&gt;

&lt;span&gt;#lmit the return to 2 tags&lt;/span&gt;

&lt;span&gt;soup.find_all(re.compile("^a"),limit=2)&lt;/span&gt;

&lt;span&gt;
&lt;/span&gt;

&lt;span&gt;#finding multiple tags&lt;/span&gt;

&lt;span&gt;soup.find_all(['a','h1'])&lt;/span&gt;

&lt;span&gt;
&lt;/span&gt;

&lt;span&gt;#fiind by custom or built in attributes&lt;/span&gt;

&lt;span&gt;soup.find_all(attrs={'data':'abc'})&lt;/span&gt;
&lt;/pre&gt;

&lt;h3&gt;
Problem 2:&lt;/h3&gt;

&lt;p&gt;&lt;b&gt;In the above example, we are using HTML string for parsing, now we will see how we can hit a URL and get the HTML for that page and then we can parse it in the same manner as we were doing for HTML string above&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;For this will be using urllib3 package of python. It can be easily installed by the following command&lt;/p&gt;

&lt;pre&gt;pip install urllib3&lt;/pre&gt;

&lt;p&gt;Documentation for urllib3 can be seen &lt;a href="https://urllib3.readthedocs.io/en/latest/user-guide.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;pre&gt;import urllib3
http = urllib3.PoolManager()
#hiitng the url 
r = http.request('GET', 'https://en.wikipedia.org/wiki/India')

#creating a soup object using html from the link
soup=BeautifulSoup(r.data,"html.parser")

#getting whole text from the wiki page
text=soup.text

#getting all the links from wiki page
links=soup.find_all('a')

#iterating over the new pages and getting text from them
#this can be done in a recursive fashion to parse large number of pages
for link in links:
    prihref=nt link.get('href')
    new_url='https://en.wikipedia.org'+href
    http = urllib3.PoolManager()
    r_new = http.request('GET', new_url)
    #do something with new page
    new_text=r_new.text


#getting source of all the images
src=soup.find('img').get('src')

&lt;/pre&gt;

&lt;p&gt;This was just a basic introduction to web scraping using Python. Much more can be achieved using the packages used in this tutorial. This article can serve as a starting point.&lt;/p&gt;

&lt;h2&gt;
Points to Remember&lt;/h2&gt;

&lt;p&gt;Web Scraping is very useful in gathering data for different purposes like data mining, knowledge creation, data analysis etc but it should be done with care.&lt;/p&gt;

&lt;p&gt;As a basic rule of thumb, we should not scrape anything which is paid content. Being said that we should comply with the robots.txt file of the site to know the areas which can be crawled.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;It is very important to look into the legal implications before scraping.&lt;/b&gt;&lt;br&gt;
&lt;b&gt;&lt;br&gt;
&lt;/b&gt; &lt;b&gt;&lt;/b&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
   Hope the article was informative.
&lt;/h2&gt;

&lt;p&gt;TechScouter (JSC)&lt;br&gt;
&lt;b&gt;&lt;br&gt;
&lt;/b&gt;&lt;/p&gt;



&lt;br&gt;

</description>
      <category>scraping</category>
      <category>mining</category>
      <category>analysis</category>
    </item>
    <item>
      <title>Topic Modeling</title>
      <dc:creator>Japneet Singh Chawla</dc:creator>
      <pubDate>Tue, 22 Aug 2017 04:11:25 +0000</pubDate>
      <link>https://dev.to/japneet121/topic-modelling</link>
      <guid>https://dev.to/japneet121/topic-modelling</guid>
      <description>&lt;p&gt;&lt;a href="https://2.bp.blogspot.com/-2R8o8TuByy0/WZupbHOH4iI/AAAAAAAABtw/mXSUjcGlp7wDJ9jRxAlvxF-f67v8HqsoQCLcBGAs/s1600/tm.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F2.bp.blogspot.com%2F-2R8o8TuByy0%2FWZupbHOH4iI%2FAAAAAAAABtw%2FmXSUjcGlp7wDJ9jRxAlvxF-f67v8HqsoQCLcBGAs%2Fs1600%2Ftm.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hi readers,&lt;/p&gt;

&lt;p&gt;In the previous post, I wrote about gaining the knowledge from the Text which is available from many sources. In this post, I will be writing about Topic Mining.&lt;/p&gt;

&lt;h2&gt;
Introduction&lt;/h2&gt;

&lt;p&gt;Topic Mining can be described as finding words from the group of words which can best describe the group.&lt;/p&gt;

&lt;p&gt;Textual Data in raw form is not associated with any context. A human can easily identify the context or topic for an article by reading the article and categorise it in one or other category like politics, sports, economics, crime etc.&lt;/p&gt;

&lt;p&gt;One of the factors any human will consider while classifying the text into one of the topics is the knowledge that how a word is associated with a topic e.g&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;India won Over Sri Lanka in the &lt;b&gt;test match&lt;/b&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
&lt;li&gt;World &lt;b&gt;Badminton &lt;/b&gt;Championships: When and where to watch &lt;b&gt;Kidambi Srikanth&lt;/b&gt;’s first round, live TV coverage, time in IST, live streaming  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here we may not find word sports explicitly in the sentences but the words marked in bold are associated with sports.&lt;/p&gt;

&lt;p&gt;Topic modelling can be broadly categorised into two type&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Rule-Based topic modelling&lt;/li&gt;
&lt;li&gt;Unsupervised topic modelling&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
Rule-Based Topic Modelling&lt;/h2&gt;

&lt;p&gt;As the name suggests rule-based topic modelling depends on the rules which can be used to associate a given text with some topic.&lt;/p&gt;

&lt;p&gt;In the simplest rule-based approach, we can just search for some words in the text and associate it with a topic e.g finding the word sports for associating the topic with sports, finding travelling for associating with topic travel&lt;/p&gt;

&lt;p&gt;This approach can be extended and a topic can be represented as a set of words with some given probabilities e.g For the category sports we can have a set of words with some weight assigned to each word.&lt;br&gt;
Topic : Sports{"sports":0.4,"cricket":0.1,"badminton":0.1 ,"&lt;b&gt;traveling&lt;/b&gt;":0.05 .....}&lt;br&gt;
Topic : Travel{"travel":0.4, "hiking":0.1,"train":0.05,"&lt;b&gt;traveling&lt;/b&gt;":0.20 .......}&lt;/p&gt;

&lt;p&gt;Notice the word  "travelling", it occurs in both the Topics but has different weight.&lt;/p&gt;

&lt;p&gt;If we have a sentence "Badminton players are travelling to UK  for the tournament", by the simple approach of finding the words for the topic then this sentence will go under the topic Travel. The second approach improves the prediction by checking the probabilities and weight for different words, in this case, "Badminton"  and "travelling" and improves the result by predicting the more accurate result that is Sports.&lt;/p&gt;

&lt;p&gt;The main disadvantage of the Rule-Based approach is that all the topics have to be known in the beginning and probabilities have to be determined and examined. This rules out the possibility of finding some new topic in the text corpus.&lt;/p&gt;

&lt;h2&gt;
Unsupervised Topic Modelling&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-g3dI_6STH5s/WZuqBxnrhgI/AAAAAAAABt8/3AGn9JJPL7EHtkNl06Gpms2uMXLxKqj3QCLcBGAs/s1600/lda.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F1.bp.blogspot.com%2F-g3dI_6STH5s%2FWZuqBxnrhgI%2FAAAAAAAABt8%2F3AGn9JJPL7EHtkNl06Gpms2uMXLxKqj3QCLcBGAs%2Fs640%2Flda.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The topic of a text sentence largely depends on the words used in the sentence and this property is exploited in unsupervised topic modelling technique to extract topics from the sentences.&lt;br&gt;
It largely relies on the Bayesian Inference Model.&lt;/p&gt;

&lt;h3&gt;
Bayesian Inference Model&lt;/h3&gt;

&lt;p&gt;It is a method by which we can calculate the probability of occurrence of some event-based on some common sense assumptions and the outcomes of previous related events.&lt;/p&gt;

&lt;p&gt;It also allows us to use new observations to improve the model, by going through many iterations where a prior probability is updated with the observational evidence in order to produce a new  and improved posterior probability&lt;/p&gt;

&lt;h3&gt;
Some of the techniques for Unsupervised Topic Modelling are:&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;TF-IDF&lt;/li&gt;
&lt;li&gt;Latent Semantic Indexing&lt;/li&gt;
&lt;li&gt;Latent Dirichlet Allocation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will not discuss the techniques in detail rather will focus on the implementation of these in python.&lt;br&gt;
All the approaches use the vector space representation of the documents. In vector space model a document is represented by a document-term matrix.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://3.bp.blogspot.com/-MF9AfD1OzdE/WZphHhbDM0I/AAAAAAAABtg/0YAaHqg6-H4EtrE4zA_2TBMHKxQiQzrHgCLcBGAs/s1600/Modeling2.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F3.bp.blogspot.com%2F-MF9AfD1OzdE%2FWZphHhbDM0I%2FAAAAAAAABtg%2F0YAaHqg6-H4EtrE4zA_2TBMHKxQiQzrHgCLcBGAs%2Fs1600%2FModeling2.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here D1,D2,D3...Dn are different documents and W1,W2,W3....Wn are the words that belong to our dictionary(all unique words in the corpus). DxWy gives the number of time the word has occurred in the document.&lt;/p&gt;

&lt;pre&gt;


&lt;span&gt;#importing corpora from gensim for creating dictionary&lt;/span&gt;

&lt;span&gt;from gensim import corpora&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;#some raw documents .These documents can be stored in a file and read from the file&lt;/span&gt;

&lt;span&gt;documents = ["Two analysts have provided estimates for Tower Semiconductor's earnings. The stock of Tower Semiconductor Ltd. (USA) (NASDAQ:TSEM) has \"Buy\" rating given on Friday, June 9 by Needham. The company's stock had a trading volume of 573,163 shares.",&lt;/span&gt;

&lt;span&gt;             "More news for Tower Semiconductor Ltd. (USA) (NASDAQ:TSEM) were recently published by: Benzinga.com, which released: “15 Biggest Mid-Day Gainers For Monday” on August 14, 2017.",&lt;/span&gt;

&lt;span&gt;             "Tower Semiconductor Ltd now has $2.33 billion valuation. The stock decreased 1.61% or $0.41 during the last trading session, reaching $25.06.",&lt;/span&gt;

&lt;span&gt;             "The investment company owned 74,300 stocks of the industrial products firms shares after disposing 5,700 shares through out the quarter.",              &lt;/span&gt;

&lt;span&gt;             "Tower Semiconductor Ltd now has $2.37B valuation. The stock rose 0.16% or $0.04 reaching $25.1 per share. About 115,031 shares traded.",&lt;/span&gt;

&lt;span&gt;             "Needle moving action has been spotted in Steven Madden Ltd (SHOO) as shares are moving today on volatility -2.00% or -0.85 from the open.",&lt;/span&gt;

&lt;span&gt;             "Shares of Steven Madden Ltd (SHOO) have seen the needle move -1.20% or -0.50 in the most recent session. The NASDAQ listed company saw a recent bid of 41.10 on 82437 volume.",&lt;/span&gt;

&lt;span&gt;             "Shares of Steven Madden Ltd (SHOO) is moving on volatility today -1.37% or -0.57 from the open. The NASDAQ listed company saw a recent bid of 41.03 on 279437 volume.",&lt;/span&gt;

&lt;span&gt;             "Shares of Steven Madden, Ltd. (NASDAQ:SHOO) are on a recovery track as they have regained 28.79% since bottoming out at $32.3 on Oct. 26, 2016."]&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;'''&lt;/span&gt;

&lt;span&gt;    Text Preprocessiing before calculation vectors&lt;/span&gt;

&lt;span&gt;'''&lt;/span&gt;

&lt;span&gt;# remove common words(stop words) and tokenize&lt;/span&gt;

&lt;span&gt;stoplist = set('for a of the and to in its by his has have on were was which or as they since'.split())&lt;/span&gt;

&lt;span&gt;texts = [[word for word in document.lower().split() if word not in stoplist]&lt;/span&gt;

&lt;span&gt;         for document in documents]&lt;/span&gt;



&lt;span&gt;&lt;/span&gt;

&lt;span&gt;# remove words that appear only once in the corpus.&lt;/span&gt;

&lt;span&gt;from collections import defaultdict&lt;/span&gt;

&lt;span&gt;frequency = defaultdict(int)&lt;/span&gt;

&lt;span&gt;for text in texts:&lt;/span&gt;

&lt;span&gt;    for token in text:&lt;/span&gt;

&lt;span&gt;        frequency[token] += 1&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;texts = [[token for token in text if frequency[token] &amp;gt; 1] for text in texts]&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;from pprint import pprint  # pretty-printer&lt;/span&gt;

&lt;span&gt;pprint(texts)&lt;/span&gt;

&lt;span&gt;'''&lt;/span&gt;

&lt;span&gt;dictionary is collection of all the words that apprear in our corpus &lt;/span&gt;

&lt;span&gt;'''&lt;/span&gt;

&lt;span&gt;#creating dictionary from the corpus which represents each word in the text corpus as a unique integer&lt;/span&gt;

&lt;span&gt;dictionary = corpora.Dictionary(texts)&lt;/span&gt;

&lt;span&gt;dictionary.save('/tmp/deerwester.dict')  # store the dictionary, for future reference&lt;/span&gt;

&lt;span&gt;#saved dictionary can be loaded from the memory for future references.Saves time for recreation of dictionary&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;#to print the token id of the words&lt;/span&gt;

&lt;span&gt;print(dictionary.token2id)&lt;/span&gt;

&lt;span&gt;'''&lt;/span&gt;

&lt;span&gt;We can convert any document to vector format by just calling the doc2bow() method of dictionary object.&lt;/span&gt;

&lt;span&gt;document="hello this is a good morning"&lt;/span&gt;

&lt;span&gt;Input=['hello','this','is','a','good','morning']&lt;/span&gt;

&lt;span&gt;preprocessing can be done on the document before passing to doc2bow()&lt;/span&gt;

&lt;span&gt;'''&lt;/span&gt;

&lt;span&gt;corpus = [dictionary.doc2bow(text) for text in texts]&lt;/span&gt;

&lt;span&gt;print corpus&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;'''&lt;/span&gt;

&lt;span&gt;creating vector space representation for a single document&lt;/span&gt;

&lt;span&gt;str="human interaction interface interface"&lt;/span&gt;

&lt;span&gt;dictionary.doc2bow(text for text in str.split(' '))&lt;/span&gt;

&lt;span&gt;'''&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;from gensim import corpora, models, similarities&lt;/span&gt;



&lt;span&gt;#convertig the vector space representation to tf-idf transformation&lt;/span&gt;

&lt;span&gt;tfidf = models.TfidfModel(corpus)&lt;/span&gt;

&lt;span&gt;list(tfidf[corpus])&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;#using LDA model for getting the topics&lt;/span&gt;

&lt;span&gt;lda= models.LdaModel(corpus, id2word=dictionary, num_topics=2) # initialize an LDA transformation&lt;/span&gt;

&lt;span&gt;corpus_lda= lda[corpus] # create a double wrapper over the original corpus: bow-&amp;gt;tfidf-&amp;gt;fold-in-lda&lt;/span&gt;

&lt;span&gt;print lda.print_topics(2) #print the topics&lt;/span&gt;
&lt;span&gt;
&lt;/span&gt;
&lt;span&gt;#for classifying the text documents into topics &lt;/span&gt;
&lt;span&gt;#here I have used the old corpus on which it was trained ,one can also use new documents &lt;/span&gt;

&lt;span&gt;for doc in corpus_lda:&lt;/span&gt;

&lt;span&gt;    print doc&lt;/span&gt;

&lt;span&gt;    &lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;Output:&lt;/span&gt;

&lt;pre&gt;[&lt;/pre&gt;
&lt;pre&gt;(0, u'0.069*"shares" + 0.062*"ltd" + 0.059*"steven" + 0.044*"madden" + 0.044*"tower" + 0.043*"moving" + 0.043*"recent" + 0.041*"stock" + 0.040*"(shoo)" + 0.036*"semiconductor"'),&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;(1, u'0.077*"shares" + 0.066*"tower" + 0.056*"semiconductor" + 0.050*"stock" + 0.048*"company" + 0.046*"ltd" + 0.045*"ltd." + 0.043*"(usa)" + 0.038*"(nasdaq:tsem)" + 0.035*"trading"')]&lt;/pre&gt;


&lt;span&gt;&lt;/span&gt;

&lt;span&gt;#the output gives the topics along with there probability distribution for different words&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;

&lt;span&gt;&lt;/span&gt;


&lt;/pre&gt;

&lt;p&gt;Parameters of LdaModel can be tweaked for result improvements.Also by just replacing LdaModel by LsiModel one can use LSI technique&lt;/p&gt;



&lt;br&gt;

</description>
      <category>textanalysis</category>
      <category>analysis</category>
      <category>topicmining</category>
      <category>textmining</category>
    </item>
    <item>
      <title>Machine Learning-A Problem or Solution</title>
      <dc:creator>Japneet Singh Chawla</dc:creator>
      <pubDate>Thu, 17 Aug 2017 17:39:30 +0000</pubDate>
      <link>https://dev.to/japneet121/machine-learning-a-problem-or-solution</link>
      <guid>https://dev.to/japneet121/machine-learning-a-problem-or-solution</guid>
      <description>&lt;h3&gt;
The article will be divided into different sections as follows:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Introduction to Machine Learning
&lt;/li&gt;
&lt;li&gt;Types of Solutions&lt;/li&gt;
&lt;li&gt;Classification using Naive Bayes&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
&lt;span id="intro"&gt;&lt;b&gt;A brief about Machine Learning&lt;/b&gt;&lt;/span&gt;
&lt;/h2&gt;


According to the definition by Wikipedia,&lt;b&gt; Machine learning&lt;/b&gt;&lt;span&gt; is the subfield of &lt;/span&gt;&lt;a href="https://en.wikipedia.org/wiki/Computer_science" title="Computer science"&gt;computer science&lt;/a&gt;&lt;span&gt; that, according to &lt;/span&gt;&lt;a href="https://en.wikipedia.org/wiki/Arthur_Samuel" title="Arthur Samuel"&gt;Arthur Samuel&lt;/a&gt;&lt;span&gt; in 1959, gives "computers the ability to learn without being explicitly programmed." &lt;/span&gt;

&lt;span&gt;Machine Learning defines a set of problems that have to be evolved through the data by implying some algorithm.&lt;/span&gt;
&lt;span&gt; One factor that has to be kept in mind while defining a solution through ML is accuracy. Accuracy is very critical in case you are developing a solution in medical domain(cancer detection).There should be a threshold set for every solution which can be based on risk %age that is acceptable.&lt;/span&gt;

&lt;h3&gt;
&lt;span&gt;A useful cheatsheet from Microsoft's site to sum up the use of different ML algorithms for the different type of problems.&lt;/span&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://2.bp.blogspot.com/-xf-9_wVJzc0/WUZw8z43-NI/AAAAAAAABlQ/bvC5rZblonUuwBrS36gE7NCikZ2etcu8gCLcBGAs/s1600/ML_problems.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7bGabkd8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://2.bp.blogspot.com/-xf-9_wVJzc0/WUZw8z43-NI/AAAAAAAABlQ/bvC5rZblonUuwBrS36gE7NCikZ2etcu8gCLcBGAs/s1600/ML_problems.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
Types of solution&lt;/h2&gt;

Machine Learning solutions can be broadly classified into three types 

&lt;h3&gt;
&lt;ul&gt;
&lt;li&gt;Supervised Learning&lt;/li&gt;
&lt;li&gt;Unsupervised Learning&lt;/li&gt;
&lt;li&gt;Reinforcement Learning&lt;/li&gt;
&lt;/ul&gt;

Supervised Learning&lt;span&gt; is a category in which a machine is given training on some labelled data and is informed about the problem that is to be solved.The learnt model then uses its knowledge to solve the future similar problems.In this type of learner, a major requirement is the availability of labelled data.An Example of this kind of problems can be an object identification problem.&lt;/span&gt;

&lt;span&gt;First, a model is trained and told about the different types of objects, and then it is asked to identify the objects.&lt;/span&gt;





Unsupervised Learning&lt;span&gt;, on the other hand, is a category in which a learner is not provided with any input i.e labelled data. They use their algorithm and start working with problem and make some sense out of the problem on their own. Initially they may not provide much accuracy but they will eventually learn to provide accurate results.We can think about this learner as a person who went to some foreign country without any prior knowledge of the language spoken there.He learns from his experience how people greet each other, how to say hi etc.&lt;/span&gt;

&lt;span&gt;
&lt;/span&gt;

Reinforcement Learning &lt;span&gt;is a category in which a machine learns by making some predictions and is penalised for making a wrong prediction, due to this penalty machine learns not to make this mistake again.Take an example of a driverless robot that crawls on a road with obstacles, every time it see's an object and bumps into the object, it is penalised for its prediction of not taking appropriate action. Next time robot see's the same kind of obstacle it knows from the past experience and avoids the same mistake again.&lt;/span&gt;

&lt;span&gt;
&lt;/span&gt;
&lt;/h3&gt;
&lt;h2&gt;
&lt;span&gt;Machine Learning for Classification problem&lt;/span&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;span&gt;Classification is set of machine learning problems in which an input is classified into different classes which can be either supervised or unsupervised.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-bFNOwGlrJLI/WUaVHAfs15I/AAAAAAAABlg/-S2D8WFACOQEmwF9z-DoJYtUHSuUDWtvwCLcBGAs/s1600/classification.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LtwSqVc4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-bFNOwGlrJLI/WUaVHAfs15I/AAAAAAAABlg/-S2D8WFACOQEmwF9z-DoJYtUHSuUDWtvwCLcBGAs/s1600/classification.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Examples of classification problems:&lt;/span&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Sentiment Analysis&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Product Categorization&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Binary Classification on reviews(pos, neg) and much more&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
Movie Review Classification using Naive Bayes Algorithm&lt;/h2&gt;

&lt;p&gt;&lt;span&gt;I will be using nltk i.e is Natural Language Processing Toolkit available in python and movie review corpus that has labelled data for movie reviews classified as positive and negative.&lt;/span&gt;&lt;br&gt;
&lt;span&gt;&lt;br&gt;
&lt;/span&gt; &lt;span&gt;&lt;/span&gt;&lt;/p&gt;

&lt;pre&gt;
&lt;code&gt;import&lt;/code&gt; &lt;code&gt;nltk.classify.util&lt;/code&gt;

&lt;code&gt;from&lt;/code&gt; &lt;code&gt;nltk.classify &lt;/code&gt;&lt;code&gt;import&lt;/code&gt; &lt;code&gt;NaiveBayesClassifier&lt;/code&gt;

&lt;code&gt;from&lt;/code&gt; &lt;code&gt;nltk.corpus &lt;/code&gt;&lt;code&gt;import&lt;/code&gt; &lt;code&gt;movie_reviews&lt;/code&gt;




&lt;span&gt;#word_feats will convert the sentence into features&lt;/span&gt;

&lt;code&gt;def&lt;/code&gt; &lt;code&gt;word_feats(words):&lt;/code&gt;

&lt;code&gt;    &lt;/code&gt;&lt;code&gt;return&lt;/code&gt; &lt;code&gt;dict&lt;/code&gt;&lt;code&gt;([(word, &lt;/code&gt;&lt;code&gt;True&lt;/code&gt;&lt;code&gt;) &lt;/code&gt;&lt;code&gt;for&lt;/code&gt; &lt;code&gt;word &lt;/code&gt;&lt;code&gt;in&lt;/code&gt; &lt;code&gt;words])&lt;/code&gt;




&lt;code&gt;negids &lt;/code&gt;&lt;code&gt;=&lt;/code&gt; &lt;code&gt;movie_reviews.fileids(&lt;/code&gt;&lt;code&gt;'neg'&lt;/code&gt;&lt;code&gt;)&lt;/code&gt;

&lt;code&gt;posids &lt;/code&gt;&lt;code&gt;=&lt;/code&gt; &lt;code&gt;movie_reviews.fileids(&lt;/code&gt;&lt;code&gt;'pos'&lt;/code&gt;&lt;code&gt;)&lt;/code&gt;

&lt;code&gt;&lt;span&gt;
&lt;/span&gt;&lt;/code&gt;

&lt;span&gt;#training data is converted into features&lt;/span&gt;

&lt;code&gt;negfeats &lt;/code&gt;&lt;code&gt;=&lt;/code&gt; &lt;code&gt;[(word_feats(movie_reviews.words(fileids&lt;/code&gt;&lt;code&gt;=&lt;/code&gt;&lt;code&gt;[f])), &lt;/code&gt;&lt;code&gt;'neg'&lt;/code&gt;&lt;code&gt;) &lt;/code&gt;&lt;code&gt;for&lt;/code&gt; &lt;code&gt;f &lt;/code&gt;&lt;code&gt;in&lt;/code&gt; &lt;code&gt;negids]&lt;/code&gt;

&lt;code&gt;posfeats &lt;/code&gt;&lt;code&gt;=&lt;/code&gt; &lt;code&gt;[(word_feats(movie_reviews.words(fileids&lt;/code&gt;&lt;code&gt;=&lt;/code&gt;&lt;code&gt;[f])), &lt;/code&gt;&lt;code&gt;'pos'&lt;/code&gt;&lt;code&gt;) &lt;/code&gt;&lt;code&gt;for&lt;/code&gt; &lt;code&gt;f &lt;/code&gt;&lt;code&gt;in&lt;/code&gt; &lt;code&gt;posids]&lt;/code&gt;

&lt;span&gt;
&lt;/span&gt;

&lt;span&gt;#data is divided into training and testing data&lt;/span&gt;

&lt;code&gt;negcutoff &lt;/code&gt;&lt;code&gt;=&lt;/code&gt; &lt;code&gt;len&lt;/code&gt;&lt;code&gt;(negfeats)&lt;/code&gt;&lt;code&gt;*&lt;/code&gt;&lt;code&gt;3&lt;/code&gt;&lt;code&gt;/&lt;/code&gt;&lt;code&gt;4&lt;/code&gt;

&lt;code&gt;poscutoff &lt;/code&gt;&lt;code&gt;=&lt;/code&gt; &lt;code&gt;len&lt;/code&gt;&lt;code&gt;(posfeats)&lt;/code&gt;&lt;code&gt;*&lt;/code&gt;&lt;code&gt;3&lt;/code&gt;&lt;code&gt;/&lt;/code&gt;&lt;code&gt;4&lt;/code&gt;




&lt;code&gt;trainfeats &lt;/code&gt;&lt;code&gt;=&lt;/code&gt; &lt;code&gt;negfeats[:negcutoff] &lt;/code&gt;&lt;code&gt;+&lt;/code&gt; &lt;code&gt;posfeats[:poscutoff]&lt;/code&gt;

&lt;code&gt;testfeats &lt;/code&gt;&lt;code&gt;=&lt;/code&gt; &lt;code&gt;negfeats[negcutoff:] &lt;/code&gt;&lt;code&gt;+&lt;/code&gt; &lt;code&gt;posfeats[poscutoff:]&lt;/code&gt;

&lt;code&gt;print&lt;/code&gt; &lt;code&gt;'train on %d instances, test on %d instances'&lt;/code&gt; &lt;code&gt;%&lt;/code&gt; &lt;code&gt;(&lt;/code&gt;&lt;code&gt;len&lt;/code&gt;&lt;code&gt;(trainfeats), &lt;/code&gt;&lt;code&gt;len&lt;/code&gt;&lt;code&gt;(testfeats))&lt;/code&gt;

&lt;code&gt;&lt;span&gt;
&lt;/span&gt;&lt;/code&gt;

&lt;span&gt;&lt;b&gt;#naivebayes classifier used for training on training data&lt;/b&gt;&lt;/span&gt;

&lt;code&gt;classifier &lt;/code&gt;&lt;code&gt;=&lt;/code&gt; &lt;code&gt;NaiveBayesClassifier.train(trainfeats)&lt;/code&gt;

&lt;code&gt;print&lt;/code&gt; &lt;code&gt;'accuracy:'&lt;/code&gt;&lt;code&gt;, nltk.classify.util.accuracy(classifier, testfeats)&lt;/code&gt;


&lt;code&gt;classifier.show_most_informative_features()&lt;/code&gt;
&lt;code&gt;print&lt;/code&gt;&lt;span&gt; &lt;/span&gt;&lt;code&gt;'classified as:'&lt;/code&gt;&lt;code&gt;,classifier.classify("The plot was good,but the characters were not compelling")&lt;/code&gt;
&lt;/pre&gt;



&lt;br&gt;&lt;br&gt;
&lt;br&gt;

</description>
      <category>machinelearning</category>
      <category>classification</category>
    </item>
    <item>
      <title>Sentiment Analysis</title>
      <dc:creator>Japneet Singh Chawla</dc:creator>
      <pubDate>Sun, 13 Aug 2017 05:42:15 +0000</pubDate>
      <link>https://dev.to/japneet121/sentiment-analysis</link>
      <guid>https://dev.to/japneet121/sentiment-analysis</guid>
      <description>&lt;p&gt;&lt;a href="https://3.bp.blogspot.com/-4p-ndHTS-gg/WWJ4aq9VRlI/AAAAAAAABmg/Y5fnLzIReqkw6N78qTKyoFBH9b0jpsvHACLcBGAs/s1600/sentiment.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F3.bp.blogspot.com%2F-4p-ndHTS-gg%2FWWJ4aq9VRlI%2FAAAAAAAABmg%2FY5fnLzIReqkw6N78qTKyoFBH9b0jpsvHACLcBGAs%2Fs1600%2Fsentiment.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This one took long due to the Analysis work I was doing for this post.There is a lot of work going on in the subject of Sentiment analysis so I decided to compare the accuracy of the products. Let's start with some basics...&lt;/p&gt;

&lt;h2&gt;
NLP: Natural Language Processing&lt;/h2&gt;

&lt;p&gt;Natural Language Processing is a very interesting topic and a subject of debate when it comes to accuracy of the NLP.&lt;/p&gt;

&lt;p&gt;Natural Language is very ambiguous as same sentences can have different meanings like&lt;br&gt;
&lt;span&gt;&lt;br&gt;
&lt;/span&gt; &lt;span&gt;"I saw a man on a hill with a telescope. "&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;It seems like a simple statement until you begin to unpack the many alternate meanings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There’s a man on a hill, and I’m watching him with my telescope.&lt;/li&gt;
&lt;li&gt;There’s a man on a hill, who I’m seeing, and he has a telescope.&lt;/li&gt;
&lt;li&gt;There’s a man, and he’s on a hill that also has a telescope on it.&lt;/li&gt;
&lt;li&gt;I’m on a hill, and I saw a man using a telescope.&lt;/li&gt;
&lt;li&gt;There’s a man on a hill, and I’m seeing him with a telescope.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sarcasm is that component of the language that is difficult for quite a few humans to understand.&lt;/p&gt;

&lt;h3&gt;
&lt;span&gt;Phrase:&lt;/span&gt;&lt;span&gt; "Great job."&lt;/span&gt;
&lt;/h3&gt;

&lt;h3&gt;
&lt;span&gt;Normally means:&lt;/span&gt;&lt;span&gt;&lt;span&gt; You have created something of value here.&lt;/span&gt;
Sarcastic way means:&lt;/span&gt;&lt;span&gt; You have created nothing of value here.&lt;/span&gt;
&lt;span&gt;
&lt;/span&gt; &lt;span&gt;Also every day there are some new slangs added in the vocabulary like ROFL, LOL, NaMo, BrExit etc...&lt;/span&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;span&gt;The presence of all these components in the language makes it much more difficult to process it.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;br&gt;
&lt;/span&gt;&lt;/p&gt;

&lt;h2&gt;
Sentiment Analysis&lt;/h2&gt;

&lt;p&gt;It is a sub-domain of NLP which is basically a binary classification problem[To know more about ML and classification check my post&lt;b&gt; [&lt;a href="http://techscouter.blogspot.in/2017/06/machine-learning-solution-or-problem.html" rel="noopener noreferrer"&gt;Machine Learning -A Solution or Problem&lt;/a&gt;&lt;/b&gt;]&lt;/p&gt;

&lt;p&gt;The problem is to determine the polarity i.e the negativity or positivity of a sentence by processing the sentence and applying some algorithm to it.&lt;/p&gt;

&lt;p&gt;One of the naive approach for calculating the polarity of the sentence is Bag of Words Technique.&lt;/p&gt;

&lt;p&gt;In this technique, a sentence is simply broken into words and these words are then mapped to a dictionary which contains words and the polarity of the corresponding word.The total polarity of a sentence is calculated by summing up the individual words. An example of such dictionary is &lt;a href="http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6010" rel="noopener noreferrer"&gt;AFINN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;br&gt;
&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span&gt;But this is pretty basic approach and works only for very simple sentences and not effective for complex language. There are many other algorithms like SVM, Maximum Entropy etc which calculate the polarity by form a relationship between the words.&lt;/span&gt;&lt;/p&gt;

&lt;h2&gt;
Twitter Sentiment Analysis&lt;/h2&gt;

&lt;p&gt;Twitter is a very popular social network where information spreads like a fire and reaches millions of users within seconds. Twitter sentiment analysis is a subject in which the Twitter tweets are analysed for calculating polarities and then a social notion about a topic is estimated.&lt;/p&gt;

&lt;p&gt;There are many products in the market that claim to provide market analysis based on the Twitter data.&lt;br&gt;
A big question on this type of  analysis is that if it is difficult to understand the natural language and even after too much research its accuracy in sentiment analysis is at most 85%, how difficult it is to classify a twitter tweet into positive or negative tweet by just 160 characters, also these tweets have lots of jargons and slangs.&lt;/p&gt;

&lt;p&gt;To explore this I took a set of 100 tweets and ran sentiment analysis through three models&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;IBM Watson&lt;/li&gt;
&lt;li&gt;CoreNLP by Stanford&lt;/li&gt;
&lt;li&gt;Microsoft Cognitive Service&lt;/li&gt;
&lt;li&gt;Manual Tagging&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There was a maximum accuracy of 50% and all the models nearly performed equally well.&lt;br&gt;
The results can be seen &lt;a href="https://docs.google.com/spreadsheets/d/1c-QD_t3vpPWG-BNTr2xBXZUfZDaHi873MKbaQASnusQ/edit?usp=sharing" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
One thing that was clear from this analysis is that&lt;/p&gt;

&lt;p&gt;&lt;span&gt;"Either we can not depend on Twitter's tweet data for Market Analysis and Predictions through Sentiment Analysis or we need a more sophisticated system to do the analysis."&lt;/span&gt;&lt;/p&gt;



</description>
      <category>machinelearning</category>
      <category>classification</category>
    </item>
    <item>
      <title>Text Analytics</title>
      <dc:creator>Japneet Singh Chawla</dc:creator>
      <pubDate>Sat, 12 Aug 2017 23:35:09 +0000</pubDate>
      <link>https://dev.to/japneet121/text-analytics</link>
      <guid>https://dev.to/japneet121/text-analytics</guid>
      <description>&lt;p&gt;&lt;a href="https://1.bp.blogspot.com/-b6oEfHpnNZo/WYbl77A-x2I/AAAAAAAABps/CemAjXZhbDUdcuPVcibYL3pVujMLUtn9wCEwYBhgL/s1600/text-mining.jpg" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F1.bp.blogspot.com%2F-b6oEfHpnNZo%2FWYbl77A-x2I%2FAAAAAAAABps%2FCemAjXZhbDUdcuPVcibYL3pVujMLUtn9wCEwYBhgL%2Fs1600%2Ftext-mining.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hi Readers,&lt;br&gt;
Recently I was going through some text analytics activities at work and learned some techniques for text analytics and mining.&lt;/p&gt;

&lt;p&gt;In this series of posts, I will be sharing my experiences and learnings.&lt;/p&gt;

&lt;h2&gt;
Introduction&lt;/h2&gt;

&lt;p&gt;Firstly the jargon Text Analysis and Text Mining are sub domains of the term Data Mining and are used interchangeably in most scenarios.&lt;br&gt;
Broadly &lt;b&gt;Text Analysis&lt;/b&gt; refers to,&lt;br&gt;
Extracting information from textual data keeping the problem for which we want to get data in mind.&lt;br&gt;
and &lt;b&gt;Text Mining&lt;/b&gt; refers to,&lt;br&gt;
the process of getting textual data.&lt;/p&gt;

&lt;p&gt;Nowadays, a large quantity of data is produced by humans.Data is growing faster than ever before and by the year 2020, about  1.7 megabytes of new information will be created every second for every human being on the planet and one of the main components of this data will be textual data.&lt;br&gt;
Some of the main sources of textual data are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blogs&lt;/li&gt;
&lt;li&gt;Articles&lt;/li&gt;
&lt;li&gt;Websites&lt;/li&gt;
&lt;li&gt;Facebook&lt;/li&gt;
&lt;li&gt;Comments&lt;/li&gt;
&lt;li&gt;Discussion Forums&lt;/li&gt;
&lt;li&gt;Reviews&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://2.bp.blogspot.com/-jgpbpNjeirM/WYbozkEV7wI/AAAAAAAABp4/R_6bOwPd2MUv34iObsXsf83IUBjZdoIlQCLcBGAs/s1600/observer.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2F2.bp.blogspot.com%2F-jgpbpNjeirM%2FWYbozkEV7wI%2FAAAAAAAABp4%2FR_6bOwPd2MUv34iObsXsf83IUBjZdoIlQCLcBGAs%2Fs1600%2Fobserver.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the scenario described in the image above every observer may perceive the real world in a different manner and express accordingly e.g different persons may have a different opinion on some topic.&lt;br&gt;
So, while analysing textual data this type of bias ness has to be kept in mind.&lt;/p&gt;

&lt;h4&gt;
Type of information that can be extracted from the text and converted into actionable knowledge can be&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Mining content of data&lt;/li&gt;
&lt;li&gt;Mining knowledge about the language&lt;/li&gt;
&lt;li&gt;Mining knowledge about the observer&lt;/li&gt;
&lt;li&gt;Infer other real world variables&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
NLP&lt;/h2&gt;

&lt;p&gt;NLP stands for natural language processing, a subdomain of Machine Learning that deals with processing of natural language spoken by humans and make it understandable for a machine.&lt;/p&gt;

&lt;p&gt;It is a fairly complex problem as understanding natural language involves common sense which machines lack. Many of the sentences spoken by humans involve common sense and are spoken in some context which derives the meaning of the sentence.E.g&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;He is an apple of my eye.(Phrase)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;John is the star.(Movie star or celestial body) &lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;

Analyzing the Natural language can be broadly classified into 4 types of analysis&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Lexical Analysis-Identifying Parts of Speech, Word Associations, Topic Mining&lt;/li&gt;
&lt;li&gt;Syntactical Analysis-Connecting words and creating phrases and then connecting phrases&lt;/li&gt;
&lt;li&gt;Semantical Analysis-Extracting knowledge about the sentence&lt;/li&gt;
&lt;li&gt;Pragmatic Analysis-Getting the intent of the  sentence&lt;/li&gt;
&lt;/ol&gt;



&lt;br&gt;&lt;br&gt;
&lt;br&gt;

&lt;h2&gt;
Lexical Analysis&lt;/h2&gt;

&lt;p&gt;By the definition lexical analysis means converting of a sequence of characters into the sequence of words with some defined meanings.&lt;/p&gt;

&lt;p&gt;Definitions of words can be&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;POS Tags(Nouns,Verbs,Adverbs etc)&lt;/li&gt;
&lt;li&gt;Word Associations(How two words are linked)&lt;/li&gt;
&lt;li&gt;Topic(Context of the Paragraph)&lt;/li&gt;
&lt;li&gt;Syntagmatic words &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, I will be writing about &lt;b&gt;Word Associations &lt;/b&gt;which is the analysis of how different words are linked to each other. Look at the following sentences&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; "Jack drives a car", "car" is associated to "drive"&lt;/li&gt;
&lt;li&gt;"I love cats"&lt;/li&gt;
&lt;li&gt;"I love dogs", In these sentences, the word cats is related to dogs as they can be used interchangeably to make a valid sentence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the first sentence  "Jack drives a car", the type of relationship between "car " and "drive" is called&lt;br&gt;
&lt;b&gt;&lt;br&gt;
&lt;/b&gt; &lt;b&gt;Syntagmatic Relation&lt;/b&gt;. &lt;/p&gt;

&lt;p&gt;Two words are called syntagmatically related if the probability of both the words occurring in the same sentence is high. E.g&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bob eats food&lt;/li&gt;
&lt;li&gt;John drives a car&lt;/li&gt;
&lt;li&gt;He sits on a sofa&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These types of relations are important to find the probability of a word occurring in a sentence based on another word which is present in the sentence.&lt;/p&gt;

&lt;p&gt;This problem can be mathematically reduced to Predicting a random variable v, where v=1 if the word is present in the sentence or v-0 if the word is not present in the sentence.&lt;/p&gt;

&lt;p&gt;The more random this variable is, the more difficult it is to predict the variable. For e.g probability of occurring the in a sentence when some other word has occurred in the sentence can be easily predicted as "the" is a pretty common word, on the other hand, probability of occurring of word "eat" in a sentence is low and it's difficult to predict the probability of its occurrence.&lt;/p&gt;

&lt;p&gt;The randomness of a random variable can be measured by &lt;b&gt;Entropy.&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;br&gt;
&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;H(Xw)=âˆ‘-P(Xw=V)logP(Xw=V)&lt;/b&gt;&lt;br&gt;
&lt;b&gt;(vÎµ{0,1})&lt;/b&gt;&lt;br&gt;
&lt;b&gt;&lt;br&gt;
&lt;/b&gt; X sub w is the probability that the word w is present in the sentence&lt;br&gt;
H(Xw) is the entropy of the variable X sub w&lt;br&gt;
P(Xw=V) is the probability that the variable exists(V=1) and vice-versa(V=0)&lt;br&gt;
logP(Xw=V) is the natural log of the probability that a variable exists(V=1) and vice-versa (V=0)&lt;br&gt;
&lt;b&gt;&lt;br&gt;
&lt;/b&gt; "Higher the entropy more difficult it is to predict the syntagmatic relation"&lt;/p&gt;

&lt;p&gt;Now that we know the concept of entropy and how it can be used to predict the existence of a word in a sentence, let's introduce one more concept called &lt;b&gt;Conditional Entropy.&lt;/b&gt;&lt;br&gt;
It is defined as the entropy of a word W when it is known that V has occurred in the sentence.&lt;br&gt;
This helps to reduce the entropy which in turn reduces the randomness of the random variable.&lt;br&gt;
Conditional Entropy can be defined as&lt;/p&gt;

&lt;p&gt;&lt;b&gt;H(Xmeat|Xeats =1)=-P(&lt;/b&gt;&lt;b&gt;Xmeat=0 | Xeats =1&lt;/b&gt;&lt;b&gt;)logP(&lt;/b&gt;&lt;b&gt;Xmeat=0 | Xeats =1&lt;/b&gt;&lt;b&gt;) &lt;/b&gt;&lt;br&gt;
&lt;b&gt;                                    - P(&lt;/b&gt;&lt;b&gt;Xmeat=1 | Xeats =1&lt;/b&gt;&lt;b&gt;)logP(&lt;/b&gt;&lt;b&gt;Xmeat=1 | Xeats =1)&lt;/b&gt;&lt;br&gt;
&lt;b&gt;&lt;br&gt;
&lt;/b&gt; This defines the probability of meat occurring when eats has already occurred.                                           &lt;/p&gt;

&lt;h3&gt;
Paradigmatic Relations&lt;/h3&gt;

&lt;p&gt;The other type of relationship that can be found between the words is the paradigmatic relation.Two words are said to be paradigmatically related if they can be replaced with each other and still make a valid sentence.E.g&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I love cats&lt;/li&gt;
&lt;li&gt;I love dogs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the first sentence if  "cats" is replaced with dogs it still makes a valid sentence, that means cats and dogs are paradigmatically related.&lt;/p&gt;

&lt;p&gt;In order to find these type of relationships in the sentences we first need to find the &lt;b&gt;Left Context, &lt;/b&gt;which means the words that appear left to the words and then we need to find the &lt;b&gt;Right Context, &lt;/b&gt;which means the words that appear to the right of the sentences.&lt;/p&gt;

&lt;p&gt;Once we have these context we can find the context similarities.Words with more context similarities are more paradigmatically related.This problem can be mathematically defined by representing each word in the context as a vector, where the value of vector is represented by the frequency of the occurrences of that word in the context&lt;/p&gt;

&lt;p&gt;Now the two contexts can be represented as &lt;/p&gt;

&lt;p&gt;d1=(x1,x2,x3,x4,........xn)&lt;/p&gt;

&lt;p&gt;d1=c(wi,d1) / |d1|&lt;/p&gt;

&lt;p&gt;c(wi,d1) is the count of word wi in d1 and,&lt;/p&gt;

&lt;p&gt;|d1| is the number of words in d1&lt;/p&gt;

&lt;p&gt;d2=(x1,x2,x3,x4,........xn)&lt;/p&gt;

&lt;p&gt;d2=c(wi,d2) / |d2|&lt;/p&gt;

&lt;p&gt;c(wi,d2) is the count of word wi in d2 and,&lt;/p&gt;

&lt;p&gt;|d2| is the number of words in d2&lt;/p&gt;

&lt;p&gt;And context similarity can be calculated as &lt;/p&gt;

&lt;p&gt;Sim(d1,d2)=d1.d2&lt;/p&gt;

&lt;p&gt;Sim(d1,d2)=x1y1+x2y2+x3y3+......................................+xnyn&lt;/p&gt;

&lt;p&gt;Sim(d1,d2)=ðšºxiyi where i=0 to n&lt;/p&gt;

&lt;p&gt;The similarity is the probability that a randomly picked word from d1 and d2 are identical.&lt;/p&gt;

&lt;p&gt;Soon will be adding some links for the programmatical model for these relation mining and in the next series will be writing about the Topic Mining.&lt;/p&gt;

&lt;p&gt;Stay Tuned......&lt;/p&gt;

</description>
      <category>textanalytics</category>
      <category>datamining</category>
      <category>bigdata</category>
    </item>
  </channel>
</rss>
