"We want to analyse which characters speak in scenes of different plays. Therefore, we extract the corresponding information from [DraCor](https://dracor.org/) and analyse it using [Formal Concept Analysis](https://www.upriss.org.uk/fca/fca.html).\n",
"\n",
"## DraCor\n",
"\n",
"[This simple Python library](https://pypi.org/project/dracor/) allows us to download and process plays from [DraCor](https://dracor.org/). Specifically, it allows us to extract information about which character speaks in which scene."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "legislative-disclaimer",
"metadata": {},
"outputs": [],
"source": [
"!pip install dracor"
]
},
{
"cell_type": "markdown",
"id": "finite-introduction",
"metadata": {},
"source": [
"Let us now download a play and extract the speaker information from the scenes:"
"([more Shakespeare plays](https://dracor.org/shake) or [German plays](https://dracor.org/ger))"
]
},
{
"cell_type": "markdown",
"id": "virgin-administration",
"metadata": {},
"source": [
"## Formal Concept Analysis (FCA)\n",
"\n",
"To create a [context](https://concepts.readthedocs.io/en/latest/api.html#context), we need a list of *objects* (we use the scenes of the play), a list of *attributes* (we use the characters of the play), and an *incidence relation* between them (in our example whether a character appears in a scene):"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "urban-alloy",
"metadata": {},
"outputs": [],
"source": [
"objects = play_scenes.keys()\n",
"\n",
"attributes = set()\n",
"for scene, speakers in othello_scenes.items():\n",
" attributes.update(speakers)\n",
"attributes = sorted(attributes)\n",
"\n",
"incidence = [tuple([a in othello_scenes[o] for a in attributes]) for o in objects]"
]
},
{
"cell_type": "markdown",
"id": "communist-element",
"metadata": {},
"source": [
"Now we can create a formal context and compute the number of formal concepts:"
"len(ctx.lattice) # the number of formal concepts"
]
},
{
"cell_type": "markdown",
"id": "hungry-vulnerability",
"metadata": {},
"source": [
"Finally, we can (try to) visualize the concept lattics:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "piano-practice",
"metadata": {},
"outputs": [],
"source": [
"ctx.lattice.graphviz()"
]
},
{
"cell_type": "markdown",
"id": "announced-lawsuit",
"metadata": {},
"source": [
"... and save the context:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "organic-cable",
"metadata": {},
"outputs": [],
"source": [
"ctx.tofile(\"play.cxt\", frmat=\"cxt\")"
]
},
{
"cell_type": "markdown",
"id": "muslim-charity",
"metadata": {},
"source": [
"Let's test some other examples. Choose a play from the list below (recommendations: [ger/goethe-iphigenie-auf-tauris](https://dracor.org/ger/goethe-iphigenie-auf-tauris), [shake/a-midsummer-night-s-dream](https://dracor.org/shake/a-midsummer-night-s-dream)):"
We want to analyse which characters speak in scenes of different plays. Therefore, we extract the corresponding information from [DraCor](https://dracor.org/) and analyse it using [Formal Concept Analysis](https://www.upriss.org.uk/fca/fca.html).
## DraCor
[This simple Python library](https://pypi.org/project/dracor/) allows us to download and process plays from [DraCor](https://dracor.org/). Specifically, it allows us to extract information about which character speaks in which scene.
To create a [context](https://concepts.readthedocs.io/en/latest/api.html#context), we need a list of *objects* (we use the scenes of the play), a list of *attributes* (we use the characters of the play), and an *incidence relation* between them (in our example whether a character appears in a scene):
%% Cell type:code id:urban-alloy tags:
```
objects = play_scenes.keys()
attributes = set()
for scene, speakers in othello_scenes.items():
attributes.update(speakers)
attributes = sorted(attributes)
incidence = [tuple([a in othello_scenes[o] for a in attributes]) for o in objects]
```
%% Cell type:markdown id:communist-element tags:
Now we can create a formal context and compute the number of formal concepts:
Finally, we can (try to) visualize the concept lattics:
%% Cell type:code id:piano-practice tags:
```
ctx.lattice.graphviz()
```
%% Cell type:markdown id:announced-lawsuit tags:
... and save the context:
%% Cell type:code id:organic-cable tags:
```
ctx.tofile("play.cxt", frmat="cxt")
```
%% Cell type:markdown id:muslim-charity tags:
Let's test some other examples. Choose a play from the list below (recommendations: [ger/goethe-iphigenie-auf-tauris](https://dracor.org/ger/goethe-iphigenie-auf-tauris), [shake/a-midsummer-night-s-dream](https://dracor.org/shake/a-midsummer-night-s-dream)):