Importing Mapping Metaphor into Neo4j

Michael Hunger
Neo4j Developer Blog
5 min readSep 24, 2017

I came across this tweet, which sounded really interesting.

The Metaphor Map of English shows the metaphorical links which have been identified between different areas of meaning. These links can be from the Anglo-Saxon period right up to the present day so the map covers 1300 years of the English language. This allows us the opportunity to track metaphorical ways of thinking and expressing ourselves over more than a millennium; see the Metaphor in English section for more information.

The Metaphor Map was built as part of the Mapping Metaphor with the Historical Thesaurus project. This was completed by a team in English Language at the University of Glasgow and funded by the Arts and Humanities Research Council from 2012 to early 2015. The Metaphor Map is based on the Historical Thesaurus of English, which was published in 2009 by Oxford University Press as the Historical Thesaurus of the Oxford English Dictionary.

The site is really nice and fun to explore, with an interesting data visualization of the metaphoric connections between areas of language and thought:

Metaphorical connections visualized

Metaphor in English

When most people think of metaphor, they cast their minds back to school and remember examples from poetry and drama, such as Shakespeare’s “Juliet is the sun”. This is unsurprising; metaphor is usually described as a literary phenomenon used to create arresting images in the mind of the reader. However, linguists would argue that metaphor is far more pervasive within our language and indeed within thought itself.

Useful natural language correlation network are always fun to work with, so let’s have a look at it in a graph database.

Install Neo4j & APOC

  1. Download and install Neo4j-Desktop from http://neo4j.com/download/other-releases
  2. Create a project and database and add the APOC procedure library.
  3. I also installed Neo4j Graph Algorithms to use later.
  4. Start the database.

Download Data

All the data is taken from:

Mapping Metaphor with the Historical Thesaurus. 2015. Metaphor Map of English Glasgow: University of Glasgow.

Download CSV from here

  1. select “Advanced Search”,
  2. select all categories (that you’re interested in)
  3. select “Connections between selected sections and all other sections”
  4. Metaphor Strength: “Both”
  5. Click “Search”
  6. Select “View results as a table”
  7. Click the “Download” icon in the left box

The downloaded file “metaphor.csv” should contain almost 12k lines of metaphors:

Copy metaphor.csv into the import folder of your database ("Open Folder") or in an http-accessible location to load via an http-url.

Run Import

Our data model is really simple, we have:

  1. :Category nodes with id and name.
  2. :Strong or :Weak relationships between them with the start property for the start era and examples for the example words.

A more elaborate model could model the metaphorical connection as a node, with the example words extracted as related nodes. I was just not sure, what to name each metaphor, that information was missing in the data. But for this demonstration the simpler model is good enough.

For good measure we can add a constraint.

create constraint on (c:Category) assert c.id is unique;

Run this Cypher statement to import in a few seconds

// load csv as individual lines keyed with header names
LOAD CSV WITH HEADERS FROM "file:///metaphor.csv" AS line
// get-or-create first category (note typo in name header)
merge (c1:Category {id:line.`Category 1 ID`}) ON CREATE SET c1.name=line.`Categroy 1 Name`
// get-or-create second category
merge (c2:Category {id:line.`Category 2 ID`}) ON CREATE SET c2.name=line.`Category 2 Name`
// depending on direction flip order of c1,c2
with line, case line.Direction when '>' then [c1,c2] else [c2,c1] end as cat,
// split words on ';' and remove last empty entry
apoc.coll.toSet(split(line.`Examples of metaphor`,';'))[0..-1] as words
// create relatiosnship with dynamic type, set era & words as relatiosnship properties
call apoc.create.relationship(cat[0],line.Strength,{start:line.`Start Era`, examples:words},cat[1]) yield rel
// return rows processed
return count(*)

I rendered the category nodes pretty large so that you can read the names, and have the “Strong” links display their “words” instead.

Categories, connected by Strong relationships, with example words showing on the connections.

For finding categories quickly

create index on :Category(name);

Running graph algorithms.

Degree distribution

call apoc.stats.degrees('Strong>')
union
call apoc.stats.degrees('Weak>')
degree distribution for Strong and Weak links

Top 10 Categories by in-degree

