<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Radu Gheorghe on Medium]]></title>
        <description><![CDATA[Stories by Radu Gheorghe on Medium]]></description>
        <link>https://medium.com/@radu.gheorghe?source=rss-9c9251c5083d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2ewMc6NBdCOgn0pX.jpg</url>
            <title>Stories by Radu Gheorghe on Medium</title>
            <link>https://medium.com/@radu.gheorghe?source=rss-9c9251c5083d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 13:57:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@radu.gheorghe/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Entity Extraction with spaCy]]></title>
            <link>https://medium.com/@radu.gheorghe/entity-extraction-with-spacy-234d3d11e3ba?source=rss-9c9251c5083d------2</link>
            <guid isPermaLink="false">https://medium.com/p/234d3d11e3ba</guid>
            <category><![CDATA[named-entity-recognition]]></category>
            <category><![CDATA[ecommerce]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[spacy]]></category>
            <category><![CDATA[search]]></category>
            <dc:creator><![CDATA[Radu Gheorghe]]></dc:creator>
            <pubDate>Fri, 12 Apr 2019 06:45:57 GMT</pubDate>
            <atom:updated>2019-05-02T06:56:38.418Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hFej9bpamVaU9Z-bl8xMTg.jpeg" /></figure><h3>What is Entity Extraction?</h3><p>Entity extraction is, in the context of search, the process of figuring out which fields a query should target, as opposed to always hitting all fields. The reason we may want to involve entity extraction in search is to improve precision. For example: how do we tell that, when the user typed in <strong>Apple iPhone</strong>, the intent was to run <strong>company:Apple</strong> AND <strong>product:iPhone</strong>? And not bring back phone stickers in the shape of an apple?</p><h3>What is spaCy?</h3><p><a href="https://spacy.io/">spaCy</a> is a Python framework that can do many <a href="https://en.wikipedia.org/wiki/Natural_language_processing">Natural Language Processing</a> (NLP) tasks. <a href="https://spacy.io/usage/linguistic-features#named-entities">Named Entity Extraction</a> (NER) is one of them, along with <a href="https://spacy.io/usage/training#textcat">text classification</a>, <a href="https://spacy.io/usage/linguistic-features#pos-tagging">part-of-speech tagging</a>, and others.</p><p>If this sounds familiar, that may be because we previously wrote about a different Python framework that can help us with entity extraction: <a href="https://sematext.com/blog/entity-extraction-scikit-learn-classifiers/">Scikit-learn</a>. Though Scikit-learn is more a collection of machine learning tools, rather than an NLP framework. spaCy is closer, in terms of functionality, to <a href="https://sematext.com/blog/entity-extraction-opennlp-tutorial/">OpenNLP</a>. We used all three for entity extraction during our <a href="https://www.slideshare.net/sematext/entity-extraction-for-product-search">Activate 2018 presentation</a>.</p><p>Getting spaCy is as easy as:</p><pre>pip install spacy</pre><p>In this post, we’ll use a pre-built model to extract entities, then we’ll build our own model.</p><h3>Using a pre-built model</h3><p>spaCy comes with <a href="https://spacy.io/usage/models">pre-built models for lots of languages</a>. For example, to get the English one, you’d do:</p><pre>python -m spacy download en_core_web_sm</pre><p>Then, in your Python application, it’s a matter of loading it:</p><pre>nlp = spacy.load(&#39;en_core_web_sm&#39;)</pre><p>And then you can use it to extract entities. In our <a href="https://github.com/sematext/activate/blob/master/spacy/def.py">Activate example</a>, we did:</p><pre>doc = nlp(u&quot;#bbuzz 2016: Rafał Kuć - Running High Performance And Fault Tolerant Elasticsearch&quot;)<br>for entity in doc.ents:<br>  print(entity.label_, &#39; | &#39;, entity.text)</pre><p>Which outputs:</p><pre>MONEY | #bbuzz<br>DATE | 2016<br>PERSON | Rafał Kuć - Running High</pre><p>For this particular example, this result is “approximate” at best. 2016 is indeed a date, but <a href="https://berlinbuzzwords.de/">#bbuzz</a> isn’t money. And I doubt that <a href="https://sematext.com/blog/author/kucrafal/">Rafał</a> was Running High while giving that presentation.</p><p>For this use-case, we’d need to build our own model.</p><h3>Training a new model</h3><p>To train a new model, we first need to create a pipeline that defines how we process data. In this case, we want to extract entities. Then, we’ll train a model by running test data through this pipeline. Once the model is trained, we can use it to extract entities from new data as well.</p><p>Let’s zoom into each step.</p><h3>spaCy pipelines</h3><p>With spaCy you can do much more than just entity extraction. For example, before extracting entities, you may need to <a href="https://spacy.io/usage/linguistic-features#tokenization">pre-process text</a>, for example via stemming. Or we may want to do <a href="https://spacy.io/usage/linguistic-features#pos-tagging">part-of-speech tagging</a>: is this word a verb or a noun?</p><p>For the scope of our tutorial, we’ll create an empty model, give it a name, then add a simple pipeline to it. That simple pipeline will only do named entity extraction (NER):</p><pre>nlp = spacy.blank(&#39;en&#39;) # new, empty model. Let&#39;s say it&#39;s for the English language<br>nlp.vocab.vectors.name = &#39;example_model_training&#39; # give a name to our list of vectors<br># add NER pipeline<br>ner = nlp.create_pipe(&#39;ner&#39;) # our pipeline would just do NER<br>nlp.add_pipe(ner, last=True) # we add the pipeline to the model</pre><h3>Data and labels</h3><p>To train the model, we’ll need some training data. In the case of product search, these would be queries, where we pre-label entities. For example:</p><pre>DATA = [<br>  (u&quot;Search Analytics: Business Value &amp; BigData NoSQL Backend, Otis Gospodnetic &quot;, {&#39;entities&#39;: [ (58,75,&#39;PERSON&#39;) ] }),<br>  (u&quot;Introduction to Elasticsearch by Radu &quot;, {&#39;entities&#39;: [ (16,29,&#39;TECH&#39;), (32, 36, &#39;PERSON&#39;) ] }),<br>  # ...<br>]</pre><p>Our training data has a few characteristics:</p><ul><li>The text itself is Unicode</li><li>The entities array contains a list of tuples. Each tuple is an entity labeled from the text</li><li>Each tuple contains three elements: start offset, end offset and entity name</li></ul><h3>Training the model</h3><p>Before training, we need to make our model aware of the possible entities. To do that, we add all the labels we’re aware of:</p><pre>nlp.entity.add_label(&#39;PERSON&#39;)<br>nlp.entity.add_label(&#39;TECH&#39;)<br># ...</pre><p>Now we can begin training. We’ll need to allocate the models and get an optimizer via our <a href="https://spacy.io/api/language">model</a>’s <a href="https://spacy.io/api/language#begin_training">begin_training()</a> method:</p><pre>optimizer = nlp.begin_training()</pre><p>Then we update the model with our training data. Each text, with its annotations (those labeled entities), would be passed to the <a href="https://spacy.io/api/language#update">update() function of our model</a>. Along with the newly created optimizer:</p><pre>nlp.update([text], [annotations], sgd=optimizer)</pre><p>In our <a href="https://github.com/sematext/activate/blob/master/spacy/train.py">Activate example</a>, because we have little training data, we just loop through it a few times, in random order:</p><pre>for i in range(20):<br>  random.shuffle(DATA)<br>  for text, annotations in DATA:<br>    nlp.update([text], [annotations], sgd=optimizer)</pre><p>And that’s it! Now we have a model built for our own use-case.</p><h3>Predicting entities</h3><p>The model we just built is already loaded in memory. If you don’t want to train it every time, you can <a href="https://spacy.io/api/language#to_disk">save it to disk</a> and <a href="https://spacy.io/api/language#from_disk">load it</a> when needed. With the model loaded, you’ll use it to predict entities just as you would with a pre-built model:</p><pre>doc = nlp(u&quot;#bbuzz 2016: Rafał Kuć - Running High Performance And Fault Tolerant Elasticsearch&quot;)<br>for entity in doc.ents:<br>  print(entity.label_, &#39; | &#39;, entity.text)</pre><p>Even with this small dataset, results <strong>typically</strong> look better than with the default model:</p><pre>PERSON | Rafał Kuć<br>TECH | Elasticsearch</pre><p>I’ve mentioned <strong>typically </strong>because on different runs, because of the randomization, the model looks different. Ultimately, if you want accurate results, there’s no substitute for training set size. Unless something was indeed fishy with <a href="https://sematext.com/blog/author/kucrafal/">Rafał</a> in 2016, because at times I get:</p><pre>PERSON | Rafał Kuć<br>TECH | High</pre><h3>Conclusions and next steps</h3><p>Like in the <a href="https://sematext.com/blog/entity-extraction-opennlp-tutorial/">OpenNLP example</a> we showed before, spaCy comes with pre-built models and makes it easy to build your own. It also comes with a <a href="https://spacy.io/api/cli#train">command-line training tool</a>. That said, it’s less configurable — or at least you don’t have all the options as accessible as in a purpose-built tool, like <a href="https://sematext.com/blog/entity-extraction-scikit-learn-classifiers">Scikit-learn</a>. For entity extraction, spaCy will use a <a href="https://en.wikipedia.org/wiki/Convolutional_neural_network">Convolutional Neural Network</a>, but you can <a href="https://spacy.io/api/entityrecognizer#model">plug in your own model</a> if you need to.</p><p>If you find this stuff exciting, please join us: <a href="https://sematext.com/jobs/">we’re hiring worldwide</a>. If you need entity extraction, relevancy tuning, or any other help with your search infrastructure, please <a href="https://sematext.com/contact/">reach out</a>, because we do:</p><ul><li><a href="https://sematext.com/consulting/">Solr, Elasticsearch and Elastic Stack consulting</a></li><li><a href="https://sematext.com/support/">Solr, Elasticsearch and Elastic Stack production support</a></li><li><a href="https://sematext.com/training/">Solr, Elasticsearch and Elastic Stack training classes</a> (on site and remote, public and private)</li><li><a href="https://sematext.com/cloud/">Monitoring, log centralization and tracing</a> for not only Solr and Elasticsearch, but for other applications (e.g. Kafka, Zookeeper), <a href="https://sematext.com/spm">infrastructure</a> and <a href="https://sematext.com/docker">containers</a></li></ul><p><em>Originally published at </em><a href="https://sematext.com/blog/entity-extraction-with-spacy/"><em>https://sematext.com</em></a><em> on April 12, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=234d3d11e3ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>