<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Suryank Tiwari on Medium]]></title>
        <description><![CDATA[Stories by Suryank Tiwari on Medium]]></description>
        <link>https://medium.com/@suryanktiwari?source=rss-f5220f19938------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xRvAsPfxcCA_zZgxbH-d5Q.jpeg</url>
            <title>Stories by Suryank Tiwari on Medium</title>
            <link>https://medium.com/@suryanktiwari?source=rss-f5220f19938------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 09:49:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suryanktiwari/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Analysis of the Indian Railway Network]]></title>
            <link>https://medium.com/codex/analysis-of-the-indian-railway-network-4fe0c501779e?source=rss-f5220f19938------2</link>
            <guid isPermaLink="false">https://medium.com/p/4fe0c501779e</guid>
            <category><![CDATA[network]]></category>
            <category><![CDATA[railways]]></category>
            <category><![CDATA[network-science]]></category>
            <category><![CDATA[trains]]></category>
            <category><![CDATA[analysis]]></category>
            <dc:creator><![CDATA[Suryank Tiwari]]></dc:creator>
            <pubDate>Sun, 20 Feb 2022 19:29:06 GMT</pubDate>
            <atom:updated>2022-09-08T01:50:08.358Z</atom:updated>
            <content:encoded><![CDATA[<h4>A network science approach</h4><h4>Deconstructing the third largest railway network of the world</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FfA1BetPpjhyrwb7" /><figcaption>Photo by <a href="https://unsplash.com/@5tep5?utm_source=medium&amp;utm_medium=referral">Alexander Popov</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The onset of trains and railroad networks has proven to be indispensable to mankind. With their readily accessible nature, the world has become a smaller place whether you’re on your way to a cross-country skiing trip, transferring goods and commodities to far-off moors, or steaming through platform 9¾ to learn how to fend off dementors.</p><p>Almost each country has their own railway network and dissecting these real world networks help us with scaling, congestion management and overall socio-economic growth.</p><p>This article briefly glides through the makings and markings of a Network Science project and highlights the results and inferences obtained.</p><h3>Objectives</h3><ul><li>To extract properties and inferences out of the Indian Railway Network or IRN.</li><li>To be able to query the network and draw out subgraphs and connections between regions.</li><li>Compute various centrality measures, visualizations and network parameters.</li></ul><h3>Dataset</h3><p>The data used throughout our analyses comes from the <strong>Indian Railways Timetable for trains available as on 01.11.2017</strong> published by the Indian Government.</p><p>The dataset consists of the following columns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CFzN6zYLFdQWqR_5oNXiAQ.png" /><figcaption>Columns of <strong>Indian Railways Timetable </strong>dataset.<strong> </strong>(Image by author)</figcaption></figure><p>Each row in the dataset tells us about a train with a specific name and number, traveling to a specific destination from a specified source spanning a deterministic distance within said timings.</p><p>For a train traveling from source A to final destination Z, there will be rows for each station the train will stop at in the journey, separated by an incrementing sequence number. Look at this snapshot from the dataset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4mPZ2Gtx-3z_LjqQ0hyWQg.png" /><figcaption>Snapshot from the dataset — <strong>Indian Railways Timetable for trains available as on 01.11.2017</strong> published by the Indian Government.</figcaption></figure><p>In this above snapshot, we have a story about a train called <em>‘SWV-MAO-VLNK’</em> which will travel from <em>SAWANTWADI R </em><strong><em>(SWV)</em></strong> at 10:25:00 to<em> MADGAON JN</em>. <strong>(MAO)</strong>, arriving at 12:10:00 traveling a distance of 78 kilometers. In its journey, the train will stop at <em>Thivim</em> <strong>(THVM)</strong>, and <em>Karmali</em> <strong>(KRMI)</strong>.</p><p>We know this is the path train took since it is kindly highlighted to us by an incrementing sequence number denoted by SEQ. The SEQ resets to 1 when we start looking at a different train. Through incrementing SEQs, the values pertaining to the Source Station and the Destination Station stay constant.</p><p>This way we have the data of <strong>7580</strong> unique trains and <strong>8147</strong> different railway stations.</p><p>If you want to have a go at the dataset yourself, here is the <a href="https://data.gov.in/catalog/indian-railways-train-time-table-0?filters%5Bfield_catalog_reference%5D=332021&amp;format=json&amp;offset=0&amp;limit=6&amp;sort%5Bcreated%5D=desc">source</a>.</p><h3>Network formation</h3><p>Since we have cleared the first hurdle of securing our data and understanding what it represents, the next obvious step is to form a network, admittedly without which analyzing it becomes infinitely more complex.</p><p>We’ll need to define a set of heuristics which drive our network forming process. Following are the rules we defined for construction.</p><ul><li>Each railway station is a unique node.</li><li>An edge exists between two nodes if there exists a train that connects them both. Hence each station in a route must be connected to each other via a direct edge, forming a complete graph.</li><li>Edge Weights can be considered either on the basis of distance and on the number of trains between two nodes. We’ll try out both.</li></ul><p>With our ground rules in place, we can remove null values from the dataset, and create directed weighted graphs based on train count between two stations.</p><p>We’re making directed graphs as we can simply convert to undirected ones whenever desired by a simple call to <em>graph</em><strong><em>.</em></strong><em>to_undirected(), </em>thanks to networkx.</p><p>Here are some basic details about our formed network:</p><pre>Number of nodes: 8147<br>Number of edges: 902602<br>Average in degree: 110.7895<br>Average out degree: 110.7895</pre><p>As we can see above, IRN has total <strong>8147 railway stations</strong> . With our heuristic stated above, we have a total of 902602 edges with average in and out degree as 110.7895.</p><p>Since, this graph is a directed depiction of the IRN. Converting it to an undirected graph and fetching the same properties</p><pre>Number of nodes: 8147<br>Number of edges: 500412<br>Average degree: 122.8457</pre><p>We notice that the number of nodes is the same while the number of edges decrease, which is to be expected. The average degree has also increased from 110.7895 to 122.8457, again to be expected.</p><p>But reading about these values isn’t very alluring. Let’s try to bring some visuals into play. We should plot the entire graph and see how the network really looks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*HoJoGMS7obPBEKiJSMALng.png" /><figcaption>Spring Layout for the Indian Railway Network. (Image by author)</figcaption></figure><p>Fair to say that this image over here isn’t doing any justice to our railway network. The number of nodes and edges is far too large to be plotted in a meaningful way.</p><p>We need to take out a subgraph out of this network and we’ll have a better time with plotting. You can find the code used this segment <a href="https://github.com/suryanktiwari/Indian-Railway-Network-Analysis/blob/main/Data%20Preprocessing%2C%20Querying%20and%20Visualization.ipynb">here</a>.</p><h3>Computing regions between two nodes — Subgraph Querying and Visualization System</h3><p>In this section we visualize a set of nodes (stations) specified, their neighbors and the shortest path between them.</p><p>We plan to query a set of nodes from the original railway network that we have created. For this set of nodes, we will highlight the shortest route from one every station to another. And for prettifying the graph, we will also plot out the direct neighbors of the nodes we have queried.</p><p>To find the code for this section, you can check out <a href="https://github.com/suryanktiwari/Indian-Railway-Network-Analysis/blob/main/Data%20Preprocessing%2C%20Querying%20and%20Visualization.ipynb">this notebook</a>.<br><em>(Note: Edge weight code hasn’t been updated in the linked version for distance based clubbing)</em></p><p><strong>The following is the subgraph querying and visualization demo for the station codes: DAA and SWV with train count as edge weights</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*nGLqa-QPNHCoc7KkXKl9Kg.png" /><figcaption>Subgraph highlight shortest route between stations DAA and SWV (Image by author)</figcaption></figure><p>Here, the shortest route from DAA to SWV is highlighted in red. The original queried nodes are printed out in bold letters. There’s no labelling the neighboring stations since that would just lead to clutter.</p><p>We can see there are other paths leading from DAA to SWV as well, but the highlighted path is at the mercy of networkx library’s shortest_path function, which is doing a fine job at highlighting the shortest path.</p><p>Keep in mind since that we are using number of trains connecting a station as our edge weights this graph doesn’t necessarily gives the shortest physical distance between two railway stations, so we didn’t use the edge weight feature here. We just took the route with the minimum number of stations in between.</p><p>We get a totally different graph (duh) if we use the distance between the stations as the edge weight. Let’s query it out for the same stations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*GmfkXdOIyGU9K7onRJWi1w.png" /><figcaption>Subgraph highlight shortest route between stations DAA and SWV, edge weight by distance (Image by author)</figcaption></figure><p>So we have some concept of distance between the neighbors now, and turns out the route we had originally wasn’t the shortest one since we have to go through a different station ST now.</p><p>Let’s see some more demonstrations for demonstration’s sake.</p><p>The earlier stations we saw were smaller stations. Let’s query the subgraph for New Delhi (NDLS), Dehradun (DDN) and Bangalore (BNCE). Initially we look at the train count edge weighted graph.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*iKqqjXMoWLHxKfQX0o7AXg.png" /><figcaption>Subgraph highlight shortest route between stations NDLS, DDN and BNCE (Image by author)</figcaption></figure><p>We see the neighboring stations clubbed out in layers as the number of trains they can be connected by would a discrete number. Hence the stations connected by 1 train only sort together, neighbors connected to our queried stations connected by 2 different trains sort together and so on. It certainly makes for a visually pleasing pattern.</p><p>Here’s another:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*1HOBcMFSIOhPxwwU0ig9IQ.png" /><figcaption>Subgraph highlight shortest route between stations DAA, UHP and CUR (Image by author)</figcaption></figure><p>This is fun but we can’t drive real inferences from these subgraphs. Let’s move ahead to find some useful information.</p><h3>Network Analysis and Inferences</h3><p>In this section we’ll go through the basic definitions of a few types of centralities and what they mean. We’ll also list the top stations with highest values in those specific fields.</p><h4>Degree Centrality</h4><p>Definition: Degree centrality computes an importance score based on the number of nodes by which each node is connected.</p><p><strong><em>Inference: Degree Centrality tells us what are the most popular, or well-connected nodes in the network.</em></strong></p><p>These nodes have the most one-hop connections to other nodes or Stations in our case. These nodes can connect to the wider network in less number of hops. In our network, the most connected stations, holding the most information are the following:</p><pre>        STATION NAME		 DEGREE CENTRALITY</pre><pre>	 HOWRAH JN. 		 0.30211146575006137<br>	 VIJAYWADA JN 		 0.2704394794991407<br>	 KANPUR CENTR 		 0.263810459121041<br>	 VARANASI JN. 		 0.25767247728946724<br>	 GHAZIABAD JN 		 0.25202553400441935<br>	 KALYAN JN 		 0.24797446599558065<br>	 ITARSI 		 0.24441443653326786<br>	 LUCKNOW JN. 		 0.243677878713479<br>	 AHMEDABAD 		 0.23852197397495703<br>	 MATHURA JN. 		 0.2363123005155905</pre><h4>Betweenness Centrality</h4><p>Definition: Betweenness centrality is a measure of the number of times a station node lies on the shortest path between other stations.</p><p><strong><em>Inference: This measure shows which stations act as a “bridge” connecting other stations together. The stations having a high betweenness centrality will lie on shortest paths between two stations. These stations have a high influence in the network.</em></strong></p><p>The stations which act as a bridge or have high influence on the flow of the network are the following:</p><pre><br> 	STATION NAME		 BETWEENNESS CENTRALITY</pre><pre>	 HOWRAH JN. 		 0.03406351792928565<br>	 SEALDAH 		 0.021795409939440936<br>	 KANPUR CENTR 		 0.020930718809083173<br>	 VIJAYWADA JN 		 0.015177400281457002<br>	 AHMEDABAD 		 0.014301664879802252<br>	 YESVANTPUR J 		 0.014256569821548529<br>	 VADODARA JN. 		 0.012967968029171826<br>	 VARANASI JN. 		 0.012623744668081235<br>	 KOLKATA 		 0.011898350401287686<br>	 PILIBHIT JN. 		 0.01184539309233258</pre><h4><strong>Closeness Centrality</strong></h4><p>Definition: Closeness centrality assigns score to each station node based on their ‘closeness’ to all other stations in the railway network.</p><p><strong><em>Inference: Closeness Centrality is helpful in finding the stations which are placed to influence the entire railway network most quickly. This measure can help identify good “broadcasters” in a network.</em></strong></p><p>The stations with the highest closeness centrality in order are the following:</p><pre>       STATION NAME		 BETWEENNESS CENTRALITY</pre><pre>	 HOWRAH JN. 		 0.5124082033874587<br>	 AHMEDABAD 		 0.5107660799308908<br>	 VADODARA JN. 		 0.5063522924820026<br>	 KANPUR CENTR 		 0.5062879529276847<br>	 VARANASI JN. 		 0.5052287059583946<br>	 NEW DELHI 		 0.5051966767517281<br>	 KALYAN JN 		 0.5036640360941574<br>	 MUGHAL SARAI 		 0.5035049206471066<br>	 VIJAYWADA JN 		 0.5021406666088064<br>	 ITARSI 		 0.4998101090743702</pre><h4><strong>Eigenvector Centrality</strong></h4><p>Definition: This measure works like degree centrality, by measuring a station’s influence over the others based on its degree but it also takes into account things like well-connectedness of a station and how many nodes are the connections connected to, and so on, throughout the network.</p><p><strong><em>Inferences: Eigen Vector Centrality is a good all rounder measure. It shows which stations have influence over the entire network, not just the stations it is connected to.</em></strong></p><pre>        STATION NAME		 BETWEENNESS CENTRALITY</pre><pre>	 VARANASI JN. 		 0.07072646825994845<br>	 HOWRAH JN. 		 0.0696564673531263<br>	 KANPUR CENTR 		 0.06676069453032213<br>	 ITARSI 		 0.06674728443587287<br>	 NEW DELHI 		 0.06624320025503437<br>	 LUDHIANA JN. 		 0.06593208791866616<br>	 MATHURA JN. 		 0.06569071466478024<br>	 KALYAN JN 		 0.06527068019550916<br>	 PATNA JN. 		 0.06473312502358505<br>	 MUGHAL SARAI 		 0.06457963445769375</pre><h3>Inferences based on Geographic Location</h3><p>The main stations in the Indian Railway Network based on the station(node)-degree relationship can be inferred from the degree centrality. These stations are further classified into two groups, based on their geographic locations:</p><ol><li>Stations that are near metropolitan cities in India. For example: Howrah which is near Calcutta, Ghaziabad near New Delhi and Kalyan station near Mumbai</li><li>Stations that are located in the central parts of the country, or at the meetin points of railway lines connecting different zones of the country. For example: Vadodara Junction, Vijaywada Junction. <strong>These stations are potential points of congestion in the network as they do not have much resources as compared to the stations near metropolitan states.</strong></li></ol><p>All these nodes handle a high amount of traffic.</p><p>For example:</p><ol><li>Howrah- Near Calcutta which is a metropolitan city. (Type 1) Likely to have more resources.</li><li>Itarsi- Falls in the central region of the country. (Type 2) Likely to have less resources and cause congestion.</li></ol><h3>General Inferences</h3><pre>Number of trains: 7580</pre><pre>Number of stations: 8147</pre><pre>Longest train route: 4260 km. Train =  15905 CAPE - DBRG . Starting station:  KANNIYAKUMARI . Ending Station:  DIBRUGARH</pre><pre>Shortest train route: 1 km. Train =  3308 PLJE-SZE MEM . Starting station:  PHULWARTANR . Ending Station:  SONARDIH</pre><pre>Maximum distance between any two consecutive stations: 1301  km with train RSD-PJP BSF</pre><pre>Minimum distance between any two consecutive stations: 1  km with train  LGL KNJ EMU</pre><pre>Average total train route distance: 439.7 km</pre><pre>Average distance between consecutive stops 17.91 km</pre><p>With these sections, some inferences have been made out about the network which can help us identify possible points of congestion and other general details. We are going to dive deeper into network based properties in the next section.</p><p>The code for this section can be found in <a href="https://github.com/suryanktiwari/Indian-Railway-Network-Analysis/blob/main/Centrality%20Measures%20and%20Inferences.ipynb">this notebook.</a></p><h3>Further Network Analysis</h3><p>In this final section, we look at some common network properties for IRN. The topics will be loosely coupled amongst themselves but will highlight crucial information about the network.</p><h4>Plotting the correlation between in-degree kᵢₙ and out-degree kₒᵤₜ of the IRN.</h4><p>In-degree is defined as the number of paths that are coming to a node, whereas out-degree is defined as the number of paths going out of a node in a network. The plot of correlation of in-degree vs out-degree for IRN is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*7zOoc3kOH2iM-YVgM5-Clw.png" /><figcaption>Correlation between in-degree and out-degree of the IRN. (Image by author)</figcaption></figure><ul><li>The strong in-degree and out-degree correlation and the high <strong>reciprocity parameter R of 0.8912</strong> both indicate that <strong>IRN is a symmetrical network</strong>.</li><li>The reciprocity calculated for the directed graph is defined as the ratio of the number of edges pointing in both directions to the total number of edges in the graph.</li><li><strong>The above inference shows that if there is a train from station i to station j , there is also a great possibility that there is a train from station j to station i .</strong></li></ul><h4>Reflecting on the Connectivity of Nodes in the Railway Network</h4><p>Following are the stations with maximum degree in our network:</p><pre>The 4 stations having the highest degree (that are directly accessible the most) by different stations in the Indian Railway Network are:<br>HOWRAH JN. 1284<br>VIJAYWADA JN 1171<br>VARANASI JN. 1124<br>KANPUR CENTR 1119</pre><h4>Path Length Distribution of the Railway Network</h4><p>The path length is defined as the shortest distance between two nodes. The average path length is the average of the path length averaged over all pairs of nodes.</p><p>By counting how many node pairs (source station and destination station) have a particular path length we find the path length distribution of the network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/1*Q2nhsM9Tnt_-GQdcXBoy9w.png" /><figcaption>Path length distribution of IRN (Image by author)</figcaption></figure><p>The above graph infers that most of the node pairs are connected having the shortest path length as 3.</p><p>The stations are connected with each other maximally at a distance of 10 and number of node pairs doing so are 18. Imagine switching 10 trains to reach your station.</p><p>The code for this final section can be found at <a href="https://github.com/suryanktiwari/Indian-Railway-Network-Analysis/blob/main/Network%20Analysis.ipynb">this notebook.</a></p><p>There are some more experiments we performed that can be found in the <a href="https://github.com/suryanktiwari/Indian-Railway-Network-Analysis">Github repository</a> for this project.</p><p>Feel free to drop any comments or any ideas that can be further performed on this topic.</p><p>Thanks for reading.</p><h3>Contributions:</h3><p><a href="https://www.iiitd.ac.in/bagler"><strong>Dr. Ganesh Bagler</strong></a><strong> </strong>— Mentor and guide throughout the project.</p><p><strong>Suryank Tiwari</strong> (<a href="https://medium.com/u/f5220f19938?source=post_page-----5710ee869505-----------------------------------">Suryank Tiwari</a>)</p><p><strong>Rose Verma (</strong><a href="https://medium.com/u/4f0aadbc3369?source=post_page-----5710ee869505-----------------------------------">Rose Verma</a><strong>)</strong></p><p><strong>Harshita Pandey</strong></p><p>This project has been done for the course Network Science 2021 at <a href="https://www.iiitd.ac.in/">IIITD</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4fe0c501779e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/analysis-of-the-indian-railway-network-4fe0c501779e">Analysis of the Indian Railway Network</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Structured recipe from an unstructured corpus]]></title>
            <link>https://medium.com/data-science/structured-recipe-from-an-unstructured-corpus-5710ee869505?source=rss-f5220f19938------2</link>
            <guid isPermaLink="false">https://medium.com/p/5710ee869505</guid>
            <category><![CDATA[knowledge-graph]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[food]]></category>
            <category><![CDATA[information-retrieval]]></category>
            <dc:creator><![CDATA[Suryank Tiwari]]></dc:creator>
            <pubDate>Fri, 22 May 2020 13:12:33 GMT</pubDate>
            <atom:updated>2020-06-25T13:52:53.085Z</atom:updated>
            <content:encoded><![CDATA[<h4>Information Retrieval</h4><h4>Outline of a food-based knowledge graph driven information retrieval and natural language processing project</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dgk2ek02nmFMIPGl" /><figcaption>Photo by <a href="https://unsplash.com/@socialcut?utm_source=medium&amp;utm_medium=referral">S O C I A L. C U T</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This article briefly glides through the makings and markings of an Information Retrieval (and Natural Language Processing Hybrid) Project and highlights the results and inferences obtained.</p><p>The objectives of this project are to:</p><ul><li>Derive a structure out of an unstructured corpus of recipe data.</li><li>Making use of this structure to arrive at a Knowledge Graph.</li><li>Extracting and reviewing inferences from the Knowledge Graph obtained.</li><li>Building an information retrieval and visualization system.</li></ul><p>The work that follows can serve as a basis for cuisine classification, recommendation systems, knowledge graph-based semantic derivation, and so on.</p><h3>What is Unstructured Data?</h3><ul><li>The absence of a well-defined structure in data collection makes it <strong>Unstructured</strong>.</li><li>Images, text, songs, videos are some examples of such data.</li><li>Most of all data generated are unstructured.</li></ul><blockquote>While there is truth to the fact that no data is purely unstructured, our machines find this form of data uneventful; unless we can help it.</blockquote><p>Information extraction and analysis cannot be performed on unstructured data. Since most of all data is unstructured, this is a widely faced problem and hence our problem definition.</p><h3>Problem Definition</h3><p>Numerous cooking websites present food recipes with varied cuisines and methodologies. Even though a plethora of recipe data exists, there is little to no structure to it. Any structures if present, are not competent for deriving useful patterns from them.</p><p>Therefore, our task is to derive some structure and meaningful relationships from <strong>unstructured recipe based data</strong> and to be able to run an information retrieval system on it.</p><h3>Data set</h3><p>The data set we used has the following structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*TyAl-weWoIKimKuEmQqQSA.png" /><figcaption>Single Recipe JSON Breakdown. (<em>Image by author</em>)</figcaption></figure><p>This is the JSON breakdown for one recipe. The <em>title</em> of the recipe, the <em>ingredients</em> list, and the <em>directions</em> list are the only entries we are concerned about within this project. Depiction of the ingredients list is as follows. i.e. our unstructured data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*31S1OqiIc4gvsV_GaRgCDA.png" /><figcaption>Ingredients List JSON breakdown for one recipe. (<em>Image by author</em>)</figcaption></figure><p>The adjoining image shows how recipe ingredient data is stored in the ingredients list. As can be seen, this data is unstructured.</p><p>This list is our input to be structured.</p><h3>Structuring the data</h3><p>The unstructured ingredients data can be structured using the <a href="https://github.com/nytimes/ingredient-phrase-tagger">CRF Ingredient Phrase Tagger by NYTimes</a>.</p><p>The problem is a structured prediction problem in which we need to identify a sequence of labels rather than identifying a single label. For this purpose, CRF Ingredient Phrase Tagger makes use of Linear Chain Conditional Random Fields which has often been successful in tasks such as POS Tagging and Named Entity Recognition(NER).</p><p>Various labels identified in the case of ingredients include Quantity, Unit, Name, and Comments. The model finds the best possible sequence of labels by finding the highest probability among all the label sequences possible over an ingredient. This is depicted as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*zztemO_6I7lSWOK9_czJww.png" /><figcaption>If our ingredient phrase is “2 teaspoons salt.” Then we need to score all the possible sequences of tags. (<em>Image by author</em>)</figcaption></figure><p>The structure that is generated consists of labels attached to the ingredients.</p><p>The model is trained on 20000 ingredients and an intermediate model file is generated which is further used for testing purposes.</p><p>The evaluation criteria are the accuracy of the model to predict at:</p><ul><li>Sentence Level: The model predicts the actual value of every word in the ingredient sentence. If a word in the sentence is incorrectly identified, then the sentence is added to incorrectly specified sentences.</li><li>Word Level: In the case of word-level, every word is checked irrespective of the sentence.</li></ul><p>Testing on 2000 ingredients generates accuracy of approximately 74% at sentence level whereas 90% at the word level. The lower accuracy at sentence level can be attributed to the presence of multiple words in a sentence and not all words are correctly identified, hence dropping the overall accuracy at the sentence level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6ArfAkCJ8mo3xmKQePcuKg.png" /><figcaption>Word Level Plot and Sentence level Plot of accuracies achieved in the structuring stage. (<em>Image by author</em>)</figcaption></figure><p>Application of model on ingredients corresponding to the dataset of recipes, we can structure its data, thereby implying the robustness and accuracy of the model.</p><p>Now that we have a structure ready from our corpus, we can move to the construction of Knowledge Graphs from here.</p><h3>Knowledge Graph Construction</h3><p>The next step is the construction of triples for making a Knowledge Graph.</p><p>If you are new to knowledge graphs, check out this quick read by Egor Dezhic:</p><p><a href="https://medium.com/@Dezhic/understanding-knowledge-graphs-5cb05593eb84">Understanding Knowledge Graphs</a></p><p>Before making triples, we need to do some quick preprocessing steps:</p><ol><li>Lemmatization</li><li>Punctuation Removal</li><li>Stopword Removal</li><li>Leading and trailing spaces removal</li><li>Lowercase conversion</li></ol><p>This cleans our data and provides uniformity for later use in the querying section of the article.</p><p>The triples to be constructed are of the form: (<strong>source</strong>) — <strong>edge</strong> — (<strong>target</strong>). Each source and target value becomes a <strong>node</strong> and the third triple connects them as an <strong>edge</strong>.</p><p>A collection of triples is created for each (recipe, structured cooking step) pair. For each pair, the ‘<strong>source</strong>’ node is the ‘<strong>title</strong>’ column of the structured JSON, the ‘<strong>target</strong>’ node is the ingredient ‘<strong>name</strong>’ and the ‘<strong>edge</strong>’ in the field ‘<strong>comment</strong>’ from the structured data obtained.</p><p>In short, every recipe title is a source node that is connected to its ingredients via the processing to be applied to them in the recipe.</p><p>Our collection results in a knowledge graph like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JdSyttwVR9gl-H0h5lgncQ.png" /><figcaption>A knowledge graph for 10 recipes. (<em>Image by author</em>)</figcaption></figure><p>If you think this Knowledge graph for 10 recipes is dense then look at the following graph for 100 recipes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iGdV93tULu_KvLtLIwZp2w.png" /><figcaption>A Knowledge Graph for 100 recipes. (<em>Image by author</em>)</figcaption></figure><p>For clarity, let’s have a look at a Knowledge Graph from a single recipe:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*uCXIBEvqP2_ikqRA4T967g.jpeg" /><figcaption>A Knowledge graph for a single recipe. (<em>Image by author</em>)</figcaption></figure><p>So we have a graph. But where to go from here?</p><h3>Querying the graph</h3><p>Let’s query the graph so we can attain any desired information from it.</p><p>An <strong>inverted index </strong>is built for each keyword that is found in the triple word base. Each keyword points to a node or edge that the keyword is found in. The inverted index looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zOMYddqukxxtijEHYUn4VQ.png" /><figcaption>An example of the inverted index. Here the elements inside the curly braces are nodes or edges in the Knowledge Graph. (<em>Image by author</em>)</figcaption></figure><p>For each query term, the candidate set contains the set of nodes or edges that the term points to in the inverted index. This candidate set is used to draw the output subgraph from our original Knowledge Graph.</p><p>So, any node containing the query term and its immediate neighbors will be shown in the graph. Any edge containing the term carrot will come up with the two nodes it is connecting as well. Interconnections are taken care of.</p><p>Consider the query — <strong>carrot</strong>. The output for this query is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OaWxIvuBcLc0eBERDqLdKQ.png" /><figcaption>Subgraph output for the query: carrot. (<em>Image by author</em>)</figcaption></figure><p>This graph is much sparser and the nodes and edges can be observed clearly.</p><p>Let’s consider another query — <strong>diced spinach</strong>. For multiple query terms, the output Knowledge Graph may be connected or disconnected depending on the revelations the query terms bring out from the graph. The result of this query is this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-YwV87MdgWKeLPXFnt3gcw.png" /><figcaption>Subgraph output for the query: diced spinach. (<em>Image by author</em>)</figcaption></figure><p>We get a slightly more connected graph. Even some recipe nodes which are connected to all their respective ingredients are present in the graph.</p><p>A few more queries:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oYIkzqxVPGGM--6_RYHuEA.png" /><figcaption>‘<strong>canned</strong>’ and ‘<strong>cardamom</strong>’ as queries. (<em>Image by author</em>)</figcaption></figure><p>This concludes the querying section. Let’s talk about the inferences we can make from our full Knowledge Graph.</p><h3>Inferences</h3><p>The constructed Knowledge Graph is used to draw inferences to find out important information.</p><p>Measures evaluated:</p><p><strong>Degree Centrality:</strong></p><p>Degree Centrality is a measure of popularity. It determines the nodes that are connected to the most number of nodes. This measure determines the nodes in the graph that can quickly spread information in a localized region.</p><p>For the context of recipe data, an ingredient node with high degree centrality is an ingredient that is popular and is used in a lot of recipes or with a lot of other ingredients.</p><p><strong>Betweenness Centrality:</strong></p><p>Betweenness Centrality is a measure that provides a sense about which nodes are important not because they have a large number of connections but because they provide connectivity and cohesion to the network.</p><p>These nodes are the ones that are important ingredients of a recipe and they have high betweenness centrality but low degree centrality.</p><p><strong>Eigen Vector Centrality:</strong></p><p>Eigen Vector Centrality is useful for understanding which nodes can get information to a large number of nodes quickly.</p><p>It can also be seen as a related influence or the nodes that play the role of power behind the scenes. Essentially, the nodes that are neighbors of the important nodes in the knowledge graph.</p><p>These ingredients can be seen as the most essential ingredients.</p><p><strong>Connected Components:</strong></p><p>It is seen that a majority of recipes form the largest connected component of the graph and the rest contain one or two recipes. This hints that some recipes are very unique and different due to their ingredients or methods of preparation.</p><p>These recipes are like the outliers in the sample space as their method of preparation and/or the ingredient set is very different than the most recipes.</p><p><strong>Community Detection:</strong></p><p>Community detection is an approach by which the knowledge graph is partitioned in the best possible way. It is seen that the recipes form groupings together. These groupings are called <strong>communities</strong>.</p><p>Communities hint at the segregation of recipes based on the ingredients or the method of preparation.</p><p><strong>It was seen that in a knowledge graph constructed from a corpus of 100 recipes with 2 connected components had 9 communities within.</strong></p><p><strong>The communities hint at a possible classification of the recipes that can be performed. These clusters have recipes of the same kind.</strong></p><p>Pooled Inferences:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IHW1LDTxKnSFMCep-0F8_Q.png" /><figcaption>Inferences obtained from the measures described. (<em>Image by author</em>)</figcaption></figure><p>If you want to read more about the measures used for inferences, here are some useful links:</p><ul><li><a href="https://towardsdatascience.com/graph-analytics-introduction-and-concepts-of-centrality-8f5543b55de3">Graph Analytics — Introduction and Concepts of Centrality</a></li><li><a href="https://www.analyticsvidhya.com/blog/2020/04/community-detection-graphs-networks/">Getting Started with Community Detection in Graphs and Networks</a></li></ul><h3>Future Work:</h3><p>Future work may include containing several copies of architecturally different Knowledge graphs with different rules for triple mapping. A combination of different structures might be able to better represent complex relationships. The graph may be further queried to derive more inferences.</p><h3>Contributions:</h3><p>The development of this project has been a joint effort. The team has collaborated closely on the majority of this project. The stumps we faced while getting poor results to the ideas we had which improved the performances, has been a highly cohesive collaboration. While the cohesive structure of the project is known to all, work distribution breakdown is as follows.</p><p><a href="https://www.iiitd.ac.in/tanmoy"><strong>Dr. Tanmoy Chakraborty</strong></a> (<a href="https://medium.com/u/ba1b9df6733e">TANMOY CHAKRABORTY</a>) — Mentor and guide throughout the project.</p><p><strong>Suryank Tiwari</strong> (<a href="https://medium.com/u/f5220f19938">Suryank Tiwari</a>) — Implementation and design of the Knowledge Graphs, and the subgraph querying system. Structure to triples design and conversion.</p><p><strong>Rose Verma (</strong><a href="https://medium.com/u/4f0aadbc3369">Rose Verma</a><strong>) </strong>— Extracting inferences from the Knowledge Graphs. Structure to triples design and conversion.</p><p><strong>Prateek Agarwal (</strong><a href="https://medium.com/u/6025bd767d06">Prateek Agarwal</a><strong>)</strong> — Creation of structured data from unstructured data and data preprocessing techniques.</p><p>This project has been done for the course Information Retrieval 2020 at <a href="https://www.iiitd.ac.in/">IIITD</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5710ee869505" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/structured-recipe-from-an-unstructured-corpus-5710ee869505">Structured recipe from an unstructured corpus</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>