Skip to content

Instantly share code, notes, and snippets.

@fnielsen
Created March 5, 2020 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnielsen/499d1faaf8601e21e9ff54d0212e565e to your computer and use it in GitHub Desktop.
Save fnielsen/499d1faaf8601e21e9ff54d0212e565e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Homographs - wordnet\n",
"===================="
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from os.path import expanduser, join\n",
"from rdflib import Graph\n",
"from nltk.corpus import wordnet as wn"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"dannet_path = expanduser(join('~', 'data', 'dannet', 'DanNet-2.2_owl'))\n",
"words_filename = join(dannet_path, 'words.rdf')\n",
"wordsenses_filename = join(dannet_path, 'wordsenses.rdf')\n",
"glossary_filename = join(dannet_path, 'glossary.rdf')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Graph identifier=N6f37c91961f6412ba1ad6facec1afa73 (<class 'rdflib.graph.Graph'>)>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = Graph()\n",
"g.parse(words_filename)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Graph identifier=N6f37c91961f6412ba1ad6facec1afa73 (<class 'rdflib.graph.Graph'>)>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.parse(wordsenses_filename)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Graph identifier=N6f37c91961f6412ba1ad6facec1afa73 (<class 'rdflib.graph.Graph'>)>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.parse(glossary_filename)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"query = \"\"\"\n",
"PREFIX wn20schema: <http://www.w3.org/2006/03/wn/wn20/schema/>\n",
"\n",
"SELECT ?synset ?wordsense ?word ?gloss {\n",
" ?word wn20schema:lexicalForm \"bil\" .\n",
" ?wordsense wn20schema:word ?word .\n",
" ?synset wn20schema:containsWordSense ?wordsense . \n",
" ?synset wn20schema:gloss ?gloss .\n",
"}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"query_results = g.query(query)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-\n",
"firehjulet motorkøretøj til person- el. godstransp ... (Brug: \"min bil gik i stå, fordi der ikke var mere benzin i den || du har overhovedet ikke herredømme over bilen, når du enten har sat bilen i frigear eller har koblet ud\")\n"
]
}
],
"source": [
"for row in query_results:\n",
" print(\"-\")\n",
" print(row[3])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"synsets = wn.synsets(\"station\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-\n",
"a facility equipped with special equipment and personnel for a particular purpose\n",
"-\n",
"proper or designated social situation\n",
"-\n",
"(nautical) the location to which a ship or fleet is assigned for duty\n",
"-\n",
"the position where someone (as a guard or sentry) stands or is assigned to stand\n",
"-\n",
"the frequency assigned to a broadcasting station\n",
"-\n",
"assign to a station\n"
]
}
],
"source": [
"for synset in synsets:\n",
" print('-')\n",
" print(synset.definition())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment