CINXE.COM
Tagging and Disambiguating KBpedia Knowledge Graph Concepts
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> <!--[if IE 8]> <html lang="en" class="ie8"> <![endif]--> <!--[if IE 9]> <html lang="en" class="ie9"> <![endif]--> <!--[if !IE]><!--> <html lang="en"> <!--<![endif]--> <head> <title>Tagging and Disambiguating KBpedia Knowledge Graph Concepts</title> <link rel="alternate" type="application/rss+xml" title="" href="/resources/feeds/news.xml" /> <!-- Meta --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content=""> <meta name="author" content=""> <!-- Favicon --> <link rel="shortcut icon" href="/favicon.ico"> <!-- CSS Global Compulsory --> <link rel="stylesheet" href="/assets/plugins/bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="/assets/css/style.css"> <!-- CSS Implementing Plugins --> <link rel="stylesheet" href="/assets/plugins/line-icons/line-icons.css"> <link rel="stylesheet" href="/assets/plugins/font-awesome/css/font-awesome.min.css"> <!-- CSS Theme --> <link rel="stylesheet" href="/assets/css/theme-colors/blue.css"> <link rel="stylesheet" href="/assets/css/footers/footer-v1.css"> <link rel="stylesheet" href="/assets/css/headers/header-v1.css"> <!-- CSS Customization --> <link rel="stylesheet" href="/css/custom.css"> <meta property="og:site_name" content="KBpedia"/> <meta property="og:title" content="Machine Learning Use Cases: Tagging and Disambiguating KBpedia Knowledge Graph Concepts"> <meta property="og:type" content="article"/> <meta property="og:description" content="This use case describes a fast method for disambiguating concepts, when more than one possible match is provided."> <meta property="og:image" content="/imgs/kbpedia-logo-350.png"> <meta property="og:image:width" content="350"> <meta property="og:image:height" content="390"> </head> <body> <div class="wrapper"> <div class="header-v1"> <!-- Topbar --> <div class="topbar"> <div class="container"> <div class="col-md-1"> </div> <a class="navbar-brand" href="/"> <img id="logo-header" src="/imgs/kbpedia-logo-420-horz.png" height="75" alt="KBpedia Knowledge Structure" name="logo-header"> </a> </div> </div><!-- End Topbar --> <!-- Navbar --> <div class="navbar navbar-default mega-menu" role="navigation"> <div class="container"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse"> <span class="sr-only">Toggle navigation</span> </button> </div> <div style="clear:both; height: 1px;"> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="col-md-1"> </div><!--/col-md-1--> <div class="collapse navbar-collapse navbar-responsive-collapse col-md-10"> <ul class="nav navbar-nav pull-left"> <!-- Demo --> <li> <a href="/">Home</a> </li> <!-- Home --> <li> <a href="/knowledge-graph/">knowledge Graph</a> </li> <li> <a href="http://sparql.kbpedia.org/">SPARQL</a> </li> <!-- Background --> <li class="dropdown"> <a href="/background/">Background</a> <ul class="dropdown-menu"> <li> <a href="/background/overview/">Overview</a> </li> <li> <a href="/background/features-and-benefits/">Features & Benefits</a> </li> <li> <a href="/background/data-and-knowledge-structures/">Data and Knowledge Structures</a> </li> <li> <a href="/background/technology/">Technology</a> </li> <li> <a href="/background/machine-learning/">Machine Learning</a> </li> <li> <a href="/background/uses/">Uses</a> </li> </ul> </li><!-- End Uses --> <li class="dropdown"> <a href="/use-cases/">Use Cases</a> <ul class="dropdown-menu"> <li class="dropdown-submenu"> <a href="/use-cases/knowledge-graph/">Knowledge Graph (KG)</a> <ul class="dropdown-menu"> <li><a href="/use-cases/browse-the-knowledge-graph/">Browse the Knowledge Graph</a></li> <li><a href="/use-cases/search-the-knowledge-graph/">Search the Knowledge Graph</a></li> <li><a href="/use-cases/expand-queries-using-semsets/">Expand Queries Using Semsets</a></li> <li><a href="/use-cases/use-and-control-of-inferencing/">Uses and Control of Inferencing</a></li> </ul> </li> <li class="dropdown-submenu"> <a href="/use-cases/machine-learning-use-case/">Machine Learning (KBAI)</a> <ul class="dropdown-menu"> <li><a href="/use-cases/text-classification-using-esa-and-svm/">Create Supervised Learning Training Sets</a></li> <li><a href="/use-cases/document-specific-word2vec-training-corpuses/">Create Word Embedding Corpuses</a></li> <li><a href="/use-cases/extending-kbpedia-with-kbpedia-categories/">Create Graph Embedding Corpuses</a></li> <li><a href="/use-cases/text-classification-using-esa-and-svm/">Classify Text</a></li> <li><a href="/use-cases/dynamic-machine-learning/">Create 'Gold Standards' for Tuning Learners</a></li> <li><a href="/use-cases/disambiguating-kbpedia-knowledge-graph-concepts/">Disambiguate KG Concepts</a></li> <li><a href="/use-cases/dynamic-machine-learning/">Dynamic Machine Learning Using the KG</a></li> </ul> </li> <li class="dropdown-submenu"> <a href="/use-cases/mapping-use-case/">Mapping</a> <ul class="dropdown-menu"> <li><a href="/use-cases/mapping-external-data-and-schema/">Map Concepts</a></li> <li><a href="/use-cases/extending-kbpedia-with-kbpedia-categories/">Extend KBpedia with Wikipedia</a></li> <li><a href="/use-cases/benefits-from-extending-kbpedia-with-private-datasets/">Extend KBpedia for Domains</a></li> <li><a href="/use-cases/mapping-external-data-and-schema/">General Use of the Mapper</a></li> </ul> </li> </ul> </li> <li class="dropdown"> <a href="/resources/">Resources</a> <ul class="dropdown-menu"> <li><a href="/resources/downloads/">Download KBpedia</a></li> <li><a href="/resources/about/">About KBpedia</a></li> <li><a href="/resources/faq/">KBpedia FAQ</a></li> <li><a href="/resources/news/">News About KBpedia</a></li> <li><a href="/resources/statistics/">KBpedia Statistics</a></li> <li><a href="/resources/documentation/">Additional Documentation</a></li> <li><a href="/resources/support/">Support for KBpedia</a>.</li> </ul> </li> </ul> </div><!--/navbar-collapse--> <div class="col-md-1"> </div><!--/col-md-1--> </div> </div><!-- End Navbar --> </div><!--=== End Header ===--> <!--=== Breadcrumbs ===--> <div class="breadcrumbs"> <div class="container"> <div class="col-md-1"> </div><!--/col-md-1--> <div class="col-md-10"> <h1 class="pull-left"></h1> <ul class="pull-right breadcrumb"> <li>Use Cases</li> <li class="active">Disambiguating Concepts</li> </ul> </div><!--/col-md-10--> <div class="col-md-1"> </div><!--/col-md-1--> </div> </div> <!--/breadcrumbs--> <!--=== End Breadcrumbs ===--> <!--=== Content Part ===--> <div class="container content"> <div class="row"> <div class="col-md-2"> </div> <div class="col-md-8"> <div class="use-cases-header"> <table border="0" cellpadding="4" cellspacing="2"> <tbody> <tr> <td colspan="2" align="center"> <h2> <b>USE CASE</b> </h2> </td> </tr> <tr> <td style="width: 140px;" valign="top"> <b>Title:</b> </td> <td style="padding-left: 25px;" valign="top"> <span style="font-weight: bold;">Disambiguating KBpedia Knowledge Graph Concepts</span> </td> </tr> <tr> <td valign="top"> <b>Short Description:</b> </td> <td style="padding-left: 25px;" valign="top"> This use case describes a fast method for disambiguating concepts, when more than one possible match is provided. </td> </tr> <tr> <td valign="top"> <b>Problem:</b> </td> <td style="padding-left: 25px;" valign="top"> When "tagging" or extracting concepts, most methods rely on matching the surface forms of the names or labels associated with the concepts. However, these specific terms or phrases may be, in themselves, ambiguous; that is, they may match to multiple possible concepts (think of words like "bank", "club", "fan", "right", etc.). Thus, the proper meaning of a term or phrase depends on its use and context. Disambiguation methods must make these discriminations. </td> </tr> <tr> <td valign="top"> <b>Approach:</b> </td> <td style="padding-left: 25px;" valign="top"> The KBpedia knowledge graph is based on consistent and coherent relationships. Related concepts are located close to one another; unrelated concepts reside much farther apart in the graph. Graph embeddings for each node and its adjacent neighbors in the graph are created using the <a href="https://arxiv.org/abs/1403.6652">DeepWalk</a> method, which creates a vector for each concept. Surface forms of matching concepts are first obtained via fairly straightforward parsing. Where potential matches have more than one candidate concept, its vector representation is compared to vectors from the surrounding text; most closely aligned vectors are taken as the most accurate match, thus disambiguating from amongst the ambiguous candidates. Simple linear algebra equations are used to make these comparison calculations, a process that is extremely fast. The overall process is captured by a repeatable pipeline with statistical reporting, enabling rapid refinements in parameters and methods to achieve the best-performing model. This general method can be applied to entities as well as concepts. </td> </tr> <tr> <td valign="top"> <b>Key Findings</b> </td> <td valign="top"> <ul> <li>A fast, and acceptably accurate, way to disambiguate concepts is defined</li> <li>The general method is applicable to disambiguating both concepts and entities</li> <li>Accurate graph embeddings to aid disambiguation may be obtained automatically and for "free" given the coherent structure of the KBpedia knowledge graph</li> <li>A disambiguation accuracy of more than 65% is possible with the first-candidate approach with optimized hyperparameters</li> <li>The chosen method can disambiguate more than 1500 terms per minute on commodity hardware<br> </li> <li>Slower methods can be invoked to increase accuracy further</li> <li>The speed of the disambiguation process suggests it may be justified to include as a pre-processing step to other machine learning tasks (classification, entity typing, etc.)<br> </li> <li>Future experiments may include other graph walk strategies like depth-first or breadth-first walks, as well as combinations of different graph relationships.<br> </li> </ul> </td> </tr> </tbody> </table> </div> </div> <div class="col-md-2"> </div><!--/col-md-2--> </div> <div class="row"> </div> <div class="row"> </div> <div class="row"> <div class="col-md-2"> </div> <div class="col-md-8"> <p> One of the most important natural language processing tasks is to "tag" concepts in text. Tagging a concept means determining whether words or phrases in a text document match any of the concepts that exist in a knowledge structure (such as a knowledge graph, an ontology, a taxonomy, a vocabulary, etc.). (BTW, a similar definition and process applies to tagging an entity.) What is usually performed when tagging is that the input text is parsed and normalized in some manner. Then all of the surface forms of the concepts within the input knowledge structure (based on their preferred and alternative labels) are matched against the words within the text. "Tagging" is when a match occurs between a concept in the knowledge structure and one of its surface forms in the input text. </p> <p> But here is the problem. Given the ambiguous world we live in, often this surface form, which after all is only a word or phrase, may be associated with multiple different concepts. When we identify the surface form of "bank", does that term refer to a financial institution, the shore of a river, a plane turning, or a pool shot? Identical surface forms may refer to totally different concepts. Further, sometimes a single concept will be identified but it won't be the right concept, possibly because the right one is missing from the knowledge structure or other issues. </p> <p> The purpose of this article is thus to explain how we can "disambiguate" (that is, suggest the proper concept from an ambiguous list) the concepts that have been tagged. What we will do is to show how we can leverage the KBpedia knowledge graph structure as-is to perform this disambiguation. What we will do is to create graph embeddings for each of the KBpedia concepts using the DeepWalk algorithm. Then we will perform simple linear algebra equations on the graph embeddings to determine if the tagged concept(s) is the right one given its context or not. We will test multiple different algorithms and strategies to analyze the impact on the overall disambiguation performance of the system. </p> <div id="outline-container-orgafcce09" class="outline-2"> <br/> <h2 id="orgafcce09">Different Situations</h2> <div class="outline-text-2" id="text-orgafcce09"> <p> Before starting to work on the problem, let's discuss the issues we may encounter with any tagging and disambiguation task. The three possibilities we may encounter are: </p> <ol class="org-ol"> <li>A word in the text refers to a concept, but the concept hasn't been identified (is not in the graph)</li> <li>A word in the text has been tagged with a single concept, but that concept is not the right one (the right one is not in the graph)</li> <li>A word in the text has been tagged with multiple concepts, so the right concept needs to be selected amongst the options offered.</li> </ol> <p> What we present in this article is how we can automatically disambiguate the situations 2 and 3. (To fix the first situation, not addressed further herein, we would have to add additional surface forms to the knowledge graph by adding new alternative labels to existing concepts, or by adding new concepts with their own surface forms.) </p> </div> </div> <div id="outline-container-org3214f1c" class="outline-2"> <br/> <h2 id="org3214f1c">Disambiguation Process</h2> <div class="outline-text-2" id="text-org3214f1c"> <p> The disambiguation process is composed of two main tasks: the creation of the graph embeddings for each concept in the KBpedia knowledge graph, and then the evaluation of each tagged concept to disambiguate them. </p> </div> <div id="outline-container-org607ec3e" class="outline-3"> <br/> <h3 id="org607ec3e">Introducing DeepWalk</h3> <div class="outline-text-3" id="text-org607ec3e"> <p> <a href="https://arxiv.org/abs/1403.6652">DeepWalk</a> was created to learn <i>social representations</i> of a graph's vertices that capture neighborhood similarity and community membership. DeepWalk generalizes neural language models to process a special language composed of a set of randomly-generated walks. </p> <p> We want to use DeepWalk not to learn <i>social representations</i> but to learn the relationship (that is, the similarity) between all of the concepts existing in a knowledge graph given different kinds of relationships such as <code>sub-class-of</code>, <code>super-class-of</code>, <code>equivalent-class</code> or other relationships such as KBpedia's <code>80 aspects relationships</code>. (Note, we also discussed and used DeepWalk in another use case <a href="/use-cases/extending-kbpedia-with-kbpedia-categories/">extending KBpedia</a>.) </p> <p> For this use case we use the DeepWalk algorithm to select concepts from the broader Wikipedia corpus. Other tasks that could be performed using DeepWalk in a similar manner are: </p> <ol class="org-ol"> <li>Content recommendation,</li> <li>Anomaly detection [in the knowledge graph], or</li> <li>Missing link prediction [in the knowledge graph].</li> </ol> <p> Note that we randomly walk the graphs as stated in DeepWalk's original paper <sup><a id="fnr.1" class="footref" href="#fn.1">1</a></sup>. However more experiments could be performed to change the random walk by other graph walk strategies like depth-first or breadth-first walks. </p> </div> </div> <div id="outline-container-org6b9844f" class="outline-3"> <br/> <h3 id="org6b9844f">Create Graph Embedding Vectors</h3> <div class="outline-text-3" id="text-org6b9844f"> <p> We first create a <code>graph embedding</code> for each of the Wikipedia categories. What we have to do is to use the Wikipedia category structure along with the linked KBpedia knowledge graph. Then we have to generate the graph embedding for each of the Wikipedia categories that exist in the structure. </p> <p> The graph embeddings are generated using the <a href="https://arxiv.org/abs/1403.6652">DeepWalk</a> algorithm over that linked structure. It randomly walks the linked graph hundred of times to generate the graph embeddings for each category. </p> </div> <div id="outline-container-org2082ce9" class="outline-4"> <br/> <h4 id="org2082ce9">Create Deeplearning4j Graph</h4> <div class="outline-text-4" id="text-org2082ce9"> <p> To generate the graph embeddings, we use <a href="https://deeplearning4j.org/">Deeplearning4j's</a> DeepWalk implementation. This step creates a Deeplearning4j <code>graph</code> structure that is used by its DeepWalk implementation to generate the embeddings. </p> <p> The graph we have to create is composed of the latest version of KBpedia knowledge graph version <code>1.20</code>. Since the only concepts we want to disambiguate are the KBpedia reference concepts as tagged by the tagger, then this is the only knowledge structure we have to load to create the graph embeddings. </p> <p> Then we generate the initial Deeplearning4j graph structure composed of the <i>inferred</i> KBpedia knowledge graph. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>use '<span style="color: #66D9EF;">[</span><span style="color: #66D9EF;">cognonto-deepwalk.core</span><span style="color: #66D9EF;">]</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>require '<span style="color: #66D9EF;">[</span><span style="color: #66D9EF;">clojure.string</span> <span style="color: #AE81FF;">:as</span> string<span style="color: #66D9EF;">]</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>require '<span style="color: #66D9EF;">[</span><span style="color: #66D9EF;">clojure.data.csv</span> <span style="color: #AE81FF;">:as</span> csv<span style="color: #66D9EF;">]</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>require '<span style="color: #66D9EF;">[</span><span style="color: #66D9EF;">clojure.java.io</span> <span style="color: #AE81FF;">:as</span> io<span style="color: #66D9EF;">]</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>require '<span style="color: #66D9EF;">[</span><span style="color: #66D9EF;">cognonto-owl.query</span> <span style="color: #AE81FF;">:as</span> query<span style="color: #66D9EF;">]</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defonce</span> <span style="color: #FD971F;">knowledge-graph</span> <span style="color: #66D9EF;">(</span>load-knowledge-graph <span style="color: #E6DB74;">"file:/d:/cognonto-git/kbpedia-generator/kbpedia/1.10/target/kbpedia_reference_concepts_linkage_inferrence_extended_2.n3.owl"</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> </div> </div> <div id="outline-container-org9b34106" class="outline-4"> <br/> <h4 id="org9b34106">Train DeepWalk</h4> <div class="outline-text-4" id="text-org9b34106"> <p> Once the Deeplearning4j graph is created, the next step is to create and train the DeepWalk algorithm. What the <code>(create-deep-walk)</code> function does is to create and initialize a <code>DeepWalk</code> object with the <code>Graph</code> we created above and with some hyperparameters. </p> <p> The <code>:window-size</code> hyperparameter is the size of the window used by the continuous <a href="https://en.wikipedia.org/wiki/N-gram#Skip-gram">Skip-gram</a> algorithm used in DeepWalk. The <code>:vector-size</code> hyperparameter is the size of the embedding vectors we want the DeepWalk to generate (it is the number of dimensions of our model). The <code>:learning-rate</code> is the initial leaning rate of the <a href="https://en.wikipedia.org/wiki/Stochastic_gradient_descent">Stochastic gradient descent</a>. </p> <p> For this task, we initially use a window of <code>15</code> and <code>3</code> dimensions to make visualizations simpler to interpret, and an initial learning rate of <code>2.5%</code>. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">graph</span> <span style="color: #66D9EF;">(</span>create-deepwalk-graph knowledge-graph <span style="color: #AE81FF;">:directed?</span> <span style="color: #AE81FF;">true</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">deep-walk</span> <span style="color: #66D9EF;">(</span>create-deep-walk graph <span style="color: #AE81FF;">:window-size</span> 15 <span style="color: #AE81FF;">:vector-size</span> 3 <span style="color: #AE81FF;">:learning-rate</span> 0.025<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <p> Once the DeepWalk object is created and initialized with the graph, the next step is to train that model to generate the embedding vectors for each vertex in the graph. </p> <p> The training is performed using a random walk iterator. The two hyperparameters related to the training process are the <code>walk-length</code> and the <code>walks-per-vertex</code>. The <code>walk-length</code> is the number of vertices we want to visit for each iteration. The <code>walks-per-vertex</code> is the number of timex we want to create random walks for each vertex in the graph. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">train</span> <span style="color: #66D9EF;">(</span><span style="color: #A6E22E;">[</span>deep-walk iterator<span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span>train deep-walk iterator 1<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #A6E22E;">[</span>deep-walk iterator walks-per-vertex<span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">.fit</span> deep-walk iterator<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">dotimes</span> <span style="color: #E6DB74;">[</span>n walks-per-vertex<span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">.reset</span> iterator<span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">.fit</span> deep-walk iterator<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #A6E22E;">[</span>deep-walk graph walk-length walks-per-vertex<span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span>train deep-walk <span style="color: #E6DB74;">(</span><span style="color: #F92672;">new</span> <span style="color: #66D9EF;">RandomWalkIterator</span> graph walk-length<span style="color: #E6DB74;">)</span> walks-per-vertex<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <p> For the initial setup, we want to have a <code>walk-length</code> of <code>15</code> and we want to iterate the process <code>175</code> times per vertex. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>train deep-walk graph 15 175<span style="color: #AE81FF;">)</span> </pre> </div> </div> </div> </div> <div id="outline-container-org7ea8f0f" class="outline-3"> <br/> <h3 id="org7ea8f0f">Concept Disambiguation</h3> <div class="outline-text-3" id="text-org7ea8f0f"> <p> Now that all of the KBpedia reference concepts are characterized using the graph embeddings, how can we disambiguate each of these concepts in a text? The first thing is to observe what is happening. When we tag a sentence such as <i>"A sandstorm blows over damaged buildings in rebel held area of Douma."</i> the first step that happens is that the surface forms (preferred labels and alternative labels) of the KBpedia concepts are identified within the sentence. As we discussed above, it is possible that a word, or a group of words, may get tagged with multiple KBpedia reference concepts. </p> <p> Because we characterized each of the concepts with its graph embeddings, it means that what we are dealing with is a series of vectors that represent the "meaning" of each concept in the knowledge graph structure. What we end-up with is the following: </p> <div class="figure"> <p><img src="sentence.png" alt="sentence.png" /> </p> </div> <p> The next step is to use that information to try to disambiguate each of these concepts. To try to disambiguate the concepts, we have to make a few assumptions. First we have to assume that the graph embeddings created by the DeepWalk algorithm when crawling the KBpedia knowledge graph represent the <i>"meaning"</i> of the concept within the knowledge structure. Then we have to assume that the sentence where the concept has been identified creates a <i>context</i> that we can use to disambiguate the identified concepts based on their <i>"meaning"</i>. </p> <p> In the example above, the context is the sentence, and four concepts have been identified. The big assumption we are making here is that each identified concept is "tightly" related to others in the knowledge graph. Given this assumption, what we have to do is to calculate the relatedness of each concept, within the context, and keep the ones that are closest to each other. </p> </div> <div id="outline-container-org4c1cb97" class="outline-4"> <br/> <h4 id="org4c1cb97">Gold Standard Creation</h4> <div class="outline-text-4" id="text-org4c1cb97"> <p> Before digging into how we will perform the disambiguation of these concepts, another step is to create a gold standard that we will use to evaluate the performance of our model and to check the impact of different hyperparameters on the disambiguation process. We created this gold standard manually by using random sentences found in online news articles. We first tagged each of these sentences using the tagger. Then, we manually disambiguated each of the tagged concepts. This gives us the properly labeled training set for our exercise. The result is the following <a href="disambiguation-gold-standard.csv">gold standard file</a>. </p> <p> Each of the annotations looks like this: </p> <p> <img src="ltximg/kbpedia-concepts-disambiguation_main_7c24ba70dbba722f3a2b8cfcf3afea2446f60105.png" alt="kbpedia-concepts-disambiguation_main_7c24ba70dbba722f3a2b8cfcf3afea2446f60105.png" /> </p> <p> This markup first presents the surface forms for the related concept(s) in the knowledge graph within the double brackets, followed by the concept(s) URI endings between the double parenthesis. The double colon <code>::</code> designator provides the suggested disambiguated concept. If nothing follows the double colon it means that the correct concept does not exist in the knowledge graph). </p> </div> </div> <div id="outline-container-orgb597cc7" class="outline-4"> <br/> <h4 id="orgb597cc7">Disambiguation Method</h4> <div class="outline-text-4" id="text-orgb597cc7"> <p> The actual disambiguation method is based on some simple linear algebra formulas. </p> <p> Each tagged word <img src="ltximg/kbpedia-concepts-disambiguation_main_0d1399fad7c1911e6241538c7b4fdfb50329d273.png" alt="kbpedia-concepts-disambiguation_main_0d1399fad7c1911e6241538c7b4fdfb50329d273.png" /> is related to a sliding context <img src="ltximg/kbpedia-concepts-disambiguation_main_7d61ea454e712ce09c7f75d3140788acf53ae5fe.png" alt="kbpedia-concepts-disambiguation_main_7d61ea454e712ce09c7f75d3140788acf53ae5fe.png" /> where <img src="ltximg/kbpedia-concepts-disambiguation_main_9783bbde9fb6923e6ceddef20e7a918b54a2f992.png" alt="kbpedia-concepts-disambiguation_main_9783bbde9fb6923e6ceddef20e7a918b54a2f992.png" />. Each tagged word has <img src="ltximg/kbpedia-concepts-disambiguation_main_ef038ba123ebef4319d569d54ec40dc5f67bc0c5.png" alt="kbpedia-concepts-disambiguation_main_ef038ba123ebef4319d569d54ec40dc5f67bc0c5.png" /> graph embedding vectors (i.e. a <i>"sense"</i> vector) <img src="ltximg/kbpedia-concepts-disambiguation_main_a5f0e1561072b96080a9c732794bd18fe00f1a57.png" alt="kbpedia-concepts-disambiguation_main_a5f0e1561072b96080a9c732794bd18fe00f1a57.png" /> where <img src="ltximg/kbpedia-concepts-disambiguation_main_72cf9483351da664caaca6cbde55432080d6c202.png" alt="kbpedia-concepts-disambiguation_main_72cf9483351da664caaca6cbde55432080d6c202.png" />. What we want the disambiguation method to do is to calculate a score between each embedding vector of each identified concept of each word given its sliding context. The score is calculated by performing the dot product between the graph embedding vector <img src="ltximg/kbpedia-concepts-disambiguation_main_6b526e3a714ac2c7be46661934ef653eac7e7fd8.png" alt="kbpedia-concepts-disambiguation_main_6b526e3a714ac2c7be46661934ef653eac7e7fd8.png" /> and the sliding context vector <img src="ltximg/kbpedia-concepts-disambiguation_main_774d0389f3a45245568f0233190a445a7ad65f53.png" alt="kbpedia-concepts-disambiguation_main_774d0389f3a45245568f0233190a445a7ad65f53.png" />. The sliding context vector is calculated by summing the graph embedding vector <img src="ltximg/kbpedia-concepts-disambiguation_main_6b526e3a714ac2c7be46661934ef653eac7e7fd8.png" alt="kbpedia-concepts-disambiguation_main_6b526e3a714ac2c7be46661934ef653eac7e7fd8.png" /> of each word <img src="ltximg/kbpedia-concepts-disambiguation_main_ccb5e9230532a6990c88b620742d233da163504d.png" alt="kbpedia-concepts-disambiguation_main_ccb5e9230532a6990c88b620742d233da163504d.png" /> within the window: </p> <p> <img src="ltximg/kbpedia-concepts-disambiguation_main_73e561775d087190d01c276973ae7b6941ab72e7.png" alt="kbpedia-concepts-disambiguation_main_73e561775d087190d01c276973ae7b6941ab72e7.png" /> </p> <p> Finally the score is calculated using: </p> <p> <img src="ltximg/kbpedia-concepts-disambiguation_main_15bde81280c85f78a2f16bd40d9ac16d62b8fb41.png" alt="kbpedia-concepts-disambiguation_main_15bde81280c85f78a2f16bd40d9ac16d62b8fb41.png" /> </p> <p> This is the first definition of the score we want to use to begin to disambiguate concepts from the knowledge graph associated with words in a sentence. As you can read from the formula, only the first sense for each word <img src="ltximg/kbpedia-concepts-disambiguation_main_0d1399fad7c1911e6241538c7b4fdfb50329d273.png" alt="kbpedia-concepts-disambiguation_main_0d1399fad7c1911e6241538c7b4fdfb50329d273.png" /> is being selected for each word within the sliding window that creates the context. (Other variants are be explored later in this article.) However as we will see below, we will change that formulas a bit such that the score becomes easier to understand for a human by expressing it in degrees. What is important to understand here is that we use two vectors to calculate the score: the graph embedding of the concept we want to disambiguate and the vector calculated from the windowed context that sums all the graph embedding vectors of each concept within that window. </p> <p> Now let's put this method into code. The first thing we have to do is to create a lookup table that is composed of the graph embeddings for each of the concepts that exist in the KBpedia knowledge graph as we calculated with the DeepWalk algorithm above. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">index</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">->></span> <span style="color: #A6E22E;">(</span><span style="color: #66D9EF;">query</span><span style="color: #66D9EF;">/</span>get-classes knowledge-graph<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>map-indexed <span style="color: #E6DB74;">(</span><span style="color: #F92672;">fn</span> <span style="color: #FD971F;">[</span>i class<span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">if-not</span> <span style="color: #F92672;">(</span>string? class<span style="color: #F92672;">)</span> <span style="color: #F92672;">{</span><span style="color: #AE81FF;">(</span><span style="color: #F92672;">.toString</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">.getIRI</span> class<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>inc i<span style="color: #AE81FF;">)</span><span style="color: #F92672;">}</span> <span style="color: #F92672;">{</span>class <span style="color: #AE81FF;">(</span>inc i<span style="color: #AE81FF;">)</span><span style="color: #F92672;">}</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>apply merge<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> ^<span style="color: #AE81FF;">:dynamic</span> <span style="color: #FD971F;">vertex-vectors</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">.getVertexVectors</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">.lookupTable</span> deep-walk<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <p> Getting the vector <img src="ltximg/kbpedia-concepts-disambiguation_main_a5f0e1561072b96080a9c732794bd18fe00f1a57.png" alt="kbpedia-concepts-disambiguation_main_a5f0e1561072b96080a9c732794bd18fe00f1a57.png" /> for the word <img src="ltximg/kbpedia-concepts-disambiguation_main_0d1399fad7c1911e6241538c7b4fdfb50329d273.png" alt="kbpedia-concepts-disambiguation_main_0d1399fad7c1911e6241538c7b4fdfb50329d273.png" /> is as simple as getting it from the lookup table we created above: </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">get-vector</span> <span style="color: #66D9EF;">[</span>uri-ending<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when-let</span> <span style="color: #A6E22E;">[</span>concept <span style="color: #E6DB74;">(</span>get index <span style="color: #FD971F;">(</span>str <span style="color: #E6DB74;">"http://kbpedia.org/kko/rc/"</span> uri-ending<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">.getRow</span> vertex-vectors concept<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <p> To get the sense <img src="ltximg/kbpedia-concepts-disambiguation_main_e58c29910eb8c832fb24c34d0ac56af7b0892c2e.png" alt="kbpedia-concepts-disambiguation_main_e58c29910eb8c832fb24c34d0ac56af7b0892c2e.png" /> of the word <img src="ltximg/kbpedia-concepts-disambiguation_main_f1a1bc341aaedf18366e6daed56662ba6789bae3.png" alt="kbpedia-concepts-disambiguation_main_f1a1bc341aaedf18366e6daed56662ba6789bae3.png" /> (which is <code>SandstormAsObject</code>) in our example sentence above, we only have to: </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">.toString</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">.data</span> <span style="color: #A6E22E;">(</span>get-vector <span style="color: #E6DB74;">"SandstormAsObject"</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> [0.08586349,-0.06854561,-0.14704005] </pre> <p> To get the vector <img src="ltximg/kbpedia-concepts-disambiguation_main_774d0389f3a45245568f0233190a445a7ad65f53.png" alt="kbpedia-concepts-disambiguation_main_774d0389f3a45245568f0233190a445a7ad65f53.png" /> of the context of the word <img src="ltximg/kbpedia-concepts-disambiguation_main_0d1399fad7c1911e6241538c7b4fdfb50329d273.png" alt="kbpedia-concepts-disambiguation_main_0d1399fad7c1911e6241538c7b4fdfb50329d273.png" /> we have to do some more work. We have to create a function that will calculate the <code>dot product</code> between two vectors. Then we will have to create another function that will calculate the <code>sum</code> of <code>x</code> number of vectors. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">dot-product-clj</span> <span style="color: #66D9EF;">[</span>x y<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">->></span> <span style="color: #A6E22E;">(</span>interleave x y<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>partition 2 2<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>map #<span style="color: #E6DB74;">(</span>apply * <span style="color: #FD971F;">%</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>reduce +<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">angle-clj</span> <span style="color: #66D9EF;">[</span>a b<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #66D9EF;">Math</span><span style="color: #66D9EF;">/</span><span style="color: #F92672;">toDegrees</span> <span style="color: #A6E22E;">(</span><span style="color: #66D9EF;">Math</span><span style="color: #66D9EF;">/</span>acos <span style="color: #E6DB74;">(</span>/ <span style="color: #FD971F;">(</span>dot-product-clj a b<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>* <span style="color: #F92672;">(</span><span style="color: #66D9EF;">Math</span><span style="color: #66D9EF;">/</span>sqrt <span style="color: #AE81FF;">(</span>dot-product-clj a a<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span><span style="color: #66D9EF;">Math</span><span style="color: #66D9EF;">/</span>sqrt <span style="color: #AE81FF;">(</span>dot-product-clj b b<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">disambiguate</span> <span style="color: #66D9EF;">[</span>line<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">let</span> <span style="color: #A6E22E;">[</span>tags <span style="color: #E6DB74;">(</span>re-seq #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74;">"</span> line<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #66D9EF;">clojure.pprint</span><span style="color: #66D9EF;">/</span>pprint tags<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">loop</span> <span style="color: #E6DB74;">[</span>i 0 tag <span style="color: #FD971F;">(</span>first tags<span style="color: #FD971F;">)</span> rtags <span style="color: #FD971F;">(</span>rest tags<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>word <span style="color: #F92672;">(</span>second tag<span style="color: #F92672;">)</span> concepts <span style="color: #F92672;">(</span>last tag<span style="color: #F92672;">)</span> concept <span style="color: #F92672;">(</span>get-tag-concept concepts<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span>println word <span style="color: #E6DB74;">" --> "</span> concepts<span style="color: #FD971F;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Disambiguate concepts</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">let</span> <span style="color: #F92672;">[</span>ambiguous-concepts <span style="color: #AE81FF;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split <span style="color: #66D9EF;">(</span>first <span style="color: #A6E22E;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split concepts #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> #<span style="color: #E6DB74;">" "</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">]</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #AE81FF;">[</span>ambiguous-concept ambiguous-concepts<span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span>println <span style="color: #E6DB74;">"a-vector: "</span> <span style="color: #66D9EF;">(</span>get-vector ambiguous-concept<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>println <span style="color: #E6DB74;">"b-vector: "</span> <span style="color: #66D9EF;">(</span>get-context i tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>println <span style="color: #E6DB74;">"dot product: "</span> <span style="color: #66D9EF;">(</span>dot-product <span style="color: #A6E22E;">(</span>get-vector ambiguous-concept<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>get-context i tags<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>println <span style="color: #E6DB74;">"angle: "</span> <span style="color: #66D9EF;">(</span>angle <span style="color: #A6E22E;">(</span>get-vector ambiguous-concept<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>get-context i tags<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>println<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">when-not</span> <span style="color: #F92672;">(</span>empty? rtags<span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">recur</span> <span style="color: #AE81FF;">(</span>inc i<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>first rtags<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>rest rtags<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">dot-product</span> <span style="color: #75715E;">"Calculate the dot product of two vectors (NDArray)"</span> <span style="color: #66D9EF;">[</span>v1 v2<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>first <span style="color: #A6E22E;">(</span>read-string <span style="color: #E6DB74;">(</span><span style="color: #F92672;">.toString</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">.data</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">.mmul</span> v1 <span style="color: #AE81FF;">(</span><span style="color: #F92672;">.transpose</span> v2<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">sum-vectors</span> <span style="color: #75715E;">"Sum any number of vectors (NDArray)"</span> <span style="color: #66D9EF;">[</span>& args<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">let</span> <span style="color: #A6E22E;">[</span>args <span style="color: #E6DB74;">(</span>remove nil? args<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">loop</span> <span style="color: #E6DB74;">[</span>result <span style="color: #FD971F;">(</span>first args<span style="color: #FD971F;">)</span> args <span style="color: #FD971F;">(</span>rest args<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">if</span> <span style="color: #FD971F;">(</span>empty? args<span style="color: #FD971F;">)</span> result <span style="color: #FD971F;">(</span><span style="color: #F92672;">recur</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">.add</span> result <span style="color: #AE81FF;">(</span>first args<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span>rest args<span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <p> The next step is to create the function that calculates the context vector, which is a sliding window of the concept associated with <img src="ltximg/kbpedia-concepts-disambiguation_main_82ab83e730d76d23ce3732d5b1705d391cb72c45.png" alt="kbpedia-concepts-disambiguation_main_82ab83e730d76d23ce3732d5b1705d391cb72c45.png" />, <img src="ltximg/kbpedia-concepts-disambiguation_main_0d1399fad7c1911e6241538c7b4fdfb50329d273.png" alt="kbpedia-concepts-disambiguation_main_0d1399fad7c1911e6241538c7b4fdfb50329d273.png" /> and <img src="ltximg/kbpedia-concepts-disambiguation_main_aa3602038506d03eb84e703eb2db703d74794a9c.png" alt="kbpedia-concepts-disambiguation_main_aa3602038506d03eb84e703eb2db703d74794a9c.png" />. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">get-tag-concept</span> <span style="color: #66D9EF;">[</span>concepts<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">if</span> <span style="color: #A6E22E;">(</span>> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">.indexOf</span> concepts <span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">)</span> -1<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>second <span style="color: #E6DB74;">(</span>re-find #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74;">"</span> concepts<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> concepts<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <p> Let's see how that works. To calculate <img src="ltximg/kbpedia-concepts-disambiguation_main_774d0389f3a45245568f0233190a445a7ad65f53.png" alt="kbpedia-concepts-disambiguation_main_774d0389f3a45245568f0233190a445a7ad65f53.png" /> when <img src="ltximg/kbpedia-concepts-disambiguation_main_99f89040e96fd7145a771f6ea53e64f14a07bfbc.png" alt="kbpedia-concepts-disambiguation_main_99f89040e96fd7145a771f6ea53e64f14a07bfbc.png" /> we have to calculate <img src="ltximg/kbpedia-concepts-disambiguation_main_3e70cb55d508d5cd9276f01daa169dce44fb7bcb.png" alt="kbpedia-concepts-disambiguation_main_3e70cb55d508d5cd9276f01daa169dce44fb7bcb.png" /> which can be done with the following code: </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">s{1,1}</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">sandstormasobject</span> <span style="color: #66D9EF;">(</span>get-vector <span style="color: #E6DB74;">"SandstormAsObject"</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">s{2,1}</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">blowingair</span> <span style="color: #66D9EF;">(</span>get-vector <span style="color: #E6DB74;">"BlowingAir"</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">s{3,1}</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">building</span> <span style="color: #66D9EF;">(</span>get-vector <span style="color: #E6DB74;">"Building"</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">c</span> <span style="color: #66D9EF;">(</span>sum-vectors sandstormasobject blowingair building<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>println <span style="color: #66D9EF;">(</span><span style="color: #F92672;">.toString</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">.data</span> c<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> [0.24941699,-0.0940429,-0.11848262] </pre> <p> Finally we have to calculate the score used to disambiguate the two senses of the word <code>blows</code> by performing the dot product between <img src="ltximg/kbpedia-concepts-disambiguation_main_1776d587df81749698f57cb9f0873c69870fe6ce.png" alt="kbpedia-concepts-disambiguation_main_1776d587df81749698f57cb9f0873c69870fe6ce.png" /> and <img src="ltximg/kbpedia-concepts-disambiguation_main_34ba6b04114515f0170ff209540475e3fcdf7ddb.png" alt="kbpedia-concepts-disambiguation_main_34ba6b04114515f0170ff209540475e3fcdf7ddb.png" /> where <img src="ltximg/kbpedia-concepts-disambiguation_main_eafcc43344ccdb500c279acd0b8a17187992c261.png" alt="kbpedia-concepts-disambiguation_main_eafcc43344ccdb500c279acd0b8a17187992c261.png" /> which can be done using the following code: </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">s{2,2}</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">windprocess</span> <span style="color: #66D9EF;">(</span>get-vector <span style="color: #E6DB74;">"WindProcess"</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>println <span style="color: #E6DB74;">"BlowingAir score:"</span> <span style="color: #66D9EF;">(</span>dot-product blowingair c<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>println <span style="color: #E6DB74;">"WindProcess score:"</span> <span style="color: #66D9EF;">(</span>dot-product windprocess c<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> BlowingAir score: 0.011248392 WindProcess score: 0.005993685 </pre> <p> What the scores suggest is, given the context, the right concept associated with the word <code>blows</code> is <code>BlowingAir</code> since its score is bigger. This is the right answer. We can see how simple linear algebra manipulations can be used to help us automatically disambiguate such concepts. In fact, the crux of the problem is not to perform these operations but to create a coherent and consistent knowledge graph such as KBpedia and then to create the right graph embeddings for each of its concepts. The coherent graph structure gives us this disambiguation capability for "free". </p> <p> However, these scores, as is, are hard to interpret and understand. What we want to do next is to transform these numbers into a degree between <code>0</code> and <code>360</code>. The degree between the word sense's vector <img src="ltximg/kbpedia-concepts-disambiguation_main_a5f0e1561072b96080a9c732794bd18fe00f1a57.png" alt="kbpedia-concepts-disambiguation_main_a5f0e1561072b96080a9c732794bd18fe00f1a57.png" /> and <img src="ltximg/kbpedia-concepts-disambiguation_main_a62515e81db25785ddb65fc097b8bc1e0c6f84ad.png" alt="kbpedia-concepts-disambiguation_main_a62515e81db25785ddb65fc097b8bc1e0c6f84ad.png" /> represent how close the two vectors are to each other. A degree <code>0</code> would means that both vectors are identical in terms of relationship, and a degree of <code>180</code> would mean they are quite dissimilar. What we will see later is that we can use scores such as this to drop senses that score above some similarity degree threshold. </p> <p> The angle between the two vectors can easily be calculated with the following formula: </p> <p> <img src="ltximg/kbpedia-concepts-disambiguation_main_8fa9e31902fcb39732a13f13ac5105eaa36976ee.png" alt="kbpedia-concepts-disambiguation_main_8fa9e31902fcb39732a13f13ac5105eaa36976ee.png" /> </p> <p> The following code will calculate the angle between two vectors using this formula: </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">angle</span> <span style="color: #66D9EF;">[</span>a b<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #66D9EF;">Math</span><span style="color: #66D9EF;">/</span><span style="color: #F92672;">toDegrees</span> <span style="color: #A6E22E;">(</span><span style="color: #66D9EF;">Math</span><span style="color: #66D9EF;">/</span>acos <span style="color: #E6DB74;">(</span>/ <span style="color: #FD971F;">(</span>dot-product a b<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>* <span style="color: #F92672;">(</span><span style="color: #66D9EF;">Math</span><span style="color: #66D9EF;">/</span>sqrt <span style="color: #AE81FF;">(</span>dot-product a a<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span><span style="color: #66D9EF;">Math</span><span style="color: #66D9EF;">/</span>sqrt <span style="color: #AE81FF;">(</span>dot-product b b<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <p> OK, so let's get the angle between the example we created above: </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>println <span style="color: #E6DB74;">"BlowingAir degree score:"</span> <span style="color: #66D9EF;">(</span>angle blowingair c<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>println <span style="color: #E6DB74;">"WindProcess degree score:"</span> <span style="color: #66D9EF;">(</span>angle windprocess c<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> BlowingAir degree score: 76.77807342393672 WindProcess degree score: 82.80469144165336 </pre> <p> Since the degree between the context and <code>BlowingAir</code> is smaller than the degree between the context and <code>WindProcess</code>, we keep the word <code>blows</code> as the disambiguated concept. </p> </div> </div> </div> <div id="outline-container-org85b00ef" class="outline-3"> <br/> <h3 id="org85b00ef">Evaluate Model</h3> <div class="outline-text-3" id="text-org85b00ef"> <p> The last step is to evaluate the model we created. What we have to do is to create a function called <code>(evaluate-disambiguation-model)</code> that will read the gold standard file, parse the markup and then perform the disambiguation. Then the <code>precision</code>, <code>recall</code>, <code>acuracy</code> and <code>F1</code> metrics will be calculated to get the performance of this disambiguation system and the models we created for it. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">get-first-sense-vector</span> <span style="color: #66D9EF;">[</span>concepts<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>get-vector <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if</span> <span style="color: #E6DB74;">(</span>> <span style="color: #FD971F;">(</span><span style="color: #F92672;">.indexOf</span> concepts <span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span><span style="color: #FD971F;">)</span> -1<span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>first <span style="color: #FD971F;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split <span style="color: #F92672;">(</span>second <span style="color: #AE81FF;">(</span>re-find #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span> concepts<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> #<span style="color: #E6DB74;">" "</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> concepts<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">get-context</span> <span style="color: #66D9EF;">[</span>i tags<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">if</span> <span style="color: #A6E22E;">(</span>= <span style="color: #E6DB74;">(</span>count tags<span style="color: #E6DB74;">)</span> 1<span style="color: #A6E22E;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">There is only one tag in the sentence</span> <span style="color: #A6E22E;">(</span>get-first-sense-vector <span style="color: #E6DB74;">(</span>last <span style="color: #FD971F;">(</span>first tags<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if</span> <span style="color: #E6DB74;">(</span>= <span style="color: #FD971F;">(</span>count tags<span style="color: #FD971F;">)</span> 2<span style="color: #E6DB74;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">There is only 2 tags in the sentence</span> <span style="color: #E6DB74;">(</span>sum-vectors <span style="color: #FD971F;">(</span>get-first-sense-vector <span style="color: #F92672;">(</span>last <span style="color: #AE81FF;">(</span>first tags<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>get-first-sense-vector <span style="color: #F92672;">(</span>last <span style="color: #AE81FF;">(</span>second tags<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Target concept is the first one of the sentence</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">if</span> <span style="color: #FD971F;">(</span>= i 0<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>sum-vectors <span style="color: #F92672;">(</span>get-first-sense-vector <span style="color: #AE81FF;">(</span>last <span style="color: #66D9EF;">(</span>first tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span>get-first-sense-vector <span style="color: #AE81FF;">(</span>last <span style="color: #66D9EF;">(</span>second tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span>get-first-sense-vector <span style="color: #AE81FF;">(</span>last <span style="color: #66D9EF;">(</span>nth tags <span style="color: #A6E22E;">(</span>+ i 2<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Target concept is the last one of the sentence</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">if</span> <span style="color: #F92672;">(</span>= i <span style="color: #AE81FF;">(</span>- <span style="color: #66D9EF;">(</span>count tags<span style="color: #66D9EF;">)</span> 1<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span>sum-vectors <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>last <span style="color: #A6E22E;">(</span>nth tags <span style="color: #AE81FF;">(</span>- i 2<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>last <span style="color: #A6E22E;">(</span>nth tags <span style="color: #AE81FF;">(</span>- i 1<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>last <span style="color: #A6E22E;">(</span>nth tags i<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Target is in-between</span> <span style="color: #F92672;">(</span>sum-vectors <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>last <span style="color: #A6E22E;">(</span>nth tags <span style="color: #AE81FF;">(</span>- i 1<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>last <span style="color: #A6E22E;">(</span>nth tags i<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>last <span style="color: #A6E22E;">(</span>nth tags <span style="color: #AE81FF;">(</span>+ i 1<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">predict-label</span> <span style="color: #66D9EF;">[</span>ambiguous-concepts i tags max-angle<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>second <span style="color: #A6E22E;">(</span>first <span style="color: #E6DB74;">(</span><span style="color: #F92672;">->></span> ambiguous-concepts <span style="color: #FD971F;">(</span>map <span style="color: #F92672;">(</span><span style="color: #F92672;">fn</span> <span style="color: #AE81FF;">[</span>ambiguous-concept<span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">if-let</span> <span style="color: #66D9EF;">[</span>a <span style="color: #A6E22E;">(</span>get-vector ambiguous-concept<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">if-let</span> <span style="color: #A6E22E;">[</span>b <span style="color: #AE81FF;">(</span>get-context i tags<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if</span> <span style="color: #AE81FF;">(</span>> <span style="color: #66D9EF;">(</span>angle a b<span style="color: #66D9EF;">)</span> max-angle<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">{</span><span style="color: #AE81FF;">}</span> <span style="color: #AE81FF;">{</span><span style="color: #66D9EF;">(</span>angle a b<span style="color: #66D9EF;">)</span> ambiguous-concept<span style="color: #AE81FF;">}</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">{</span><span style="color: #A6E22E;">}</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">{</span><span style="color: #66D9EF;">}</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>apply merge<span style="color: #FD971F;">)</span> sort<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">predict-label-angle</span> <span style="color: #66D9EF;">[</span>ambiguous-concepts i tags<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>first <span style="color: #A6E22E;">(</span><span style="color: #F92672;">->></span> ambiguous-concepts <span style="color: #E6DB74;">(</span>map <span style="color: #FD971F;">(</span><span style="color: #F92672;">fn</span> <span style="color: #F92672;">[</span>ambiguous-concept<span style="color: #F92672;">]</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">if-let</span> <span style="color: #AE81FF;">[</span>a <span style="color: #66D9EF;">(</span>get-vector ambiguous-concept<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">if-let</span> <span style="color: #66D9EF;">[</span>b <span style="color: #A6E22E;">(</span>get-context i tags<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">{</span><span style="color: #A6E22E;">(</span>angle a b<span style="color: #A6E22E;">)</span> ambiguous-concept<span style="color: #66D9EF;">}</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">{</span><span style="color: #AE81FF;">}</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>apply merge<span style="color: #E6DB74;">)</span> sort<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">evaluate-disambiguation-model</span> <span style="color: #66D9EF;">[</span>gold-standard-file & <span style="color: #A6E22E;">{</span><span style="color: #AE81FF;">:keys</span> <span style="color: #E6DB74;">[</span>max-angle<span style="color: #E6DB74;">]</span> <span style="color: #AE81FF;">:or</span> <span style="color: #E6DB74;">{</span>max-angle 90.0<span style="color: #E6DB74;">}</span><span style="color: #A6E22E;">}</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">let</span> <span style="color: #A6E22E;">[</span>sentences <span style="color: #E6DB74;">(</span><span style="color: #F92672;">with-open</span> <span style="color: #FD971F;">[</span>in-file <span style="color: #F92672;">(</span><span style="color: #66D9EF;">io</span><span style="color: #66D9EF;">/</span>reader gold-standard-file<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">doall</span> <span style="color: #F92672;">(</span><span style="color: #66D9EF;">csv</span><span style="color: #66D9EF;">/</span>read-csv in-file <span style="color: #AE81FF;">:separator</span> <span style="color: #E6DB74;">\tab</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> true-positive <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> false-positive <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> true-negative <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> false-negative <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #E6DB74;">[</span><span style="color: #FD971F;">[</span>sentence<span style="color: #FD971F;">]</span> sentences<span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>tags <span style="color: #F92672;">(</span>re-seq #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74;">"</span> sentence<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">loop</span> <span style="color: #F92672;">[</span>i 0 tag <span style="color: #AE81FF;">(</span>first tags<span style="color: #AE81FF;">)</span> rtags <span style="color: #AE81FF;">(</span>rest tags<span style="color: #AE81FF;">)</span><span style="color: #F92672;">]</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">when-not</span> <span style="color: #AE81FF;">(</span>= i <span style="color: #66D9EF;">(</span>count tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Disambiguate concepts</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">let</span> <span style="color: #66D9EF;">[</span>word <span style="color: #A6E22E;">(</span>second tag<span style="color: #A6E22E;">)</span> concepts <span style="color: #A6E22E;">(</span>last tag<span style="color: #A6E22E;">)</span> concept <span style="color: #A6E22E;">(</span>get-tag-concept concepts<span style="color: #A6E22E;">)</span> label <span style="color: #A6E22E;">(</span>second <span style="color: #AE81FF;">(</span>re-find #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74;">"</span> concepts<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> ambiguous-concepts <span style="color: #A6E22E;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split <span style="color: #AE81FF;">(</span>first <span style="color: #66D9EF;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split concepts #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> #<span style="color: #E6DB74;">" "</span><span style="color: #A6E22E;">)</span> predicted-label <span style="color: #A6E22E;">(</span>predict-label ambiguous-concepts i tags max-angle<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">and</span> <span style="color: #AE81FF;">(</span>= label predicted-label<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>not <span style="color: #66D9EF;">(</span>empty? label<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>not <span style="color: #66D9EF;">(</span>empty? predicted-label<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>swap! true-positive inc<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">and</span> <span style="color: #AE81FF;">(</span>not <span style="color: #66D9EF;">(</span>= label predicted-label<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>not <span style="color: #66D9EF;">(</span>empty? predicted-label<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>swap! false-positive inc<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">and</span> <span style="color: #AE81FF;">(</span>empty? label<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>empty? predicted-label<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>swap! true-negative inc<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">and</span> <span style="color: #AE81FF;">(</span>not <span style="color: #66D9EF;">(</span>empty? label<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>empty? predicted-label<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>swap! false-negative inc<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">recur</span> <span style="color: #A6E22E;">(</span>inc i<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>first rtags<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>rest rtags<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"True positive: "</span> @true-positive<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"false positive: "</span> @false-positive<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"True negative: "</span> @true-negative<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"False negative: "</span> @false-negative<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if</span> <span style="color: #E6DB74;">(</span>= 0 @true-positive<span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>precision 0 recall 0 accuracy 0 f1 0<span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Precision: "</span> precision<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Recall: "</span> recall<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Accuracy: "</span> accuracy<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"F1: "</span> f1<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">{</span><span style="color: #AE81FF;">:precision</span> precision <span style="color: #AE81FF;">:recall</span> recall <span style="color: #AE81FF;">:accuracy</span> accuracy <span style="color: #AE81FF;">:f1</span> f1<span style="color: #FD971F;">}</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>precision <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ @true-positive <span style="color: #66D9EF;">(</span>+ @true-positive @false-positive<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> recall <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ @true-positive <span style="color: #66D9EF;">(</span>+ @true-positive @false-negative<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> accuracy <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ <span style="color: #66D9EF;">(</span>+ @true-positive @true-negative<span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>+ @true-positive @false-negative @false-positive @true-negative<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> f1 <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>* 2 <span style="color: #66D9EF;">(</span>/ <span style="color: #A6E22E;">(</span>* precision recall<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>+ precision recall<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Precision: "</span> precision<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Recall: "</span> recall<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Accuracy: "</span> accuracy<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"F1: "</span> f1<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">{</span><span style="color: #AE81FF;">:precision</span> precision <span style="color: #AE81FF;">:recall</span> recall <span style="color: #AE81FF;">:accuracy</span> accuracy <span style="color: #AE81FF;">:f1</span> f1<span style="color: #FD971F;">}</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> </div> <div id="outline-container-org08f46b5" class="outline-4"> <br/> <h4 id="org08f46b5">Evaluation Baseline: Random Sense</h4> <div class="outline-text-4" id="text-org08f46b5"> <p> Before evaluating the initial model we described above, the first thing we should be doing is to check the result of each metric we want to evaluate if we take a concept at random. To do this, we will modify the <code>(evaluate-disambiguation-model)</code> function we created above to perform the selection of the "disambiguated" sense at random. This will provide the baseline to evaluate the other algorithms we will test. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">evaluate-disambiguation-model-random</span> <span style="color: #66D9EF;">[</span>gold-standard-file & <span style="color: #A6E22E;">{</span><span style="color: #AE81FF;">:keys</span> <span style="color: #E6DB74;">[</span>max-angle<span style="color: #E6DB74;">]</span> <span style="color: #AE81FF;">:or</span> <span style="color: #E6DB74;">{</span>max-angle 90.0<span style="color: #E6DB74;">}</span><span style="color: #A6E22E;">}</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">let</span> <span style="color: #A6E22E;">[</span>sentences <span style="color: #E6DB74;">(</span><span style="color: #F92672;">with-open</span> <span style="color: #FD971F;">[</span>in-file <span style="color: #F92672;">(</span><span style="color: #66D9EF;">io</span><span style="color: #66D9EF;">/</span>reader gold-standard-file<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">doall</span> <span style="color: #F92672;">(</span><span style="color: #66D9EF;">csv</span><span style="color: #66D9EF;">/</span>read-csv in-file <span style="color: #AE81FF;">:separator</span> <span style="color: #E6DB74;">\tab</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> true-positive <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> false-positive <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> true-negative <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> false-negative <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #E6DB74;">[</span><span style="color: #FD971F;">[</span>sentence<span style="color: #FD971F;">]</span> sentences<span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>tags <span style="color: #F92672;">(</span>re-seq #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74;">"</span> sentence<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">loop</span> <span style="color: #F92672;">[</span>i 0 tag <span style="color: #AE81FF;">(</span>first tags<span style="color: #AE81FF;">)</span> rtags <span style="color: #AE81FF;">(</span>rest tags<span style="color: #AE81FF;">)</span><span style="color: #F92672;">]</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">when-not</span> <span style="color: #AE81FF;">(</span>= i <span style="color: #66D9EF;">(</span>count tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Disambiguate concepts</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">let</span> <span style="color: #66D9EF;">[</span>word <span style="color: #A6E22E;">(</span>second tag<span style="color: #A6E22E;">)</span> concepts <span style="color: #A6E22E;">(</span>last tag<span style="color: #A6E22E;">)</span> concept <span style="color: #A6E22E;">(</span>get-tag-concept concepts<span style="color: #A6E22E;">)</span> label <span style="color: #A6E22E;">(</span>second <span style="color: #AE81FF;">(</span>re-find #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74;">"</span> concepts<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> ambiguous-concepts <span style="color: #A6E22E;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split <span style="color: #AE81FF;">(</span>first <span style="color: #66D9EF;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split concepts #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> #<span style="color: #E6DB74;">" "</span><span style="color: #A6E22E;">)</span> predicted-label <span style="color: #A6E22E;">(</span>rand-nth <span style="color: #AE81FF;">(</span>into ambiguous-concepts <span style="color: #66D9EF;">[</span><span style="color: #AE81FF;">nil</span><span style="color: #66D9EF;">]</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">and</span> <span style="color: #AE81FF;">(</span>= label predicted-label<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>not <span style="color: #66D9EF;">(</span>empty? label<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>not <span style="color: #66D9EF;">(</span>empty? predicted-label<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>swap! true-positive inc<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">and</span> <span style="color: #AE81FF;">(</span>not <span style="color: #66D9EF;">(</span>= label predicted-label<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>not <span style="color: #66D9EF;">(</span>empty? predicted-label<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>swap! false-positive inc<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">and</span> <span style="color: #AE81FF;">(</span>empty? label<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>empty? predicted-label<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>swap! true-negative inc<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">and</span> <span style="color: #AE81FF;">(</span>not <span style="color: #66D9EF;">(</span>empty? label<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>empty? predicted-label<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>swap! false-negative inc<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">recur</span> <span style="color: #A6E22E;">(</span>inc i<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>first rtags<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>rest rtags<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if</span> <span style="color: #E6DB74;">(</span>= 0 @true-positive<span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>precision 0 recall 0 accuracy 0 f1 0<span style="color: #FD971F;">]</span> <span style="color: #FD971F;">{</span><span style="color: #AE81FF;">:precision</span> precision <span style="color: #AE81FF;">:recall</span> recall <span style="color: #AE81FF;">:accuracy</span> accuracy <span style="color: #AE81FF;">:f1</span> f1<span style="color: #FD971F;">}</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>precision <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ @true-positive <span style="color: #66D9EF;">(</span>+ @true-positive @false-positive<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> recall <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ @true-positive <span style="color: #66D9EF;">(</span>+ @true-positive @false-negative<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> accuracy <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ <span style="color: #66D9EF;">(</span>+ @true-positive @true-negative<span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>+ @true-positive @false-negative @false-positive @true-negative<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> f1 <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>* 2 <span style="color: #66D9EF;">(</span>/ <span style="color: #A6E22E;">(</span>* precision recall<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>+ precision recall<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">{</span><span style="color: #AE81FF;">:precision</span> precision <span style="color: #AE81FF;">:recall</span> recall <span style="color: #AE81FF;">:accuracy</span> accuracy <span style="color: #AE81FF;">:f1</span> f1<span style="color: #FD971F;">}</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <p> What we do is to calculate the average random sense disambiguation score for each metric. We run the <code>(evaluate-disambiguation-model-random)</code> one thousand times and then calculate the average. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">let</span> <span style="color: #66D9EF;">[</span>f1 <span style="color: #A6E22E;">(</span>atom 0<span style="color: #A6E22E;">)</span> precision <span style="color: #A6E22E;">(</span>atom 0<span style="color: #A6E22E;">)</span> accuracy <span style="color: #A6E22E;">(</span>atom 0<span style="color: #A6E22E;">)</span> recall <span style="color: #A6E22E;">(</span>atom 0<span style="color: #A6E22E;">)</span> nb <span style="color: #A6E22E;">(</span>atom 0<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">dotimes</span> <span style="color: #A6E22E;">[</span>i 1000<span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">let</span> <span style="color: #E6DB74;">[</span>results <span style="color: #FD971F;">(</span>evaluate-disambiguation-model-random <span style="color: #E6DB74;">"resources/disambiguation-gold.standard.csv"</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span>swap! nb inc<span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>swap! f1 + <span style="color: #FD971F;">(</span><span style="color: #AE81FF;">:f1</span> results<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>swap! precision + <span style="color: #FD971F;">(</span><span style="color: #AE81FF;">:precision</span> results<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>swap! recall + <span style="color: #FD971F;">(</span><span style="color: #AE81FF;">:recall</span> results<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>swap! accuracy + <span style="color: #FD971F;">(</span><span style="color: #AE81FF;">:accuracy</span> results<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>println <span style="color: #E6DB74;">"Average precision: "</span> <span style="color: #A6E22E;">(</span>/ @precision @nb<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>println <span style="color: #E6DB74;">"Average recall: "</span> <span style="color: #A6E22E;">(</span>/ @recall @nb<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>println <span style="color: #E6DB74;">"Average accuracy: "</span> <span style="color: #A6E22E;">(</span>/ @accuracy @nb<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>println <span style="color: #E6DB74;">"Average F1: "</span> <span style="color: #A6E22E;">(</span>/ @f1 @nb<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> Average precision: 0.6080861368775368 Average recall: 0.49945064118504523 Average accuracy: 0.42075299778580666 Average F1: 0.5480979214012622 </pre> <p> This gives us a baseline <code>precision</code> score of <code>0.608</code>, <code>recall</code> score of <code>0.499</code>, <code>accuracy</code> score of <code>0.420</code> and <code>F1</code> score of <code>0.548</code>. </p> </div> </div> <div id="outline-container-orgfd10448" class="outline-4"> <br/> <h4 id="orgfd10448">Evaluation: First Sense Context</h4> <div class="outline-text-4" id="text-orgfd10448"> <p> Now that we have our baseline in place, let's see the performance of the initial <code>first-sensse-context</code> disambiguation algorithm as described above compared to a random process. Note that this initial run is using an unoptimized DeepWalk graph with initial general hyperparameters. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>evaluate-disambiguation-model <span style="color: #E6DB74;">"resources/disambiguation-gold.standard.csv"</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> True positive: 246 false positive: 126 True negative: 9 False negative: 36 Precision: 0.66129035 Recall: 0.87234044 Accuracy: 0.6115108 F1: 0.7522936 </pre> <p> As you can see, even using unoptimized default hyperparameters, we calculate an <code>F1</code> score of <code>0.752</code>, which is an increase of <code>37.26%</code> over the baseline. Other measures improve, too: <code>Recall</code> (increased by <code>74.67%</code>), <code>accuracy</code> (increased by <code>45.35%</code>) and <code>precision</code> (increased by <code>8.75%</code>). </p> <p> We properly identified <code>246</code> of <code>417</code> examples. <code>126</code> were wrongly identified, <code>9</code> were properly discarded and <code>36</code> have been discarded when they shouldn't. </p> <p> These results are really promising, but can we do better? </p> </div> </div> </div> <div id="outline-container-orgb405bb4" class="outline-3"> <br/> <h3 id="orgb405bb4">Hyperparameter Optimization</h3> <div class="outline-text-3" id="text-orgb405bb4"> <p> Now that we have a disambiguation workflow in place with standards by which to compute performance statistics, the final step is to try to optimize the system by testing multiple different values for some of the hyperparameters that can impact the performance of the system. The parameters we want to optimize are the: </p> <ol class="org-ol"> <li>Size of the DeepWalk vectors</li> <li>Window size of the skip-gram</li> <li>Depth of the walks</li> <li>Number of iterations per concept, and the</li> <li>Angle threshold</li> </ol> <p> Then we will perform a <a href="https://en.wikipedia.org/wiki/Hyperparameter_optimization#Grid_search">grid search</a> to find the optimal hyperparameters to try to optimize each of the metric. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">svm-grid-search</span> <span style="color: #66D9EF;">[</span>graph & <span style="color: #A6E22E;">{</span><span style="color: #AE81FF;">:keys</span> <span style="color: #E6DB74;">[</span>grid-parameters<span style="color: #E6DB74;">]</span> <span style="color: #AE81FF;">:or</span> <span style="color: #E6DB74;">{</span>grid-parameters <span style="color: #FD971F;">{</span><span style="color: #AE81FF;">:window-size</span> <span style="color: #F92672;">[</span>10 15<span style="color: #F92672;">]</span> <span style="color: #AE81FF;">:vector-size</span> <span style="color: #F92672;">[</span>3 16 64 128 256 512<span style="color: #F92672;">]</span> <span style="color: #AE81FF;">:walk-length</span> <span style="color: #F92672;">[</span>5 10 15<span style="color: #F92672;">]</span> <span style="color: #AE81FF;">:walks-per-vertex</span> <span style="color: #F92672;">[</span>32 64 128 256<span style="color: #F92672;">]</span> <span style="color: #AE81FF;">:angle</span> <span style="color: #F92672;">[</span>30 60 90 120<span style="color: #F92672;">]</span> <span style="color: #AE81FF;">:selection-metrics</span> <span style="color: #F92672;">[</span><span style="color: #AE81FF;">:precision</span> <span style="color: #AE81FF;">:accuracy</span> <span style="color: #AE81FF;">:recall</span> <span style="color: #AE81FF;">:f1</span><span style="color: #F92672;">]</span><span style="color: #FD971F;">}</span><span style="color: #E6DB74;">}</span><span style="color: #A6E22E;">}</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">let</span> <span style="color: #A6E22E;">[</span>best <span style="color: #E6DB74;">(</span>atom <span style="color: #FD971F;">(</span><span style="color: #F92672;">->></span> <span style="color: #F92672;">(</span><span style="color: #AE81FF;">:selection-metrics</span> grid-parameters<span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span>map <span style="color: #AE81FF;">(</span><span style="color: #F92672;">fn</span> <span style="color: #66D9EF;">[</span>selection-metric<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">{</span>selection-metric <span style="color: #A6E22E;">{</span><span style="color: #AE81FF;">:window-size</span> <span style="color: #AE81FF;">nil</span> <span style="color: #AE81FF;">:vector-size</span> <span style="color: #AE81FF;">nil</span> <span style="color: #AE81FF;">:walk-length</span> <span style="color: #AE81FF;">nil</span> <span style="color: #AE81FF;">:walks-per-vertex</span> <span style="color: #AE81FF;">nil</span> <span style="color: #AE81FF;">:angle</span> <span style="color: #AE81FF;">nil</span> <span style="color: #AE81FF;">:score</span> 0.0<span style="color: #A6E22E;">}</span><span style="color: #66D9EF;">}</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span>apply merge<span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> parameters grid-parameters<span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #E6DB74;">[</span>window-size <span style="color: #FD971F;">(</span><span style="color: #AE81FF;">:window-size</span> parameters<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #FD971F;">[</span>vector-size <span style="color: #F92672;">(</span><span style="color: #AE81FF;">:vector-size</span> parameters<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">let</span> <span style="color: #F92672;">[</span>deep-walk <span style="color: #AE81FF;">(</span>create-deep-walk graph <span style="color: #AE81FF;">:window-size</span> window-size <span style="color: #AE81FF;">:vector-size</span> vector-size <span style="color: #AE81FF;">:learning-rate</span> 0.025<span style="color: #AE81FF;">)</span><span style="color: #F92672;">]</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #AE81FF;">[</span>walk-length <span style="color: #66D9EF;">(</span><span style="color: #AE81FF;">:walk-length</span> parameters<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #66D9EF;">[</span>walks-per-vertex <span style="color: #A6E22E;">(</span><span style="color: #AE81FF;">:walks-per-vertex</span> parameters<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>train deep-walk graph walk-length walks-per-vertex<span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #A6E22E;">[</span>angle <span style="color: #AE81FF;">(</span><span style="color: #AE81FF;">:angle</span> parameters<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">let</span> <span style="color: #AE81FF;">[</span>results <span style="color: #66D9EF;">(</span><span style="color: #F92672;">binding</span> <span style="color: #A6E22E;">[</span>vertex-vectors <span style="color: #E6DB74;">(</span><span style="color: #F92672;">.getVertexVectors</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">.lookupTable</span> deep-walk<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span>evaluate-disambiguation-model <span style="color: #E6DB74;">"resources/disambiguation-gold.standard.csv"</span> <span style="color: #AE81FF;">:max-angle</span> angle<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #66D9EF;">[</span>selection-metric <span style="color: #A6E22E;">(</span><span style="color: #AE81FF;">:selection-metrics</span> parameters<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">let</span> <span style="color: #A6E22E;">[</span>score <span style="color: #E6DB74;">(</span>get results selection-metric<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span>println<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"Window size: "</span> window-size<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"Vector size: "</span> vector-size<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"Walk length: "</span> walk-length<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"Walks per vertex: "</span> walks-per-vertex<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"Angle: "</span> angle<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"Score ("</span>selection-metric<span style="color: #E6DB74;">"): "</span> score<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">when</span> <span style="color: #E6DB74;">(</span>> score <span style="color: #FD971F;">(</span><span style="color: #AE81FF;">:score</span> <span style="color: #F92672;">(</span>get @best selection-metric<span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>reset! best <span style="color: #FD971F;">(</span>assoc-in @best <span style="color: #F92672;">[</span>selection-metric<span style="color: #F92672;">]</span> <span style="color: #F92672;">{</span><span style="color: #AE81FF;">:window-size</span> window-size <span style="color: #AE81FF;">:vector-size</span> vector-size <span style="color: #AE81FF;">:walk-length</span> walk-length <span style="color: #AE81FF;">:walks-per-vertex</span> walks-per-vertex <span style="color: #AE81FF;">:angle</span> angle <span style="color: #AE81FF;">:score</span> score<span style="color: #F92672;">}</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> @best<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>svm-grid-search graph <span style="color: #AE81FF;">:grid-parameters</span> <span style="color: #66D9EF;">{</span><span style="color: #AE81FF;">:window-size</span> <span style="color: #A6E22E;">[</span>10 15 20<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:vector-size</span> <span style="color: #A6E22E;">[</span>64 128 256<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walk-length</span> <span style="color: #A6E22E;">[</span>5 10 15<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walks-per-vertex</span> <span style="color: #A6E22E;">[</span>32 64 128<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:angle</span> <span style="color: #A6E22E;">[</span>60 90 120<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:selection-metrics</span> <span style="color: #A6E22E;">[</span><span style="color: #AE81FF;">:precision</span> <span style="color: #AE81FF;">:accuracy</span> <span style="color: #AE81FF;">:recall</span> <span style="color: #AE81FF;">:f1</span><span style="color: #A6E22E;">]</span><span style="color: #66D9EF;">}</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> {:precision {:window-size 10, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 60, :score 0.6812866}, :accuracy {:window-size 10, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 0.676259}, :recall {:window-size 10, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 0.99640286}, :f1 {:window-size 10, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 0.80406386}} </pre> <p> For the same <code>first-sense-context</code> disambiguation algorithm, once the key hyperparameters of the pipeline are optimized, then we endup with a <code>F1</code> score of <code>0.8040</code>, which is an increase of <code>46.72%</code> over the baseline. <code>Recall</code> has now increased by <code>99.52%</code>, <code>accuracy</code> increased by <code>60.73%</code> and <code>precision</code> increased by <code>12.04%</code>. </p> <p> Still, understand that when performing the grid search in this manner that many of the hyperparameters we are trying to optimize are related to the DeepWalk algorithm. Because of the nature of the algorithm (when the model is trained, the paths are randomly created and the learning starts with random seeds) we can't reproduce exactly the same results every time we re-create a <code>DeepWalk</code> instance. However the results tend to be similar with a differences of <code>+/- 0.02</code> for most of the metrics. </p> <p> Depending on the task at hand we don't necessarly want to keep the hyperparameters of the best score. If model creation and execution speed is also a governing consideration, we may want to sacrifice <code>0.03</code> of the <code>F1</code> score to have the <code>:window-size</code>, <code>:vector-size</code>, <code>:walk-length</code> and <code>:walk-per-vertex</code> as small as possible. </p> <p> But in any case, these numbers indicate the kind of performances we may expect from this disambiguation process. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">deep-walk</span> <span style="color: #66D9EF;">(</span>create-deep-walk graph <span style="color: #AE81FF;">:window-size</span> 10 <span style="color: #AE81FF;">:vector-size</span> 128 <span style="color: #AE81FF;">:learning-rate</span> 0.025<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>train deep-walk graph 10 128<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">binding</span> <span style="color: #66D9EF;">[</span>vertex-vectors <span style="color: #A6E22E;">(</span><span style="color: #F92672;">.getVertexVectors</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">.lookupTable</span> deep-walk<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>evaluate-disambiguation-model <span style="color: #E6DB74;">"resources/disambiguation-gold.standard.csv"</span> <span style="color: #AE81FF;">:max-angle</span> 150<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> True positive: 229 false positive: 123 True negative: 5 False negative: 1 Precision: 0.6505682 Recall: 0.9956522 Accuracy: 0.65363127 F1: 0.7869416 </pre> </div> </div> <div id="outline-container-org6b90589" class="outline-3"> <br/> <h3 id="org6b90589">Different Sliding Window Strategies</h3> <div class="outline-text-3" id="text-org6b90589"> <p> Another part of this disambiguation process that has an impact on results is how the "context" of a word is being created. There are multiple ways to define a context, such as extending the window size or letting window size extend over sentence boundaries. For clarity's sake, we initially defined the context <img src="ltximg/kbpedia-concepts-disambiguation_main_774d0389f3a45245568f0233190a445a7ad65f53.png" alt="kbpedia-concepts-disambiguation_main_774d0389f3a45245568f0233190a445a7ad65f53.png" /> as: </p> <p> <img src="ltximg/kbpedia-concepts-disambiguation_main_73e561775d087190d01c276973ae7b6941ab72e7.png" alt="kbpedia-concepts-disambiguation_main_73e561775d087190d01c276973ae7b6941ab72e7.png" /> </p> <p> One of the issues with this definition of "context" is that, so far, <img src="ltximg/kbpedia-concepts-disambiguation_main_70280f6b9061479517c21b3204e4d4aaaccf740f.png" alt="kbpedia-concepts-disambiguation_main_70280f6b9061479517c21b3204e4d4aaaccf740f.png" /> (the senses for each word) is always <code>1</code>, which means that the context is always defined with the first sense of a word, whatever it is. This is why we called this method the <code>first-sense-context</code> disambiguation. Biasing the context to the first sense ignores the other senses that may be summed into the context vector. However, this initial definition is simple enough and, as we saw above, we still have adequate results with it. Still, what could be other strategies? </p> </div> <div id="outline-container-org3c3312b" class="outline-4"> <br/> <h4 id="org3c3312b">Ignore Target Word Senses In Context</h4> <div class="outline-text-4" id="text-org3c3312b"> <p> The first test we may do is to remove the target word's sense (the word we are trying to disambiguate) from the context. That way, we would make sure that we don't bias the context toward that sense. The context would be defined as: </p> <p> <img src="ltximg/kbpedia-concepts-disambiguation_main_727c862fa4ba91e94a62e87da99e9a4131c26a83.png" alt="kbpedia-concepts-disambiguation_main_727c862fa4ba91e94a62e87da99e9a4131c26a83.png" /> </p> <p> We are still picking the first sense of each word that belongs to the context, but we are not considering that sense for the target word when we calculate the context vector. Let's see what is the impact of changing the context with this new definition. To test this new context, we have to change the <code>(get-context)</code> function to reflect the new machanism and to re-test and re-optimize the hyperparameters. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">get-context</span> <span style="color: #66D9EF;">[</span>i tags<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">if</span> <span style="color: #A6E22E;">(</span>= <span style="color: #E6DB74;">(</span>count tags<span style="color: #E6DB74;">)</span> 1<span style="color: #A6E22E;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">There is only one tag in the sentence</span> <span style="color: #A6E22E;">(</span>get-first-sense-vector <span style="color: #E6DB74;">(</span>last <span style="color: #FD971F;">(</span>first tags<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if</span> <span style="color: #E6DB74;">(</span>= <span style="color: #FD971F;">(</span>count tags<span style="color: #FD971F;">)</span> 2<span style="color: #E6DB74;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">There is only 2 tags in the sentence</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">if</span> <span style="color: #FD971F;">(</span>= i 0<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>sum-vectors <span style="color: #F92672;">(</span>get-first-sense-vector <span style="color: #AE81FF;">(</span>last <span style="color: #66D9EF;">(</span>second tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>sum-vectors <span style="color: #F92672;">(</span>get-first-sense-vector <span style="color: #AE81FF;">(</span>last <span style="color: #66D9EF;">(</span>first tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Target concept is the first one of the sentence</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">if</span> <span style="color: #FD971F;">(</span>= i 0<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>sum-vectors <span style="color: #F92672;">(</span>get-first-sense-vector <span style="color: #AE81FF;">(</span>last <span style="color: #66D9EF;">(</span>second tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span>get-first-sense-vector <span style="color: #AE81FF;">(</span>last <span style="color: #66D9EF;">(</span>nth tags <span style="color: #A6E22E;">(</span>+ i 2<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Target concept is the last one of the sentence</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">if</span> <span style="color: #F92672;">(</span>= i <span style="color: #AE81FF;">(</span>- <span style="color: #66D9EF;">(</span>count tags<span style="color: #66D9EF;">)</span> 1<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span>sum-vectors <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>last <span style="color: #A6E22E;">(</span>nth tags <span style="color: #AE81FF;">(</span>- i 2<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>last <span style="color: #A6E22E;">(</span>nth tags <span style="color: #AE81FF;">(</span>- i 1<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Target is in-between</span> <span style="color: #F92672;">(</span>sum-vectors <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>last <span style="color: #A6E22E;">(</span>nth tags <span style="color: #AE81FF;">(</span>- i 1<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>last <span style="color: #A6E22E;">(</span>nth tags <span style="color: #AE81FF;">(</span>+ i 1<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <p> Now let's re-optimize and re-evaluate the impact of this modification on our gold standard: </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>svm-grid-search graph <span style="color: #AE81FF;">:grid-parameters</span> <span style="color: #66D9EF;">{</span><span style="color: #AE81FF;">:window-size</span> <span style="color: #A6E22E;">[</span>10 15 20<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:vector-size</span> <span style="color: #A6E22E;">[</span>64 128 256<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walk-length</span> <span style="color: #A6E22E;">[</span>5 10 15<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walks-per-vertex</span> <span style="color: #A6E22E;">[</span>32 64 128<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:angle</span> <span style="color: #A6E22E;">[</span>60 90 120<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:selection-metrics</span> <span style="color: #A6E22E;">[</span><span style="color: #AE81FF;">:precision</span> <span style="color: #AE81FF;">:accuracy</span> <span style="color: #AE81FF;">:recall</span> <span style="color: #AE81FF;">:f1</span><span style="color: #A6E22E;">]</span><span style="color: #66D9EF;">}</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> {:precision {:window-size 20, :vector-size 128, :walk-length 5, :walks-per-vertex 32, :angle 120, :score 0.6666667}, :accuracy {:window-size 20, :vector-size 128, :walk-length 5, :walks-per-vertex 32, :angle 120, :score 0.66906476}, :recall {:window-size 20, :vector-size 128, :walk-length 5, :walks-per-vertex 32, :angle 120, :score 0.99636364}, :f1 {:window-size 20, :vector-size 128, :walk-length 5, :walks-per-vertex 32, :angle 120, :score 0.79883385}} </pre> <p> With this modification to the context creation algorithm, every metric slightly dropped compared to our previous version of the <code>first-sense</code> disambiguation algorithm. However, this modification of the algorithm has little impact on the final results if we consider the margin of error we have for the <code>F1</code> score incurred by DeepWalk's random walks and training process. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">deep-walk</span> <span style="color: #66D9EF;">(</span>create-deep-walk graph <span style="color: #AE81FF;">:window-size</span> 15 <span style="color: #AE81FF;">:vector-size</span> 128 <span style="color: #AE81FF;">:learning-rate</span> 0.025<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>train deep-walk graph 15 128<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">binding</span> <span style="color: #66D9EF;">[</span>vertex-vectors <span style="color: #A6E22E;">(</span><span style="color: #F92672;">.getVertexVectors</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">.lookupTable</span> deep-walk<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>evaluate-disambiguation-model <span style="color: #E6DB74;">"resources/disambiguation-gold.standard.csv"</span> <span style="color: #AE81FF;">:max-angle</span> 120<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> </div> </div> <div id="outline-container-org5b25cfc" class="outline-4"> <br/> <h4 id="org5b25cfc">Consider Multiple Contexts</h4> <div class="outline-text-4" id="text-org5b25cfc"> <p> Another modification we will test is to define not a single context but multiple contexts and to keep the most relevant one. So far, we only used the top sense for each word to define the context. The truth is that most of the words have multiple possible senses, which means that we are currently ignoring them. Let's again revisit our initial example: </p> <div class="figure"> <p><img src="sentence.png" alt="sentence.png" /> </p> </div> <p> If the target word is <code>buildings</code> then the contexts we created so far only considered the senses <img src="ltximg/kbpedia-concepts-disambiguation_main_d65b40ecc3113c0303a621e3360a8e2f51df152f.png" alt="kbpedia-concepts-disambiguation_main_d65b40ecc3113c0303a621e3360a8e2f51df152f.png" />, <img src="ltximg/kbpedia-concepts-disambiguation_main_7986902a6150fca4666ab80489c5e19f57d3c27c.png" alt="kbpedia-concepts-disambiguation_main_7986902a6150fca4666ab80489c5e19f57d3c27c.png" /> and <img src="ltximg/kbpedia-concepts-disambiguation_main_77b1bb8b1889bda6d392cbafb767fa4f6c24fc97.png" alt="kbpedia-concepts-disambiguation_main_77b1bb8b1889bda6d392cbafb767fa4f6c24fc97.png" /> but what about the other two? What we want to do here is to create multiple contexts and then to compare each of the senses of the target word against each of these contexts. Then we will check the angle between each of the senses of the target word against each of the contexts. Then we will keep the sense that is most closely related to one of the context vectors. </p> <p> The contexts are simply created out of all the possible combinations between each of the senses of each words within the window. With the example above, the possible combinations are: </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">for</span> <span style="color: #66D9EF;">[</span>x <span style="color: #A6E22E;">[</span><span style="color: #E6DB74;">[</span>1 -9 -2<span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">[</span>2 3 -7<span style="color: #E6DB74;">]</span><span style="color: #A6E22E;">]</span> y <span style="color: #A6E22E;">[</span><span style="color: #E6DB74;">[</span>-8 1 1<span style="color: #E6DB74;">]</span><span style="color: #A6E22E;">]</span> z <span style="color: #A6E22E;">[</span><span style="color: #E6DB74;">[</span>-1 -9 0<span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">[</span>0 3 -1<span style="color: #E6DB74;">]</span><span style="color: #A6E22E;">]</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #66D9EF;">clojure.pprint</span><span style="color: #66D9EF;">/</span>pprint <span style="color: #A6E22E;">(</span>vector x y x<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> [[1 -9 -2] [-8 1 1] [1 -9 -2]] [[1 -9 -2] [-8 1 1] [1 -9 -2]] [[2 3 -7] [-8 1 1] [2 3 -7]] [[2 3 -7] [-8 1 1] [2 3 -7]] </pre> <p> To now compare this larger number of combinations, we need to modify the evaluation procedure. We have to modify how the contexts are created and how all possible contexts are generated. Then we have to modify how we predict the label for an example by using the most relevant context. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">disambiguate</span> <span style="color: #66D9EF;">[</span>line<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">let</span> <span style="color: #A6E22E;">[</span>tags <span style="color: #E6DB74;">(</span>re-seq #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74;">"</span> line<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">loop</span> <span style="color: #E6DB74;">[</span>i 0 tag <span style="color: #FD971F;">(</span>first tags<span style="color: #FD971F;">)</span> rtags <span style="color: #FD971F;">(</span>rest tags<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>word <span style="color: #F92672;">(</span>second tag<span style="color: #F92672;">)</span> concepts <span style="color: #F92672;">(</span>last tag<span style="color: #F92672;">)</span> concept <span style="color: #F92672;">(</span>get-tag-concept concepts<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span>println word <span style="color: #E6DB74;">" --> "</span> concepts<span style="color: #FD971F;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Disambiguate concepts</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">let</span> <span style="color: #F92672;">[</span>ambiguous-concepts <span style="color: #AE81FF;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split <span style="color: #66D9EF;">(</span>first <span style="color: #A6E22E;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split concepts #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> #<span style="color: #E6DB74;">" "</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">]</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #AE81FF;">[</span>ambiguous-concept ambiguous-concepts<span style="color: #AE81FF;">]</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">(println "a-vector: " (get-vector ambiguous-concept))</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">let</span> <span style="color: #66D9EF;">[</span>unambiguous <span style="color: #A6E22E;">(</span>first <span style="color: #AE81FF;">(</span><span style="color: #F92672;">->></span> <span style="color: #66D9EF;">(</span>get-contexts i tags<span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>map <span style="color: #A6E22E;">(</span><span style="color: #F92672;">fn</span> <span style="color: #E6DB74;">[</span>context<span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">{</span><span style="color: #FD971F;">(</span>angle <span style="color: #F92672;">(</span>get-vector ambiguous-concept<span style="color: #F92672;">)</span> context<span style="color: #FD971F;">)</span> ambiguous-concept<span style="color: #E6DB74;">}</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>apply merge<span style="color: #66D9EF;">)</span> sort<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">(println "b-vector: " (get-context i tags)) </span> <span style="color: #66D9EF;">(</span>println <span style="color: #E6DB74;">"Unambiguous concept: "</span> <span style="color: #A6E22E;">(</span>second unambiguous<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>println <span style="color: #E6DB74;">"angle: "</span> <span style="color: #A6E22E;">(</span>first unambiguous<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>println<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">when-not</span> <span style="color: #FD971F;">(</span>empty? rtags<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">recur</span> <span style="color: #F92672;">(</span>inc i<span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span>first rtags<span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span>rest rtags<span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>require '<span style="color: #66D9EF;">[</span><span style="color: #66D9EF;">clojure.math.combinatorics</span> <span style="color: #AE81FF;">:as</span> combo<span style="color: #66D9EF;">]</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">get-word-senses</span> <span style="color: #66D9EF;">[</span>i tags<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">let</span> <span style="color: #A6E22E;">[</span>senses <span style="color: #E6DB74;">(</span>last <span style="color: #FD971F;">(</span>nth tags i<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if</span> <span style="color: #E6DB74;">(</span>> <span style="color: #FD971F;">(</span><span style="color: #F92672;">.indexOf</span> senses <span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span><span style="color: #FD971F;">)</span> -1<span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split <span style="color: #FD971F;">(</span>second <span style="color: #F92672;">(</span>re-find #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span> senses<span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span> #<span style="color: #E6DB74;">" "</span><span style="color: #E6DB74;">)</span> senses<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">get-contexts</span> <span style="color: #66D9EF;">[</span>i tags<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">if</span> <span style="color: #A6E22E;">(</span>= <span style="color: #E6DB74;">(</span>count tags<span style="color: #E6DB74;">)</span> 1<span style="color: #A6E22E;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">There is only one tag in the sentence</span> <span style="color: #A6E22E;">(</span>get-first-sense-vector <span style="color: #E6DB74;">(</span>last <span style="color: #FD971F;">(</span>first tags<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if</span> <span style="color: #E6DB74;">(</span>= <span style="color: #FD971F;">(</span>count tags<span style="color: #FD971F;">)</span> 2<span style="color: #E6DB74;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">There is only 2 tags in the sentence</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>contexts <span style="color: #F92672;">(</span><span style="color: #66D9EF;">combo</span><span style="color: #66D9EF;">/</span>cartesian-product <span style="color: #AE81FF;">(</span>get-word-senses 0 tags<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>get-word-senses 1 tags<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">->></span> contexts <span style="color: #F92672;">(</span>mapv <span style="color: #AE81FF;">(</span><span style="color: #F92672;">fn</span> <span style="color: #66D9EF;">[</span>context<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>sum-vectors <span style="color: #A6E22E;">(</span>get-first-sense-vector <span style="color: #AE81FF;">(</span>first context<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>get-first-sense-vector <span style="color: #AE81FF;">(</span>second context<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Target concept is the first one of the sentence</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">if</span> <span style="color: #FD971F;">(</span>= i 0<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">let</span> <span style="color: #F92672;">[</span>contexts <span style="color: #AE81FF;">(</span><span style="color: #66D9EF;">combo</span><span style="color: #66D9EF;">/</span>cartesian-product <span style="color: #66D9EF;">(</span>get-word-senses 0 tags<span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>get-word-senses 1 tags<span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>get-word-senses 2 tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">]</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">->></span> contexts <span style="color: #AE81FF;">(</span>mapv <span style="color: #66D9EF;">(</span><span style="color: #F92672;">fn</span> <span style="color: #A6E22E;">[</span>context<span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span>sum-vectors <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>first context<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>second context<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>get-first-sense-vector <span style="color: #66D9EF;">(</span>nth context 2<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Target concept is the last one of the sentence</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">if</span> <span style="color: #F92672;">(</span>= i <span style="color: #AE81FF;">(</span>- <span style="color: #66D9EF;">(</span>count tags<span style="color: #66D9EF;">)</span> 1<span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">let</span> <span style="color: #AE81FF;">[</span>contexts <span style="color: #66D9EF;">(</span><span style="color: #66D9EF;">combo</span><span style="color: #66D9EF;">/</span>cartesian-product <span style="color: #A6E22E;">(</span>get-word-senses <span style="color: #AE81FF;">(</span>- i 2<span style="color: #AE81FF;">)</span> tags<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>get-word-senses <span style="color: #AE81FF;">(</span>- i 1<span style="color: #AE81FF;">)</span> tags<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>get-word-senses i tags<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">->></span> contexts <span style="color: #66D9EF;">(</span>mapv <span style="color: #A6E22E;">(</span><span style="color: #F92672;">fn</span> <span style="color: #AE81FF;">[</span>context<span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span>sum-vectors <span style="color: #66D9EF;">(</span>get-first-sense-vector <span style="color: #A6E22E;">(</span>first context<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>get-first-sense-vector <span style="color: #A6E22E;">(</span>second context<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>get-first-sense-vector <span style="color: #A6E22E;">(</span>nth context 2<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Target is in-between </span> <span style="color: #F92672;">(</span><span style="color: #F92672;">let</span> <span style="color: #AE81FF;">[</span>contexts <span style="color: #66D9EF;">(</span><span style="color: #66D9EF;">combo</span><span style="color: #66D9EF;">/</span>cartesian-product <span style="color: #A6E22E;">(</span>get-word-senses <span style="color: #AE81FF;">(</span>- i 1<span style="color: #AE81FF;">)</span> tags<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>get-word-senses i tags<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>get-word-senses <span style="color: #AE81FF;">(</span>+ i 1<span style="color: #AE81FF;">)</span> tags<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">->></span> contexts <span style="color: #66D9EF;">(</span>mapv <span style="color: #A6E22E;">(</span><span style="color: #F92672;">fn</span> <span style="color: #AE81FF;">[</span>context<span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span>sum-vectors <span style="color: #66D9EF;">(</span>get-first-sense-vector <span style="color: #A6E22E;">(</span>first context<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>get-first-sense-vector <span style="color: #A6E22E;">(</span>second context<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>get-first-sense-vector <span style="color: #A6E22E;">(</span>nth context 2<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">predict-label</span> <span style="color: #66D9EF;">[</span>ambiguous-concepts i tags max-angle<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>second <span style="color: #A6E22E;">(</span>first <span style="color: #E6DB74;">(</span><span style="color: #F92672;">->></span> ambiguous-concepts <span style="color: #FD971F;">(</span>map <span style="color: #F92672;">(</span><span style="color: #F92672;">fn</span> <span style="color: #AE81FF;">[</span>ambiguous-concept<span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">let</span> <span style="color: #66D9EF;">[</span>unambiguous <span style="color: #A6E22E;">(</span>first <span style="color: #AE81FF;">(</span><span style="color: #F92672;">->></span> <span style="color: #66D9EF;">(</span>get-contexts i tags<span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>map <span style="color: #A6E22E;">(</span><span style="color: #F92672;">fn</span> <span style="color: #E6DB74;">[</span>context<span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">if-let</span> <span style="color: #FD971F;">[</span>a <span style="color: #F92672;">(</span>get-vector ambiguous-concept<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">if-not</span> <span style="color: #F92672;">(</span>nil? context<span style="color: #F92672;">)</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">if</span> <span style="color: #AE81FF;">(</span>> <span style="color: #66D9EF;">(</span>angle a context<span style="color: #66D9EF;">)</span> max-angle<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">{</span><span style="color: #AE81FF;">}</span> <span style="color: #AE81FF;">{</span><span style="color: #66D9EF;">(</span>angle a context<span style="color: #66D9EF;">)</span> ambiguous-concept<span style="color: #AE81FF;">}</span><span style="color: #F92672;">)</span> <span style="color: #F92672;">{</span><span style="color: #F92672;">}</span><span style="color: #FD971F;">)</span> <span style="color: #FD971F;">{</span><span style="color: #FD971F;">}</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>apply merge<span style="color: #66D9EF;">)</span> sort<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">{</span><span style="color: #A6E22E;">(</span>first unambiguous<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>second unambiguous<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">}</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>apply merge<span style="color: #FD971F;">)</span> sort<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">predict-label-angle</span> <span style="color: #66D9EF;">[</span>ambiguous-concepts i tags<span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>first <span style="color: #A6E22E;">(</span><span style="color: #F92672;">->></span> ambiguous-concepts <span style="color: #E6DB74;">(</span>map <span style="color: #FD971F;">(</span><span style="color: #F92672;">fn</span> <span style="color: #F92672;">[</span>ambiguous-concept<span style="color: #F92672;">]</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">let</span> <span style="color: #AE81FF;">[</span>unambiguous <span style="color: #66D9EF;">(</span>first <span style="color: #A6E22E;">(</span><span style="color: #F92672;">->></span> <span style="color: #AE81FF;">(</span>get-contexts i tags<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>map <span style="color: #66D9EF;">(</span><span style="color: #F92672;">fn</span> <span style="color: #A6E22E;">[</span>context<span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if-let</span> <span style="color: #E6DB74;">[</span>a <span style="color: #FD971F;">(</span>get-vector ambiguous-concept<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">if-not</span> <span style="color: #FD971F;">(</span>nil? context<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">{</span><span style="color: #F92672;">(</span>angle a context<span style="color: #F92672;">)</span> ambiguous-concept<span style="color: #FD971F;">}</span> <span style="color: #FD971F;">{</span><span style="color: #FD971F;">}</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">{</span><span style="color: #E6DB74;">}</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>apply merge<span style="color: #AE81FF;">)</span> sort<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">{</span><span style="color: #66D9EF;">(</span>first unambiguous<span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>second unambiguous<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">}</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>apply merge<span style="color: #E6DB74;">)</span> sort<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <p> Now, using this changed disambiguation method, let's optimize the hyperparameters of the pipeline to see how this method can perform compared to the baseline. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>svm-grid-search graph <span style="color: #AE81FF;">:grid-parameters</span> <span style="color: #66D9EF;">{</span><span style="color: #AE81FF;">:window-size</span> <span style="color: #A6E22E;">[</span>10 15 20<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:vector-size</span> <span style="color: #A6E22E;">[</span>64 128 256<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walk-length</span> <span style="color: #A6E22E;">[</span>5 10 15<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walks-per-vertex</span> <span style="color: #A6E22E;">[</span>32 64 128<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:angle</span> <span style="color: #A6E22E;">[</span>60 90 120<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:selection-metrics</span> <span style="color: #A6E22E;">[</span><span style="color: #AE81FF;">:precision</span> <span style="color: #AE81FF;">:accuracy</span> <span style="color: #AE81FF;">:recall</span> <span style="color: #AE81FF;">:f1</span><span style="color: #A6E22E;">]</span><span style="color: #66D9EF;">}</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> {:precision {:window-size 15, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 60, :score 0.69970846}, :accuracy {:window-size 15, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 0.676259}, :recall {:window-size 15, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 0.98566306}, :f1 {:window-size 15, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 0.8029197}} </pre> <p> These results are nearly the same as the <code>first-sense-context</code> method described above. Our first intuition is that this more complicated method may have little or no impact on the overall performance. Yet, as we will see below, this is actually not the case in all situations. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">def</span> <span style="color: #FD971F;">deep-walk</span> <span style="color: #66D9EF;">(</span>create-deep-walk graph <span style="color: #AE81FF;">:window-size</span> 15 <span style="color: #AE81FF;">:vector-size</span> 128 <span style="color: #AE81FF;">:learning-rate</span> 0.025<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span>train deep-walk graph 15 128<span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">binding</span> <span style="color: #66D9EF;">[</span>vertex-vectors <span style="color: #A6E22E;">(</span><span style="color: #F92672;">.getVertexVectors</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">.lookupTable</span> deep-walk<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span>evaluate-disambiguation-model <span style="color: #E6DB74;">"resources/disambiguation-gold.standard.csv"</span> <span style="color: #AE81FF;">:max-angle</span> 120<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> </div> </div> </div> <div id="outline-container-orgc757a26" class="outline-3"> <br/> <h3 id="orgc757a26">Focus Evaluation Of Multiple Senses</h3> <div class="outline-text-3" id="text-orgc757a26"> <p> Now that we evaluated how different strategies perform on the entire gold standard, what we will do next is to evaluate how the different strategies perform when we only focus on the example that has multiple possible senses. To run this evaluation, we will modify the <code>(evaluate-disambiguation-model)</code> function such that we only predict the disambiguated word sense when multiple senses have been tagged for that word. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">evaluate-disambiguation-model</span> <span style="color: #66D9EF;">[</span>gold-standard-file & <span style="color: #A6E22E;">{</span><span style="color: #AE81FF;">:keys</span> <span style="color: #E6DB74;">[</span>max-angle<span style="color: #E6DB74;">]</span> <span style="color: #AE81FF;">:or</span> <span style="color: #E6DB74;">{</span>max-angle 90.0<span style="color: #E6DB74;">}</span><span style="color: #A6E22E;">}</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">let</span> <span style="color: #A6E22E;">[</span>sentences <span style="color: #E6DB74;">(</span><span style="color: #F92672;">with-open</span> <span style="color: #FD971F;">[</span>in-file <span style="color: #F92672;">(</span><span style="color: #66D9EF;">io</span><span style="color: #66D9EF;">/</span>reader gold-standard-file<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">doall</span> <span style="color: #F92672;">(</span><span style="color: #66D9EF;">csv</span><span style="color: #66D9EF;">/</span>read-csv in-file <span style="color: #AE81FF;">:separator</span> <span style="color: #E6DB74;">\tab</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> true-positive <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> false-positive <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> true-negative <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> false-negative <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #E6DB74;">[</span><span style="color: #FD971F;">[</span>sentence<span style="color: #FD971F;">]</span> sentences<span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>tags <span style="color: #F92672;">(</span>re-seq #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74;">"</span> sentence<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">loop</span> <span style="color: #F92672;">[</span>i 0 tag <span style="color: #AE81FF;">(</span>first tags<span style="color: #AE81FF;">)</span> rtags <span style="color: #AE81FF;">(</span>rest tags<span style="color: #AE81FF;">)</span><span style="color: #F92672;">]</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">when-not</span> <span style="color: #AE81FF;">(</span>= i <span style="color: #66D9EF;">(</span>count tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Disambiguate concepts</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">let</span> <span style="color: #66D9EF;">[</span>word <span style="color: #A6E22E;">(</span>second tag<span style="color: #A6E22E;">)</span> concepts <span style="color: #A6E22E;">(</span>last tag<span style="color: #A6E22E;">)</span> concept <span style="color: #A6E22E;">(</span>get-tag-concept concepts<span style="color: #A6E22E;">)</span> label <span style="color: #A6E22E;">(</span>second <span style="color: #AE81FF;">(</span>re-find #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74;">"</span> concepts<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> ambiguous-concepts <span style="color: #A6E22E;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split <span style="color: #AE81FF;">(</span>first <span style="color: #66D9EF;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split concepts #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> #<span style="color: #E6DB74;">" "</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when</span> <span style="color: #A6E22E;">(</span>> <span style="color: #AE81FF;">(</span>count ambiguous-concepts<span style="color: #AE81FF;">)</span> 1<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">let</span> <span style="color: #AE81FF;">[</span>predicted-label <span style="color: #66D9EF;">(</span>predict-label ambiguous-concepts i tags max-angle<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">when</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">and</span> <span style="color: #A6E22E;">(</span>= label predicted-label<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>not <span style="color: #E6DB74;">(</span>empty? label<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>not <span style="color: #E6DB74;">(</span>empty? predicted-label<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>swap! true-positive inc<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">when</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">and</span> <span style="color: #A6E22E;">(</span>not <span style="color: #E6DB74;">(</span>= label predicted-label<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>not <span style="color: #E6DB74;">(</span>empty? predicted-label<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>swap! false-positive inc<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">when</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">and</span> <span style="color: #A6E22E;">(</span>empty? label<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>empty? predicted-label<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>swap! true-negative inc<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">when</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">and</span> <span style="color: #A6E22E;">(</span>not <span style="color: #E6DB74;">(</span>empty? label<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>empty? predicted-label<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>swap! false-negative inc<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">recur</span> <span style="color: #A6E22E;">(</span>inc i<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>first rtags<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>rest rtags<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"True positive: "</span> @true-positive<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"false positive: "</span> @false-positive<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"True negative: "</span> @true-negative<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println <span style="color: #E6DB74;">"False negative: "</span> @false-negative<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>println<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if</span> <span style="color: #E6DB74;">(</span>= 0 @true-positive<span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>precision 0 recall 0 accuracy 0 f1 0<span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Precision: "</span> precision<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Recall: "</span> recall<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Accuracy: "</span> accuracy<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"F1: "</span> f1<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">{</span><span style="color: #AE81FF;">:precision</span> precision <span style="color: #AE81FF;">:recall</span> recall <span style="color: #AE81FF;">:accuracy</span> accuracy <span style="color: #AE81FF;">:f1</span> f1<span style="color: #FD971F;">}</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>precision <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ @true-positive <span style="color: #66D9EF;">(</span>+ @true-positive @false-positive<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> recall <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ @true-positive <span style="color: #66D9EF;">(</span>+ @true-positive @false-negative<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> accuracy <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ <span style="color: #66D9EF;">(</span>+ @true-positive @true-negative<span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>+ @true-positive @false-negative @false-positive @true-negative<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> f1 <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>* 2 <span style="color: #66D9EF;">(</span>/ <span style="color: #A6E22E;">(</span>* precision recall<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>+ precision recall<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Precision: "</span> precision<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Recall: "</span> recall<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"Accuracy: "</span> accuracy<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">(</span>println <span style="color: #E6DB74;">"F1: "</span> f1<span style="color: #FD971F;">)</span> <span style="color: #FD971F;">{</span><span style="color: #AE81FF;">:precision</span> precision <span style="color: #AE81FF;">:recall</span> recall <span style="color: #AE81FF;">:accuracy</span> accuracy <span style="color: #AE81FF;">:f1</span> f1<span style="color: #FD971F;">}</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> </div> <div id="outline-container-org6106185" class="outline-4"> <br/> <h4 id="org6106185">Evaluation At Random</h4> <div class="outline-text-4" id="text-org6106185"> <p> To put the next evaluations into context, the first thing we will do is to check what would be the score for each metric if we would pick the concept at random. What we do is to modify the <code>(evaluate-disambiguation-model)</code> function to take the "disambiguated" concept at random. Then we run that evaluation one thousand times and display the average of these runs for each metric. This will give us the foundation to evaluate the performance of each of the strategies below. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">defn</span> <span style="color: #A6E22E;">evaluate-disambiguation-model-random</span> <span style="color: #66D9EF;">[</span>gold-standard-file & <span style="color: #A6E22E;">{</span><span style="color: #AE81FF;">:keys</span> <span style="color: #E6DB74;">[</span>max-angle<span style="color: #E6DB74;">]</span> <span style="color: #AE81FF;">:or</span> <span style="color: #E6DB74;">{</span>max-angle 90.0<span style="color: #E6DB74;">}</span><span style="color: #A6E22E;">}</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">let</span> <span style="color: #A6E22E;">[</span>sentences <span style="color: #E6DB74;">(</span><span style="color: #F92672;">with-open</span> <span style="color: #FD971F;">[</span>in-file <span style="color: #F92672;">(</span><span style="color: #66D9EF;">io</span><span style="color: #66D9EF;">/</span>reader gold-standard-file<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">doall</span> <span style="color: #F92672;">(</span><span style="color: #66D9EF;">csv</span><span style="color: #66D9EF;">/</span>read-csv in-file <span style="color: #AE81FF;">:separator</span> <span style="color: #E6DB74;">\tab</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> true-positive <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> false-positive <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> true-negative <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span> false-negative <span style="color: #E6DB74;">(</span>atom 0<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">doseq</span> <span style="color: #E6DB74;">[</span><span style="color: #FD971F;">[</span>sentence<span style="color: #FD971F;">]</span> sentences<span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>tags <span style="color: #F92672;">(</span>re-seq #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74; font-weight: bold;">\[</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\]</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">(</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*?</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74; font-weight: bold;">\</span><span style="color: #E6DB74; font-weight: bold;">)</span><span style="color: #E6DB74;">"</span> sentence<span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">(</span><span style="color: #F92672;">loop</span> <span style="color: #F92672;">[</span>i 0 tag <span style="color: #AE81FF;">(</span>first tags<span style="color: #AE81FF;">)</span> rtags <span style="color: #AE81FF;">(</span>rest tags<span style="color: #AE81FF;">)</span><span style="color: #F92672;">]</span> <span style="color: #F92672;">(</span><span style="color: #F92672;">when-not</span> <span style="color: #AE81FF;">(</span>= i <span style="color: #66D9EF;">(</span>count tags<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #75715E; font-style: italic;">;; </span><span style="color: #75715E; font-style: italic;">Disambiguate concepts</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">let</span> <span style="color: #66D9EF;">[</span>word <span style="color: #A6E22E;">(</span>second tag<span style="color: #A6E22E;">)</span> concepts <span style="color: #A6E22E;">(</span>last tag<span style="color: #A6E22E;">)</span> concept <span style="color: #A6E22E;">(</span>get-tag-concept concepts<span style="color: #A6E22E;">)</span> label <span style="color: #A6E22E;">(</span>second <span style="color: #AE81FF;">(</span>re-find #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">(</span><span style="color: #E6DB74;">.*</span><span style="color: #E6DB74;">)</span><span style="color: #E6DB74;">"</span> concepts<span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span> ambiguous-concepts <span style="color: #A6E22E;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split <span style="color: #AE81FF;">(</span>first <span style="color: #66D9EF;">(</span><span style="color: #66D9EF;">string</span><span style="color: #66D9EF;">/</span>split concepts #<span style="color: #E6DB74;">"</span><span style="color: #E6DB74;">::</span><span style="color: #E6DB74;">"</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> #<span style="color: #E6DB74;">" "</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">when</span> <span style="color: #A6E22E;">(</span>> <span style="color: #AE81FF;">(</span>count ambiguous-concepts<span style="color: #AE81FF;">)</span> 1<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">let</span> <span style="color: #AE81FF;">[</span>predicted-label <span style="color: #66D9EF;">(</span>rand-nth <span style="color: #A6E22E;">(</span>into ambiguous-concepts <span style="color: #E6DB74;">[</span><span style="color: #AE81FF;">nil</span><span style="color: #E6DB74;">]</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">]</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">when</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">and</span> <span style="color: #A6E22E;">(</span>= label predicted-label<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>not <span style="color: #E6DB74;">(</span>empty? label<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>not <span style="color: #E6DB74;">(</span>empty? predicted-label<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>swap! true-positive inc<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">when</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">and</span> <span style="color: #A6E22E;">(</span>not <span style="color: #E6DB74;">(</span>= label predicted-label<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>not <span style="color: #E6DB74;">(</span>empty? predicted-label<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>swap! false-positive inc<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">when</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">and</span> <span style="color: #A6E22E;">(</span>empty? label<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>empty? predicted-label<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>swap! true-negative inc<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> <span style="color: #AE81FF;">(</span><span style="color: #F92672;">when</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">and</span> <span style="color: #A6E22E;">(</span>not <span style="color: #E6DB74;">(</span>empty? label<span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>empty? predicted-label<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>swap! false-negative inc<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">recur</span> <span style="color: #A6E22E;">(</span>inc i<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>first rtags<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>rest rtags<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">if</span> <span style="color: #E6DB74;">(</span>= 0 @true-positive<span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>precision 0 recall 0 accuracy 0 f1 0<span style="color: #FD971F;">]</span> <span style="color: #FD971F;">{</span><span style="color: #AE81FF;">:precision</span> precision <span style="color: #AE81FF;">:recall</span> recall <span style="color: #AE81FF;">:accuracy</span> accuracy <span style="color: #AE81FF;">:f1</span> f1<span style="color: #FD971F;">}</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span><span style="color: #F92672;">let</span> <span style="color: #FD971F;">[</span>precision <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ @true-positive <span style="color: #66D9EF;">(</span>+ @true-positive @false-positive<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> recall <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ @true-positive <span style="color: #66D9EF;">(</span>+ @true-positive @false-negative<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> accuracy <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>/ <span style="color: #66D9EF;">(</span>+ @true-positive @true-negative<span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>+ @true-positive @false-negative @false-positive @true-negative<span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span> f1 <span style="color: #F92672;">(</span>float <span style="color: #AE81FF;">(</span>* 2 <span style="color: #66D9EF;">(</span>/ <span style="color: #A6E22E;">(</span>* precision recall<span style="color: #A6E22E;">)</span> <span style="color: #A6E22E;">(</span>+ precision recall<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span><span style="color: #F92672;">)</span><span style="color: #FD971F;">]</span> <span style="color: #FD971F;">{</span><span style="color: #AE81FF;">:precision</span> precision <span style="color: #AE81FF;">:recall</span> recall <span style="color: #AE81FF;">:accuracy</span> accuracy <span style="color: #AE81FF;">:f1</span> f1<span style="color: #FD971F;">}</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span><span style="color: #F92672;">let</span> <span style="color: #66D9EF;">[</span>f1 <span style="color: #A6E22E;">(</span>atom 0<span style="color: #A6E22E;">)</span> precision <span style="color: #A6E22E;">(</span>atom 0<span style="color: #A6E22E;">)</span> recall <span style="color: #A6E22E;">(</span>atom 0<span style="color: #A6E22E;">)</span> accuracy <span style="color: #A6E22E;">(</span>atom 0<span style="color: #A6E22E;">)</span> nb <span style="color: #A6E22E;">(</span>atom 0<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">]</span> <span style="color: #66D9EF;">(</span><span style="color: #F92672;">dotimes</span> <span style="color: #A6E22E;">[</span>i 1000<span style="color: #A6E22E;">]</span> <span style="color: #A6E22E;">(</span><span style="color: #F92672;">let</span> <span style="color: #E6DB74;">[</span>results <span style="color: #FD971F;">(</span>evaluate-disambiguation-model-random <span style="color: #E6DB74;">"resources/disambiguation-gold.standard.csv"</span><span style="color: #FD971F;">)</span><span style="color: #E6DB74;">]</span> <span style="color: #E6DB74;">(</span>swap! nb inc<span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>swap! f1 + <span style="color: #FD971F;">(</span><span style="color: #AE81FF;">:f1</span> results<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>swap! precision + <span style="color: #FD971F;">(</span><span style="color: #AE81FF;">:precision</span> results<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>swap! recall + <span style="color: #FD971F;">(</span><span style="color: #AE81FF;">:recall</span> results<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span> <span style="color: #E6DB74;">(</span>swap! accuracy + <span style="color: #FD971F;">(</span><span style="color: #AE81FF;">:accuracy</span> results<span style="color: #FD971F;">)</span><span style="color: #E6DB74;">)</span><span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>println <span style="color: #E6DB74;">"Average precision: "</span> <span style="color: #A6E22E;">(</span>/ @precision @nb<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>println <span style="color: #E6DB74;">"Average recall: "</span> <span style="color: #A6E22E;">(</span>/ @recall @nb<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>println <span style="color: #E6DB74;">"Average accuracy: "</span> <span style="color: #A6E22E;">(</span>/ @accuracy @nb<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span> <span style="color: #66D9EF;">(</span>println <span style="color: #E6DB74;">"Average F1: "</span> <span style="color: #A6E22E;">(</span>/ @f1 @nb<span style="color: #A6E22E;">)</span><span style="color: #66D9EF;">)</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> Average precision: 0.3583671303540468 Average recall: 0.4984975547492504 Average accuracy: 0.295875000089407 Average F1: 0.4158918097615242 </pre> <p> This gives us a baseline <code>precision</code> score of <code>0.358</code>, <code>recall</code> score of <code>0.498</code>, <code>accuracy</code> score of <code>0.295</code> and <code>F1</code> score of <code>0.4158</code>. As we can notice if we compare with the random baseline with the full gold standard is that when we focus on the words which have multiple possible senses, the problem becomes much harder. </p> </div> </div> <div id="outline-container-org7160fd8" class="outline-4"> <br/> <h4 id="org7160fd8">Evaluation Using First Sense Context</h4> <div class="outline-text-4" id="text-org7160fd8"> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>svm-grid-search graph <span style="color: #AE81FF;">:grid-parameters</span> <span style="color: #66D9EF;">{</span><span style="color: #AE81FF;">:window-size</span> <span style="color: #A6E22E;">[</span>10 15 20<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:vector-size</span> <span style="color: #A6E22E;">[</span>64 128 256<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walk-length</span> <span style="color: #A6E22E;">[</span>5 10 15<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walks-per-vertex</span> <span style="color: #A6E22E;">[</span>32 64 128<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:angle</span> <span style="color: #A6E22E;">[</span>60 90 120<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:selection-metrics</span> <span style="color: #A6E22E;">[</span><span style="color: #AE81FF;">:precision</span> <span style="color: #AE81FF;">:accuracy</span> <span style="color: #AE81FF;">:recall</span> <span style="color: #AE81FF;">:f1</span><span style="color: #A6E22E;">]</span><span style="color: #66D9EF;">}</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> {:precision {:window-size 15, :vector-size 128, :walk-length 5, :walks-per-vertex 32, :angle 60, :score 0.4397163}, :accuracy {:window-size 15, :vector-size 256, :walk-length 5, :walks-per-vertex 32, :angle 60, :score 0.41875}, :recall {:window-size 10, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 1.0}, :f1 {:window-size 15, :vector-size 256, :walk-length 5, :walks-per-vertex 32, :angle 60, :score 0.58295965}} </pre> <p> For the same <code>first-sense-context</code> disambiguation algorithm, once the key hyperparameters of the pipeline are optimized, then we endup with a <code>F1</code> score of <code>0.5829</code> which is an increase of <code>40.19%</code> over the baseline. <code>Recall</code> increases by <code>100.64%</code>, <code>accuracy</code> increases by <code>41.55%</code> and <code>precision</code> increases by <code>22.72%</code>. </p> </div> </div> <div id="outline-container-org39b1a83" class="outline-4"> <br/> <h4 id="org39b1a83">Evaluation Without Target Word's Senses</h4> <div class="outline-text-4" id="text-org39b1a83"> <p> Now let's re-run the procedure when we don't focus on the target word that we want to disambiguate to create the context vector. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>svm-grid-search graph <span style="color: #AE81FF;">:grid-parameters</span> <span style="color: #66D9EF;">{</span><span style="color: #AE81FF;">:window-size</span> <span style="color: #A6E22E;">[</span>10 15 20<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:vector-size</span> <span style="color: #A6E22E;">[</span>64 128 256<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walk-length</span> <span style="color: #A6E22E;">[</span>5 10 15<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walks-per-vertex</span> <span style="color: #A6E22E;">[</span>32 64 128<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:angle</span> <span style="color: #A6E22E;">[</span>60 90 120<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:selection-metrics</span> <span style="color: #A6E22E;">[</span><span style="color: #AE81FF;">:precision</span> <span style="color: #AE81FF;">:accuracy</span> <span style="color: #AE81FF;">:recall</span> <span style="color: #AE81FF;">:f1</span><span style="color: #A6E22E;">]</span><span style="color: #66D9EF;">}</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> {:precision {:window-size 10, :vector-size 128, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 0.38931298}, :accuracy {:window-size 10, :vector-size 128, :walk-length 5, :walks-per-vertex 32, :angle 120, :score 0.38125}, :recall {:window-size 10, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 120, :score 1.0}, :f1 {:window-size 10, :vector-size 128, :walk-length 5, :walks-per-vertex 32, :angle 120, :score 0.55203617}} </pre> <p> In this case, the <code>F1</code> score increased by <code>32.76%</code> over the baseline. <code>Recall</code> increases by <code>238.07%</code>, <code>accuracy</code> increases by <code>28.87%</code> and <code>precision</code> increases by <code>8.65%</code>. Like we experienced previously, all of the results decreased compared to the version of the algorithm that included the sense of the target word in the calculation of the context vector. This option is not improving matters, and should be dropped from further consideration. </p> </div> </div> <div id="outline-container-org23ea010" class="outline-4"> <br/> <h4 id="org23ea010">Evaluation With Multiple Contexts</h4> <div class="outline-text-4" id="text-org23ea010"> <p> Finally let's evaluate the other modification we did that keeps the context vector that best matches one of the senses for the target word. </p> <div class="org-src-container"> <pre class="src src-clojure"><span style="color: #AE81FF;">(</span>svm-grid-search graph <span style="color: #AE81FF;">:grid-parameters</span> <span style="color: #66D9EF;">{</span><span style="color: #AE81FF;">:window-size</span> <span style="color: #A6E22E;">[</span>10 15 20<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:vector-size</span> <span style="color: #A6E22E;">[</span>64 128 256<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walk-length</span> <span style="color: #A6E22E;">[</span>5 10 15<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:walks-per-vertex</span> <span style="color: #A6E22E;">[</span>32 64 128<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:angle</span> <span style="color: #A6E22E;">[</span>60 90 120<span style="color: #A6E22E;">]</span> <span style="color: #AE81FF;">:selection-metrics</span> <span style="color: #A6E22E;">[</span><span style="color: #AE81FF;">:precision</span> <span style="color: #AE81FF;">:accuracy</span> <span style="color: #AE81FF;">:recall</span> <span style="color: #AE81FF;">:f1</span><span style="color: #A6E22E;">]</span><span style="color: #66D9EF;">}</span><span style="color: #AE81FF;">)</span> </pre> </div> <pre class="example"> {:precision {:window-size 15, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 60, :score 0.4566929}, :accuracy {:window-size 15, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 0.44375}, :recall {:window-size 15, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 0.9583333}, :f1 {:window-size 15, :vector-size 64, :walk-length 5, :walks-per-vertex 32, :angle 90, :score 0.6079295}} </pre> <p> In this case, the <code>F1</code> score increased by <code>46.2%</code> over the baseline. <code>Recall</code> increases by <code>223.97%</code>, <code>accuracy</code> increases by <code>50%</code>, and <code>precision</code> increases by <code>27.44%</code>. Except for the <code>recall</code>, all the other metrics improved compared to the <code>first-sense-context</code> method. Though we previously found that there were no apparent improvements using this method against using the <code>first-sense-context</code> algorithm, when we look at the words that have multiple senses only, we see that there is a clear improvement on all the metrics except for the <code>recall</code>, which is slightly lower. This suggests that in a situation where multiple words have multiple possible senses, then this method should outperform the <code>first-sense-context</code> one. </p> </div> </div> </div> </div> <div id="outline-container-org4a5d4ea" class="outline-2"> <br/> <h2 id="org4a5d4ea">Conclusion</h2> <div class="outline-text-2" id="text-org4a5d4ea"> <p> Disambiguating tagged concepts from a knowledge graph such as KBpedia is not an easy task. However, if a coherent and consistent knowledge graph such as KBpedia is used as the starting point, along with existing machine learning algorithms such as DeepWalk, it is possible to obtain good disambiguation performance in a short period of time without the need to create extensive training sets. </p> <p> With the tests we performed in this article, we gained insight into the kinds of things that may have much influence on the disambiguation process. We also learned how different strategies could be tested to get the best results for a specific disambiguation task. </p> <p> Results from these disambiguation processes can easily become the input into other tasks such as a document classification. [Disambiguated], tagged concepts should be a better basis for classifying the documents. If the concepts are not disambiguated, then much noise is added, leading to lower performance of the classification task. </p> <p> There are two main advantages of using such a disambiguation system. The first advantage is that we have no need to create any kind of training set, which would obviously be quite expensive to create considering that hundreds of contexts for each concept within the knowledge graph would need to be disambiguated manually. With a knowledge graph of the size of KBpedia, that would mean a training set of about 3.8 million examples. But since the training takes place on the structure of the knowledge graph that already exists, there is no need to create such a training set. Also, different parts of the knowledge graph could be used to create different kinds of knowledge structures ([i.e.], in this article we used the sub-class-of paths within the knowledge structure, but we could have used different aspects, different relationship paths, etc.). Alternate knowledge structures may result in different graph embeddings that may perform better in different situations. </p> <p> The second advantage is that since we only use simple linear algebra to disambiguate the concepts, performance is really fast. On average, we can disambiguate a single word within <code>0.65</code> milliseconds which means that we can disambiguate about 1,538 words per second with this method on a commodity desktop computer. </p> <p> Many more experiments could be performed to increase the performance of these methods, while retaining the ease of model creation and the disambiguation speed. New context algorithms could be tested, bigger sliding windows, etc. New graph embedding creation algorithms could be tested. New DeepWalk graph walk algorithms could be used such as breadth and depth first searches, etc. </p> </div> </div> <div id="footnotes"> <br/> <h2 class="footnotes">Footnotes</h2> <div id="text-footnotes"> <div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1">1</a></sup>Perozzi, B., Al-Rfou, R., & Skiena, S. (2014, August). Deepwalk: Online learning of social representations. In Proceedings of the 20th ACM SIGKDD international conference on Knowledge discovery and data mining (pp. 701-710). ACM. </div> </div> </div></div> <div class="col-md-2"> </div><!--/col-md-2--> </div> </div><!--/container--> <!--=== End Content Part ===--> <div class="footer-v1"> <div class="footer"> <div class="container"> <div class="row"> <!-- About --> <div class="col-md-3 md-margin-bottom-40"> <table> <tbody><tr> <td> <a href="/"> <img id="logo-footer" class="footer-logo" src="/imgs/logo-simple-purple.png" alt="" name="logo-footer"> </a> </td> </tr> <tr> <td> <center> <p> KBpedia </p> </center> </td> </tr> </tbody></table> <p style="font-size: 0.85em;"> KBpedia exploits large-scale knowledge bases and semantic technologies for machine learning, data interoperability and mapping, and fact extraction and tagging. </p> </div><!--/col-md-3--> <!-- End About --> <!-- Latest --> <div class="col-md-3 md-margin-bottom-40"> <div class="posts"> <div class="headline"> <h2> Latest News </h2> </div> <ul class="list-unstyled latest-list"> <!-- HTML generated from an RSS Feed by rss2html.php, http://www.FeedForAll.com/ a NotePage, Inc. product (http://www.notepage.com/) --> <li> <a href="https://kbpedia.org/resources/news/kbpedia-adds-ecommerce/">KBpedia Adds Major eCommerce Capabilities</a> <small>06/15/2020</small> </li> <!-- HTML generated from an RSS Feed by rss2html.php, http://www.FeedForAll.com/ a NotePage, Inc. product (http://www.notepage.com/) --> <li> <a href="http://kbpedia.org/resources/news/kbpedia-continues-quality-improvements/">KBpedia Continues Quality Improvements</a> <small>12/04/2019</small> </li> <!-- HTML generated from an RSS Feed by rss2html.php, http://www.FeedForAll.com/ a NotePage, Inc. product (http://www.notepage.com/) --> <li> <a href="http://kbpedia.org/resources/news/wikidata-coverage-nearly-complete/">Wikidata Coverage Nearly Complete (98%)</a> <small>04/08/2019</small> </li> </ul> </div> </div><!--/col-md-3--><!-- End Latest --><!-- Link List --> <div class="col-md-3 md-margin-bottom-40"> <div class="headline"> <h2> Other Resources </h2> </div> <ul class="list-unstyled link-list"> <li> <a href="/resources/about/">About</a> </li> <li> <a href="/resources/faq/">FAQ</a> </li> <li> <a href="/resources/news/">News</a> </li> <li> <a href="/use-cases/">Use Cases</a> </li> <li> <a href="/resources/documentation/">Documentation</a> </li> <li> <a href="/resources/privacy/">Privacy</a> </li> <li> <a href="/resources/terms-of-use/">Terms of Use</a> </li> </ul> </div><!--/col-md-3--> <!-- End Link List --><!-- Address --> <div class="col-md-3 map-img md-margin-bottom-40"> <div class="headline"> <h2> Contact Us </h2> </div> <address class="md-margin-bottom-40"> c/o <a href="mailto:info@mkbergman.com?subject=KBpedia%20Inquiry">Michael K. Bergman</a> <br> 380 Knowling Drive <br> Coralville, IA 52241 <br> U.S.A. <br> Voice: +1 319 621 5225 </address> </div><!--/col-md-3--> <!-- End Address --> </div> </div> </div><!--/footer--> <div class="copyright"> <div class="container"> <div class="row"> <div class="col-md-7"> <p class="copyright" style="font-size: 10px;"> 2016-2022 漏 <a href="http://kbpedia.org" style="font-size: 10px;">Michael K. Bergman.</a> All Rights Reserved. </p> </div> <!-- Social Links --> <div class="col-md-5"> <ul class="footer-socials list-inline"> <li> <a href="/resources/feeds/news.xml" class="tooltips" data-toggle="tooltip" data-placement="top" title="" data-original-title="RSS feed"> <i class="fa fa-rss-square"></i> </a> <br></li> <li> <a href="http://github.com/Cognonto" class="tooltips" data-toggle="tooltip" data-placement="top" title="" data-original-title="Github"> <i class="fa fa-github"></i> </a> <br></li> <li> <a href="http://twitter.com/cognonto" class="tooltips" data-toggle="tooltip" data-placement="top" title="" data-original-title="Twitter"> <i class="fa fa-twitter"></i> </a> <br></li> </ul> </div> <!-- End Social Links --> </div> </div> </div><!--/copyright--> </div><!--=== End Footer Version 1 ===--> <!--/wrapper--> <!-- JS Global Compulsory --> <script type="text/javascript" src="/assets/plugins/jquery/jquery.min.js"></script> <script type="text/javascript" src="/assets/plugins/jquery/jquery-migrate.min.js"></script> <script type="text/javascript" src="/assets/plugins/bootstrap/js/bootstrap.min.js"></script> <!-- JS Implementing Plugins --> <script type="text/javascript" src="/assets/plugins/back-to-top.js"></script> <!-- JS Customization --> <script type="text/javascript" src="/assets/js/custom.js"></script> <!-- JS Page Level --> <script type="text/javascript" src="/assets/js/app.js"></script> <!-- JS Implementing Plugins --> <script type="text/javascript" src="/assets/plugins/smoothScroll.js"></script> <script type="text/javascript" src="/assets/plugins/owl-carousel/owl-carousel/owl.carousel.js"></script> <script type="text/javascript" src="/assets/plugins/layer-slider/layerslider/js/greensock.js"></script> <script type="text/javascript" src="/assets/plugins/layer-slider/layerslider/js/layerslider.transitions.js"></script> <script type="text/javascript" src="/assets/plugins/layer-slider/layerslider/js/layerslider.kreaturamedia.jquery.js"></script> <!-- JS Customization --> <script type="text/javascript" src="/assets/js/custom.js"></script> <!-- JS Page Level --> <script type="text/javascript" src="/assets/js/plugins/layer-slider.js"></script> <script type="text/javascript" src="/assets/js/plugins/style-switcher.js"></script> <script type="text/javascript" src="/assets/js/plugins/owl-carousel.js"></script> <script type="text/javascript" src="/assets/js/plugins/owl-recent-works.js"></script> <script type="text/javascript"> jQuery(document).ready(function() { App.init(); LayerSlider.initLayerSlider(); StyleSwitcher.initStyleSwitcher(); OwlCarousel.initOwlCarousel(); OwlRecentWorks.initOwlRecentWorksV2(); }); </script> <!--[if lt IE 9]> <script src="assets/plugins/respond.js"></script> <script src="assets/plugins/html5shiv.js"></script> <script src="assets/plugins/placeholder-IE-fixes.js"></script> <![endif]--> <!--[if lt IE 9]> <script src="/assets/plugins/respond.js"></script> <script src="/assets/plugins/html5shiv.js"></script> <script src="/assets/js/plugins/placeholder-IE-fixes.js"></script> <![endif]--> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-84405507-1', 'auto'); ga('send', 'pageview'); </script>