Skip to content
Snippets Groups Projects
Commit c4ace07c authored by Prof. Dr. Robert Jäschke's avatar Prof. Dr. Robert Jäschke
Browse files

+FCA

parent 6ca894ac
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:economic-poker tags: %% Cell type:markdown id:economic-poker tags:
# FCA & DraCor # FCA & DraCor
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). 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) (FCA).
## DraCor ## 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. [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 id:legislative-disclaimer tags: %% Cell type:code id:legislative-disclaimer tags:
``` python ``` python
!pip install dracor !pip install dracor
``` ```
%% Cell type:markdown id:finite-introduction tags: %% Cell type:markdown id:finite-introduction tags:
Let us now download a play and extract the speaker information from the scenes: Let us now download a play and extract the speaker information from the scenes:
%% Cell type:code id:regular-arthur tags: %% Cell type:code id:regular-arthur tags:
``` python ``` python
import dracor import dracor
play = dracor.play("ger", "goethe-iphigenie-auf-tauris") play = dracor.play("ger", "goethe-iphigenie-auf-tauris")
scenes = dracor.scenes(play) scenes = dracor.scenes(play)
scenes scenes
``` ```
%% Output %% Output
{'a0_s0': {'#iphigenie'}, {'a0_s0': {'#iphigenie'},
'a0_s1': {'#arkas', '#iphigenie'}, 'a0_s1': {'#arkas', '#iphigenie'},
'a0_s2': {'#iphigenie', '#thoas'}, 'a0_s2': {'#iphigenie', '#thoas'},
'a0_s3': {'#iphigenie'}, 'a0_s3': {'#iphigenie'},
'a1_s0': {'#orest', '#pylades'}, 'a1_s0': {'#orest', '#pylades'},
'a1_s1': {'#iphigenie', '#pylades'}, 'a1_s1': {'#iphigenie', '#pylades'},
'a2_s0': {'#iphigenie', '#orest'}, 'a2_s0': {'#iphigenie', '#orest'},
'a2_s1': {'#orest'}, 'a2_s1': {'#orest'},
'a2_s2': {'#iphigenie', '#orest', '#pylades'}, 'a2_s2': {'#iphigenie', '#orest', '#pylades'},
'a3_s0': {'#iphigenie'}, 'a3_s0': {'#iphigenie'},
'a3_s1': {'#arkas', '#iphigenie'}, 'a3_s1': {'#arkas', '#iphigenie'},
'a3_s2': {'#iphigenie'}, 'a3_s2': {'#iphigenie'},
'a3_s3': {'#iphigenie', '#pylades'}, 'a3_s3': {'#iphigenie', '#pylades'},
'a3_s4': {'#iphigenie'}, 'a3_s4': {'#iphigenie'},
'a4_s0': {'#arkas', '#thoas'}, 'a4_s0': {'#arkas', '#thoas'},
'a4_s1': {'#thoas'}, 'a4_s1': {'#thoas'},
'a4_s2': {'#iphigenie', '#thoas'}, 'a4_s2': {'#iphigenie', '#thoas'},
'a4_s3': {'#iphigenie', '#orest', '#thoas'}, 'a4_s3': {'#iphigenie', '#orest', '#thoas'},
'a4_s4': {'#arkas', '#orest', '#pylades', '#thoas'}, 'a4_s4': {'#arkas', '#orest', '#pylades', '#thoas'},
'a4_s5': {'#iphigenie', '#orest', '#thoas'}} 'a4_s5': {'#iphigenie', '#orest', '#thoas'}}
%% Cell type:markdown id:protecting-spare tags: %% Cell type:markdown id:protecting-spare tags:
([more Shakespeare plays](https://dracor.org/shake) or [German plays](https://dracor.org/ger)) ([more Shakespeare plays](https://dracor.org/shake) or [German plays](https://dracor.org/ger))
%% Cell type:markdown id:virgin-administration tags: %% Cell type:markdown id:virgin-administration tags:
## Formal Concept Analysis (FCA) ## Formal Concept Analysis (FCA)
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): 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: %% Cell type:code id:urban-alloy tags:
``` python ``` python
objects = scenes.keys() objects = scenes.keys()
attributes = set() attributes = set()
for scene, speakers in scenes.items(): for scene, speakers in scenes.items():
attributes.update(speakers) attributes.update(speakers)
attributes = sorted(attributes) attributes = sorted(attributes)
incidence = [tuple([a in scenes[o] for a in attributes]) for o in objects] incidence = [tuple([a in scenes[o] for a in attributes]) for o in objects]
``` ```
%% Cell type:markdown id:every-capture tags: %% Cell type:markdown id:durable-multimedia tags:
We use the [concepts](https://github.com/xflr6/concepts) Python library for FCA: We use the [concepts](https://github.com/xflr6/concepts) Python library for FCA:
%% Cell type:code id:younger-playback tags: %% Cell type:code id:ranking-island tags:
``` python ``` python
!pip install concepts !pip install concepts
``` ```
%% Cell type:markdown id:communist-element tags: %% Cell type:markdown id:communist-element tags:
Now we can create a formal context and compute the number of formal concepts: Now we can create a formal context and compute the number of formal concepts:
%% Cell type:code id:grave-hungary tags: %% Cell type:code id:grave-hungary tags:
``` python ``` python
import concepts import concepts
ctx = concepts.Context(objects, attributes, incidence) ctx = concepts.Context(objects, attributes, incidence)
len(ctx.lattice) # the number of formal concepts len(ctx.lattice) # the number of formal concepts
``` ```
%% Output %% Output
17 17
%% Cell type:markdown id:hungry-vulnerability tags: %% Cell type:markdown id:hungry-vulnerability tags:
Finally, we can (try to) visualize the concept lattice: Finally, we can (try to) visualize the concept lattice:
%% Cell type:code id:piano-practice tags: %% Cell type:code id:piano-practice tags:
``` python ``` python
ctx.lattice.graphviz() ctx.lattice.graphviz()
``` ```
%% Output %% Output
<graphviz.graphs.Digraph at 0x7f3682f591f0> <graphviz.graphs.Digraph at 0x7f3682f591f0>
%% Cell type:markdown id:announced-lawsuit tags: %% Cell type:markdown id:announced-lawsuit tags:
... and save the context: ... and save the context:
%% Cell type:code id:organic-cable tags: %% Cell type:code id:organic-cable tags:
``` python ``` python
ctx.tofile("play.cxt", frmat="cxt") ctx.tofile("play.cxt", frmat="cxt")
``` ```
%% Cell type:markdown id:muslim-charity tags: %% 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)): 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)):
%% Cell type:code id:interior-steam tags: %% Cell type:code id:interior-steam tags:
``` python ``` python
from ipywidgets import interact, Dropdown from ipywidgets import interact, Dropdown
import dracor import dracor
corpora = dracor.corpora() corpora = dracor.corpora()
corpus_widget = Dropdown(options=corpora, value='shake', description='Corpus:') corpus_widget = Dropdown(options=corpora, value='shake', description='Corpus:')
play_widget = Dropdown(options=dracor.plays("shake"), value="a-midsummer-night-s-dream", description='Play:') play_widget = Dropdown(options=dracor.plays("shake"), value="a-midsummer-night-s-dream", description='Play:')
def on_update_corpus_widget(*args): def on_update_corpus_widget(*args):
play_widget.options = dracor.plays(corpus_widget.value) play_widget.options = dracor.plays(corpus_widget.value)
corpus_widget.observe(on_update_corpus_widget, 'value') corpus_widget.observe(on_update_corpus_widget, 'value')
@interact(corpus=corpus_widget, play=play_widget) @interact(corpus=corpus_widget, play=play_widget)
def visualize_play_lattice(corpus, play): def visualize_play_lattice(corpus, play):
"""Compute the lattice for the given play, show it, and save it in a file.""" """Compute the lattice for the given play, show it, and save it in a file."""
scenes = dracor.scenes(dracor.play(corpus, play)) scenes = dracor.scenes(dracor.play(corpus, play))
# extract formal context (G, M, I) = (objects, attributes, incidence) # extract formal context (G, M, I) = (objects, attributes, incidence)
objects = scenes.keys() objects = scenes.keys()
attributes = set() attributes = set()
for scene, speakers in scenes.items(): for scene, speakers in scenes.items():
attributes.update(speakers) attributes.update(speakers)
attributes = sorted(attributes) attributes = sorted(attributes)
incidence = [tuple([a in scenes[o] for a in attributes]) for o in objects] incidence = [tuple([a in scenes[o] for a in attributes]) for o in objects]
ctx = concepts.Context(objects, attributes, incidence) ctx = concepts.Context(objects, attributes, incidence)
print(len(ctx.lattice), "formal concepts") print(len(ctx.lattice), "formal concepts")
ctx.tofile(play + ".cxt", frmat="cxt") ctx.tofile(play + ".cxt", frmat="cxt")
return ctx.lattice.graphviz() return ctx.lattice.graphviz()
``` ```
%% Output %% Output
Widget Javascript not detected. It may not be installed or enabled properly. Reconnecting the current kernel may help. Widget Javascript not detected. It may not be installed or enabled properly. Reconnecting the current kernel may help.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment