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

added FCA example

parent bddd71c1
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:economic-poker tags:
# 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).
## 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.
%% Cell type:code id:legislative-disclaimer tags:
```
!pip install dracor
```
%% Cell type:markdown id:finite-introduction tags:
Let us now download a play and extract the speaker information from the scenes:
%% Cell type:code id:regular-arthur tags:
```
import dracor
play = dracor.play("shake", "a-midsummer-night-s-dream")
scenes = dracor.scenes(play)
scenes
```
%% Cell type:markdown id:protecting-spare tags:
([more Shakespeare plays](https://dracor.org/shake) or [German plays](https://dracor.org/ger))
%% Cell type:markdown id:virgin-administration tags:
## 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):
%% 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:
%% Cell type:code id:grave-hungary tags:
```
import concepts
ctx = concepts.Context(objects, attributes, incidence)
len(ctx.lattice) # the number of formal concepts
```
%% Cell type:markdown id:hungry-vulnerability tags:
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)):
%% Cell type:code id:interior-steam tags:
```
from ipywidgets import interact, Dropdown
import dracor
corpora = dracor.corpora()
corpus_widget = Dropdown(options=corpora, value='shake', description='Corpus:')
play_widget = Dropdown(options=dracor.plays("shake"), value="a-midsummer-night-s-dream", description='Play:')
def on_update_corpus_widget(*args):
play_widget.options = dracor.plays(corpus_widget.value)
corpus_widget.observe(on_update_corpus_widget, 'value')
@interact(corpus=corpus_widget, play=play_widget)
def visualize_play_lattice(corpus, play):
"""Compute the lattice for the given play, show it, and save it in a file."""
scenes = dracor.scenes(dracor.play(corpus, play))
# extract formal context (G, M, I) = (objects, attributes, incidence)
objects = scenes.keys()
attributes = set()
for scene, speakers in scenes.items():
attributes.update(speakers)
attributes = sorted(attributes)
incidence = [tuple([a in scenes[o] for a in attributes]) for o in objects]
ctx = concepts.Context(objects, attributes, incidence)
print(len(ctx.lattice), "formal concepts")
ctx.tofile(play + ".cxt", frmat="cxt")
return ctx.lattice.graphviz()
```
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