MATCH (c:Category)
WITH c,size( (c)-->()) as out,size( (c)<--()) as in
RETURN c.id, c.name,in, out
ORDER BY in DESC LIMIT 10;
╒══════╤═════════════════════════╤════╤═════╕
│"c.id"│"c.name" │"in"│"out"│
╞══════╪═════════════════════════╪════╪═════╡
│"2D06"│"Emotional suffering" │119 │7 │
├──────┼─────────────────────────┼────┼─────┤
│"2C02"│"Bad" │119 │7 │
├──────┼─────────────────────────┼────┼─────┤
│"3M06"│"Literature" │116 │29 │
├──────┼─────────────────────────┼────┼─────┤
│"1O22"│"Behaviour and conduct" │109 │10 │
├──────┼─────────────────────────┼────┼─────┤
│"3L02"│"Money" │106 │44 │
├──────┼─────────────────────────┼────┼─────┤
│"2C01"│"Good" │105 │2 │
├──────┼─────────────────────────┼────┼─────┤
│"1P28"│"Greatness and intensity"│104 │2 │
├──────┼─────────────────────────┼────┼─────┤
│"2A22"│"Truth and falsity" │104 │5 │
├──────┼─────────────────────────┼────┼─────┤
│"2D08"│"Love and friendship" │100 │17 │
├──────┼─────────────────────────┼────┼─────┤
│"2A18"│"Intelligibility" │99 │5 │
└──────┴─────────────────────────┴────┴─────┘

Outgoing Page-Rank of Categories

After installing the neo4j-graph-algorithms into the “plugins” directory, and adding the config option to enable it, we can also run a few graph algorithms on the data.

call algo.pageRank.stream('Category','Strong') yield node, score
with node, toInt(score*10) as score order by score desc limit 10
return node.name, score/10.0 as score;
╒══════════════════════════════════════╤═══════╕
│"node.name" │"score"│
╞══════════════════════════════════════╪═══════╡
│"Greatness and intensity" │5.6 │
├──────────────────────────────────────┼───────┤
│"Colour " │3.5 │
├──────────────────────────────────────┼───────┤
│"Unimportance" │3.5 │
├──────────────────────────────────────┼───────┤
│"Importance" │3.4 │
├──────────────────────────────────────┼───────┤
│"Hatred and hostility" │3.4 │
├──────────────────────────────────────┼───────┤
│"Plants" │2.9 │
├──────────────────────────────────────┼───────┤
│"Good" │2.9 │
├──────────────────────────────────────┼───────┤
│"Age" │2.8 │
├──────────────────────────────────────┼───────┤
│"Love and friendship" │2.7 │
├──────────────────────────────────────┼───────┤
│"Memory, commemoration and revocation"│2.6 │
└──────────────────────────────────────┴───────┘

Funny that both importance and unimportance have such a high rank.

Betweenness Centrality

Which categories connect others:

call algo.betweenness.stream('Category','Strong') yield nodeId, centrality as score
match (node) where id(node) = nodeId
with node, toInt(score) as score order by score desc limit 10
return node.id, node.name, score;
╒═════════╤═══════════════════════════════════════════╤═══════╕
│"node.id"│"node.name" │"score"│
╞═════════╪═══════════════════════════════════════════╪═══════╡
│"2C01" │"Good" │165912 │
├─────────┼───────────────────────────────────────────┼───────┤
│"1E02" │"Animal categories, habitats and behaviour"│131109 │
├─────────┼───────────────────────────────────────────┼───────┤
│"3D05" │"Authority, rebellion and freedom" │108292 │
├─────────┼───────────────────────────────────────────┼───────┤
│"2D06" │"Emotional suffering" │87551 │
├─────────┼───────────────────────────────────────────┼───────┤
│"1J34" │"Colour " │83595 │
├─────────┼───────────────────────────────────────────┼───────┤
│"1E05" │"Insects and other invertebrates" │77171 │
├─────────┼───────────────────────────────────────────┼───────┤
│"3D01" │"Command and control" │71873 │
├─────────┼───────────────────────────────────────────┼───────┤
│"1O20" │"Vigorous action and degrees of violence" │65028 │
├─────────┼───────────────────────────────────────────┼───────┤
│"1C03" │"Mental health" │64567 │
├─────────┼───────────────────────────────────────────┼───────┤
│"1F01" │"Plants" │59444 │
└─────────┴───────────────────────────────────────────┴───────┘

There are many other explorative queries and insights we can draw from this.

Let me know in the comments what you’d be interested in.

--

--

Michael Hunger
Neo4j Developer Blog

A software developer passionate about teaching and learning. Currently working with Neo4j, GraphQL, Kotlin, ML/AI, Micronaut, Spring, Kafka, and more.