In [66]:
import matplotlib.pyplot as plt
%matplotlib inline

In [67]:
from gensim.models.word2vec import Word2Vec

In this section, you can decide which model for which corpus you want to use for your analysis. Here by default, it's the critical corpus within the years 1820-1940. We tested over 10-year slides, it's clearly much less effective due to the small size of the French corpora. After the load, put the path to your directory of models, and then select your range in "year in range".


In [68]:
from collections import OrderedDict

models = OrderedDict([
    (year, Word2Vec.load('/home/odysseus/Téléchargements/hist-vec-master/crit/LemCorpCrit_models/{}.bin'.format(year)))
    for year in range(1840, 1940, 20)
])

In [69]:
def cosine_series(anchor, query):
    
    series = OrderedDict()
    
    for year, model in models.items():
        
        series[year] = (
            model.similarity(anchor, query)
            if query in model else 0
        )

    return series

In [70]:
import numpy as np
import statsmodels.api as sm

def lin_reg(series):

    x = np.array(list(series.keys()))
    y = np.array(list(series.values()))

    x = sm.add_constant(x)

    return sm.OLS(y, x).fit()

In [71]:
def plot_cosine_series(anchor, query, w=8, h=4):
    
    series = cosine_series(anchor, query)
    
    fit = lin_reg(series)

    x1 = list(series.keys())[0]
    x2 = list(series.keys())[-1]

    y1 = fit.predict()[0]
    y2 = fit.predict()[-1]
    
    print(query)
    
    plt.figure(figsize=(w, h))
    plt.ylim(0, 1)
    plt.title(query)
    plt.xlabel('Year')
    plt.ylabel('Similarity')
    plt.plot(list(series.keys()), list(series.values()))
    plt.plot([x1, x2], [y1, y2], color='gray', linewidth=0.5)
    plt.show()

Similarity of a list of key terms to "littérature"

The cell below (which calls the methods above), shows how two words differ from one another through time, with cosine similarity. Here, we show how a list of selected concepts evolves compared to "littérature". You can manually change both below.


In [72]:
testList = ('littérature','poésie', 'science', 'savoir', 'histoire', 'philosophie', 'lettre', 'critique', 
            'roman', 'théâtre', 'drame', 'esprit', 'langue', 'diplomatie', 'politique', 'morale', 'société', 
            'pouvoir', 'théologie', 'droit', 'loi', 'méthode', 'génie', 'romantisme', 'réalisme', 'symbolisme', 
            'naturalisme')

for idx, val in enumerate(testList):
    if idx>0:
        plot_cosine_series('littérature', val)


poésie
science
savoir
histoire
philosophie
lettre
critique
roman
théâtre
drame
esprit
langue
diplomatie
politique
morale
société
pouvoir
théologie
droit
loi
méthode
génie
romantisme
réalisme
symbolisme
naturalisme

The two next cells get the 200 most similar terms to a specific term, from the training models, here "littérature".


In [73]:
def union_neighbor_vocab(anchor, topn=200):
    
    vocab = set()
    
    for year, model in models.items():
        similar = model.most_similar(anchor, topn=topn)
        vocab.update([s[0] for s in similar])
        
    return vocab

At this point, we'll do the same thing as above, and calculate, for each token in the 200 nearest terms to the main entry, the proximity of this term and its significance. The significance is calculated with the p value, that is to say that, below a certain threshold (0.05) we have a strong likelyhood that the result is sure and significant.


In [74]:
testList = ('littérature','poésie', 'science', 'savoir', 'histoire', 'philosophie', 'lettre', 'critique', 
            'roman', 'théâtre', 'drame', 'esprit', 'langue', 'diplomatie', 'politique', 'morale', 'société', 
            'pouvoir', 'théologie', 'droit', 'loi', 'méthode', 'génie', 'romantisme', 'réalisme', 'symbolisme', 
            'naturalisme')
entries={}

for word in testList:
    data = []
    for token in union_neighbor_vocab(word):
    
        series = cosine_series(word, token)
        fit = lin_reg(series)
    
        if fit.pvalues[1] < 0.05:
            data.append((token, fit.params[1], fit.pvalues[1]))
    entries[word]=data

Increasing

In this part, we want to show what terms emerge more and more with the main entry, that is to say each word of the given test list. The "slope" is the degree of progress, and the "p" value its efficiency. So here, the main emergence with "littérature" which is significant is "humanisme". All terms seem to be significant, except "fédéralisme", "welschinger", "maniere", "hennet", "réapparition", "deffence", "bourgin", "colonie", "naturalisme", "réalisme", "sillery", "gréco", "compétence", "symbolisme", "catholique", "japonais", "manuel", "romand", "topographie, "organisme", "prédominance". That is to say that those terms can be nearest, but that statistically they are not significant enough to be sure, while the others are more certain.

In this following cell, we show how the top ten most similar vectors change through time compared to the words in the test list : "humanisme" for example seems to be very rare before 1860, and then becomes more and more similar to "littérature". Those show the terms that were not similar in the beginning, but tend to be more and more related to "littérature". You should keep in mind the p values associated to each vector.


In [75]:
import pandas as pd
from IPython.display import Markdown, display
pd.set_option('display.max_rows', 1000)

for word in testList :
    display(Markdown("### <i><b>"+word+"</i></b>"))
    df1 = pd.DataFrame(entries[word], columns=('token', 'slope', 'p'))
    print(df1.sort_values('slope', ascending=False).head(10))
    print('\n\n')
    
    for i, row in df1.sort_values('slope', ascending=False).head(10).iterrows():
        plot_cosine_series(word, row['token'], 8, 4)


littérature</b>

            token     slope         p
35      humanisme  0.010302  0.019004
8   professionnel  0.008294  0.001361
15        éthique  0.007957  0.003634
23     symboliste  0.007367  0.022680
6     orientation  0.007313  0.047529
19     adaptation  0.006944  0.049543
2          apport  0.006558  0.032944
10         manuel  0.006177  0.015870
36     catholique  0.005987  0.007488
31       réaliste  0.005107  0.033174



humanisme
professionnel
éthique
symboliste
orientation
adaptation
apport
manuel
catholique
réaliste

poésie</b>

            token     slope         p
31      humanisme  0.009166  0.013652
18        éthique  0.007888  0.006259
7     orientation  0.007747  0.024581
10  professionnel  0.007081  0.039541
42     sociologie  0.006947  0.019920
32        concept  0.006696  0.010612
23     symboliste  0.006564  0.049029
13      conscient  0.006447  0.005222
24        lyrisme  0.004999  0.025161
41    psychologie  0.004763  0.002939



humanisme
éthique
orientation
professionnel
sociologie
concept
symboliste
conscient
lyrisme
psychologie

science</b>

            token     slope         p
15      humanisme  0.010324  0.015803
6         éthique  0.009705  0.003954
16        concept  0.007712  0.032518
4   professionnel  0.007474  0.001775
12         verbal  0.007292  0.045695
9      adaptation  0.007090  0.041557
1          apport  0.006653  0.015005
5          manuel  0.004922  0.026045
13    réalisation  0.004023  0.008931
10     artistique  0.003804  0.002656



humanisme
éthique
concept
professionnel
verbal
adaptation
apport
manuel
réalisation
artistique

savoir</b>

      token     slope         p
9  pâtisser  0.006054  0.028294
4     viser  0.004455  0.019309
5    risque  0.004404  0.043395
8  détester  0.002874  0.016979
0     gêner  0.002809  0.002437
2   railler  0.002230  0.022109
6     gâter  0.002218  0.028621
3    métier  0.001241  0.002480
7    vanter -0.001712  0.049584
1     seoir -0.004003  0.008388



pâtisser
viser
risque
détester
gêner
railler
gâter
métier
vanter
seoir

histoire</b>

          token     slope         p
24    humanisme  0.009313  0.007254
30   sociologie  0.008232  0.027857
17  welschinger  0.007957  0.026897
29    mentalité  0.007956  0.044890
26    réédition  0.007857  0.007885
9   orientation  0.007588  0.025802
14    référence  0.007373  0.006875
15    evolution  0.007348  0.015108
20   adaptation  0.006832  0.013572
1     vauquelin  0.006252  0.020016



humanisme
sociologie
welschinger
mentalité
réédition
orientation
référence
evolution
adaptation
vauquelin

philosophie</b>

             token     slope         p
23       humanisme  0.011137  0.017477
14         éthique  0.009203  0.002645
7      orientation  0.008874  0.032780
16      relativité  0.008523  0.004917
26         concept  0.007430  0.031548
0   inconnaissable  0.007134  0.046298
15         facteur  0.007120  0.020241
9    professionnel  0.006998  0.016379
4           apport  0.006054  0.033386
3        doctrinal  0.005609  0.033767



humanisme
éthique
orientation
relativité
concept
inconnaissable
facteur
professionnel
apport
doctrinal

lettre</b>

        token     slope         p
20  boulenger  0.006318  0.033882
9     rostand  0.006239  0.033194
2    goncourt  0.006113  0.007593
8        huis  0.005111  0.044397
0       meyer  0.005109  0.031397
10    enquête  0.004546  0.031753
21      renan  0.003846  0.006126
23     cahier  0.003013  0.038350
7      manuel  0.002998  0.046705
18   aubignac  0.002865  0.009442



boulenger
rostand
goncourt
huis
meyer
enquête
renan
cahier
manuel
aubignac

critique</b>

            token     slope         p
9   professionnel  0.007992  0.001677
27     brunetière  0.007584  0.019840
34      mentalité  0.007276  0.041012
8     spécialiste  0.006832  0.026375
22       flaubert  0.006129  0.038399
21       stendhal  0.005859  0.012322
10         manuel  0.005489  0.001237
18          taine  0.005356  0.028558
1          apport  0.005130  0.002134
23       réaliste  0.004924  0.028495



professionnel
brunetière
mentalité
spécialiste
flaubert
stendhal
manuel
taine
apport
réaliste

roman</b>

               token     slope         p
25         réédition  0.008054  0.017591
23        brunetière  0.006853  0.009692
4             ecrits  0.006385  0.008796
19          flaubert  0.005985  0.032393
16         fascicule  0.005962  0.010966
0   autobiographique  0.005752  0.021953
28       psychologie  0.005001  0.024115
3             manuel  0.004821  0.016735
17          stendhal  0.003796  0.032441
5      psychologique  0.003643  0.046890



réédition
brunetière
ecrits
flaubert
fascicule
autobiographique
psychologie
manuel
stendhal
psychologique

théâtre</b>

          token     slope         p
1    arlésienne  0.007858  0.018918
20         fail  0.006452  0.043836
15  classicisme  0.006441  0.038381
14   plébiscite  0.006274  0.003488
6        bartas  0.005827  0.044500
7       cénacle  0.003865  0.011995
13     parnasse  0.002804  0.028749
18  shakespeare  0.002791  0.003469
5          type  0.002581  0.047111
17     dialogue  0.002164  0.005454



arlésienne
fail
classicisme
plébiscite
bartas
cénacle
parnasse
shakespeare
type
dialogue

drame</b>

           token     slope         p
0     arlésienne  0.008230  0.022633
10     pacifisme  0.007402  0.049431
5   déséquilibre  0.007115  0.017128
22       concept  0.006891  0.014500
19         recul  0.006337  0.029939
4         cliché  0.006004  0.019917
9        facteur  0.004261  0.008167
1        symbole  0.003538  0.011245
26   psychologie  0.003240  0.017406
8     romantique  0.002358  0.005027



arlésienne
pacifisme
déséquilibre
concept
recul
cliché
facteur
symbole
psychologie
romantique

esprit</b>

             token     slope         p
6        conscient  0.008319  0.000038
5      inconscient  0.007676  0.002770
15  individualisme  0.006321  0.022916
2         hérédité  0.005464  0.029999
11       incapable  0.004584  0.024917
25      discipline  0.004183  0.038798
16          vision  0.004068  0.025569
23   compréhension  0.003943  0.043196
19    personnalité  0.003904  0.001634
3        intuition  0.003895  0.015316



conscient
inconscient
individualisme
hérédité
incapable
discipline
vision
compréhension
personnalité
intuition

langue</b>

            token     slope         p
22      humanisme  0.009380  0.010400
29      mentalité  0.008766  0.044714
3   professionnel  0.008668  0.008152
31     sociologie  0.006649  0.036656
10     adaptation  0.006539  0.022217
9      romantique  0.004254  0.009023
23     catholique  0.004068  0.032273
13       majorité  0.004041  0.032740
20     théoricien  0.003533  0.006794
1          france  0.003433  0.038887



humanisme
mentalité
professionnel
sociologie
adaptation
romantique
catholique
majorité
théoricien
france

diplomatie</b>

              token     slope         p
71     accoutumance  0.012401  0.018452
72          édicter  0.012000  0.017030
43           libido  0.011935  0.043128
49   évolutionniste  0.011805  0.019631
14         médiéval  0.011489  0.033102
4   automatiquement  0.011313  0.013109
36        ancestral  0.011276  0.024314
26       machinerie  0.011262  0.012191
39     pornographie  0.011246  0.007880
20       frondaison  0.011242  0.006671



accoutumance
édicter
libido
évolutionniste
médiéval
automatiquement
ancestral
machinerie
pornographie
frondaison

politique</b>

            token     slope         p
29     sociologie  0.009202  0.032623
22      humanisme  0.009007  0.020162
24        évoluer  0.008123  0.007157
11        éthique  0.008014  0.000936
7    sociologique  0.007912  0.024952
18     symboliste  0.007876  0.016387
14       ethnique  0.006631  0.027557
13   linguistique  0.006483  0.038141
25        concept  0.006414  0.028780
5   vulgarisation  0.006250  0.017014



sociologie
humanisme
évoluer
éthique
sociologique
symboliste
ethnique
linguistique
concept
vulgarisation

morale</b>

             token     slope         p
16         éthique  0.009229  0.003567
31      sociologie  0.008759  0.044621
9        conscient  0.008699  0.012856
23       humanisme  0.008354  0.039487
6      inconscient  0.008275  0.008551
0   inconnaissable  0.007942  0.038572
25         concept  0.007938  0.030949
7      objectivité  0.007583  0.010090
5    professionnel  0.006264  0.019443
22           total  0.005670  0.001354



éthique
sociologie
conscient
humanisme
inconscient
inconnaissable
concept
objectivité
professionnel
total

société</b>

         token     slope         p
19           s  0.010440  0.048208
28   humanisme  0.009360  0.012021
32  révocation  0.009260  0.001978
42         mém  0.008870  0.026577
43  sociologie  0.008341  0.015438
17  symboliste  0.007721  0.026858
0     toulouse  0.007333  0.044755
10     facteur  0.007258  0.042185
11  adaptation  0.007153  0.004541
12     mémoire  0.007135  0.016869



s
humanisme
révocation
mém
sociologie
symboliste
toulouse
facteur
adaptation
mémoire

pouvoir</b>

            token     slope         p
8          cosmos  0.006506  0.033525
10         risque  0.004583  0.023295
16     discipline  0.003366  0.026525
7        réaliser  0.003244  0.047176
15     réellement  0.003212  0.027874
17      percevoir  0.003005  0.000270
6         formule  0.002461  0.019813
19    précisément  0.002380  0.025222
5     originalité  0.002370  0.047944
3   naturellement  0.002314  0.016528



cosmos
risque
discipline
réaliser
réellement
percevoir
formule
précisément
originalité
naturellement

théologie</b>

              token     slope         p
38        humanisme  0.011796  0.025058
4                sc  0.010558  0.002768
27       symboliste  0.010449  0.009760
12     sociologique  0.010388  0.027225
1       fédéralisme  0.010293  0.020457
10      orientation  0.010264  0.036678
44  instrumentation  0.010202  0.018235
18         economie  0.010124  0.005620
26          corrèze  0.010103  0.012128
31          maugras  0.010060  0.011387



humanisme
sc
symboliste
sociologique
fédéralisme
orientation
instrumentation
economie
corrèze
maugras

droit</b>

           token     slope         p
15   castagnette  0.005653  0.038444
14  reproduction  0.005270  0.026960
21         pacte  0.004535  0.041624
6        salaire  0.004500  0.023091
8       majorité  0.003360  0.047964
1      règlement  0.001205  0.049223
13       libéral  0.001009  0.030495
7           code -0.000877  0.012986
9     obéissance -0.001873  0.020389
5       garantie -0.001939  0.004626



castagnette
reproduction
pacte
salaire
majorité
règlement
libéral
code
obéissance
garantie

loi</b>

          token     slope         p
11      facteur  0.008098  0.047704
8     conscient  0.007278  0.021705
7   inconscient  0.007228  0.003676
1        apport  0.005466  0.040811
2     intuition  0.004876  0.032810
12   divergence  0.004417  0.026987
18  réalisation  0.004195  0.033085
10   séparation  0.004173  0.019730
5       conflit  0.003994  0.020739
14     majorité  0.003969  0.008353



facteur
conscient
inconscient
apport
intuition
divergence
réalisation
séparation
conflit
majorité

méthode</b>

             token     slope         p
15         éthique  0.009695  0.011454
6      orientation  0.009458  0.042818
9      objectivité  0.008452  0.017898
10       conscient  0.008203  0.007583
8    professionnel  0.007688  0.016970
3           apport  0.005434  0.003098
21  individualisme  0.005328  0.040407
22     réalisation  0.004716  0.045058
30       idéalisme  0.003802  0.048683
4       esthétique  0.002216  0.040282



éthique
orientation
objectivité
conscient
professionnel
apport
individualisme
réalisation
idéalisme
esthétique

génie</b>

            token     slope         p
8       conscient  0.007859  0.000248
6   professionnel  0.007078  0.016404
24        concept  0.006486  0.018487
20   déterminisme  0.006152  0.048914
3      pessimisme  0.005603  0.019686
1          apport  0.005474  0.033953
17    détachement  0.005224  0.020071
19        lyrisme  0.004757  0.023723
14     dramaturge  0.004176  0.015516
11     romantique  0.004173  0.001099



conscient
professionnel
concept
déterminisme
pessimisme
apport
détachement
lyrisme
dramaturge
romantique

romantisme</b>

            token     slope         p
55      humanisme  0.009400  0.029588
46      pacifisme  0.008884  0.046662
41   nationalisme  0.008766  0.012548
44     symboliste  0.007830  0.039538
4    subconscient  0.007579  0.040010
27    parnassiens  0.006914  0.047643
48    déformation  0.006341  0.019789
12  professionnel  0.005866  0.019548
39        surtout  0.004544  0.013432
56     catholique  0.004253  0.007133



humanisme
pacifisme
nationalisme
symboliste
subconscient
parnassiens
déformation
professionnel
surtout
catholique

réalisme</b>

              token     slope         p
50  traditionalisme  0.011994  0.048885
32     nationalisme  0.011300  0.037242
5      subconscient  0.011163  0.035843
13     sociologique  0.010926  0.012673
14         médiéval  0.010896  0.005821
63    subjectivisme  0.010882  0.006278
51  instrumentation  0.010867  0.006843
46   évolutionniste  0.010796  0.005640
39        moralisme  0.010773  0.004823
29         ethnique  0.010766  0.008599



traditionalisme
nationalisme
subconscient
sociologique
médiéval
subjectivisme
instrumentation
évolutionniste
moralisme
ethnique

symbolisme</b>

              token     slope         p
58             fail  0.010605  0.006520
48  traditionalisme  0.010440  0.046668
29     nationalisme  0.010225  0.032394
32        pacifisme  0.010215  0.045741
15     sociologique  0.010174  0.018754
2       positivisme  0.009913  0.029861
8      subconscient  0.009634  0.049815
46           vêture  0.009557  0.013902
7        qualitatif  0.009490  0.035942
24      parnassiens  0.009296  0.021654



fail
traditionalisme
nationalisme
pacifisme
sociologique
positivisme
subconscient
vêture
qualitatif
parnassiens

naturalisme</b>

              token     slope         p
33  traditionalisme  0.011469  0.041427
5      subconscient  0.011179  0.020818
16     nationalisme  0.011042  0.031501
11      parnassiens  0.011031  0.006847
39          sénégal  0.011029  0.028776
30   évolutionniste  0.010822  0.010046
27     cléricalisme  0.010686  0.035960
8      sociologique  0.010600  0.019431
42    subjectivisme  0.010499  0.007542
13     déclassement  0.010493  0.035596



traditionalisme
subconscient
nationalisme
parnassiens
sénégal
évolutionniste
cléricalisme
sociologique
subjectivisme
déclassement

Decreasing

This is the same process here : we want to see which terms tend to disassociate themselves from "littérature" (by default, which you can change with the trained models). Then again, you have to check the p values. "transplantation", "choeur" and "philé" are not considered significant, "chaldéen" is, and "destination", "morceau", etc. are as well. The fact that those are less significant is logical : the fewer the terms, the more erratic their series tend to be.


In [76]:
for word in testList :
    display(Markdown("### <i><b>"+word+"</i></b>"))
    df2 = pd.DataFrame(entries[word], columns=('token', 'slope', 'p'))
    print(df2.sort_values('slope', ascending=True).head(10))
    print('\n\n')
    
    for i, row in df2.sort_values('slope', ascending=True).head(10).iterrows():
        plot_cosine_series(word, row['token'], 8, 4)


littérature</b>

          token     slope         p
41   naturalisé -0.004513  0.042377
34  théologique -0.003168  0.017397
39     barbarie -0.003002  0.020017
7      monument -0.002199  0.010337
26  comparaison -0.002012  0.041023
11         goût -0.001436  0.022249
38      domaine  0.001052  0.005780
27      utilité  0.001188  0.040852
32   prétention  0.001296  0.034148
40        revue  0.001511  0.012991



naturalisé
théologique
barbarie
monument
comparaison
goût
domaine
utilité
prétention
revue

poésie</b>

       token     slope         p
16  enjambée -0.007023  0.037809
2      haydn -0.005180  0.046533
34      lied -0.004957  0.039804
40    sonnet -0.003953  0.044455
17  héroïque -0.003859  0.027892
11      naïf -0.003788  0.031220
37    homère -0.003463  0.019579
35       ode -0.003238  0.018120
39  dialecte -0.003125  0.003727
25     grèce -0.002943  0.016600



enjambée
haydn
lied
sonnet
héroïque
naïf
homère
ode
dialecte
grèce

science</b>

            token     slope         p
11    comparaison -0.003810  0.036434
14    physiologie -0.002929  0.035389
0   investigation -0.002852  0.025221
8      obligation  0.001207  0.031690
2      esthétique  0.001881  0.014456
18     hiérarchie  0.002097  0.038372
7      convention  0.002538  0.008362
3           thèse  0.003069  0.018877
17     démocratie  0.003162  0.007856
10     artistique  0.003804  0.002656



comparaison
physiologie
investigation
obligation
esthétique
hiérarchie
convention
thèse
démocratie
artistique

savoir</b>

      token     slope         p
1     seoir -0.004003  0.008388
7    vanter -0.001712  0.049584
3    métier  0.001241  0.002480
6     gâter  0.002218  0.028621
2   railler  0.002230  0.022109
0     gêner  0.002809  0.002437
8  détester  0.002874  0.016979
5    risque  0.004404  0.043395
4     viser  0.004455  0.019309
9  pâtisser  0.006054  0.028294



seoir
vanter
métier
gâter
railler
gêner
détester
risque
viser
pâtisser

histoire</b>

               token     slope         p
18     métempsychose -0.007343  0.016137
8   particulièrement -0.003846  0.026504
13     physiologique -0.003507  0.039481
23       théologique -0.002930  0.045661
6           zoologie -0.002909  0.034656
22          religion  0.001345  0.038461
2            légende  0.001947  0.013143
10           culture  0.002186  0.011003
5             france  0.002220  0.035565
21        artistique  0.002455  0.043558



métempsychose
particulièrement
physiologique
théologique
zoologie
religion
légende
culture
france
artistique

philosophie</b>

            token     slope         p
28   morcellement -0.004171  0.040000
19    comparaison -0.003513  0.028931
11  physiologique -0.003233  0.036825
21         nature -0.002983  0.024753
2   investigation -0.002964  0.010167
25   souveraineté -0.002846  0.001365
29      tolérance -0.001984  0.017291
22         vérité -0.001719  0.047640
8       rationnel -0.001695  0.023396
5      esthétique  0.001531  0.031395



morcellement
comparaison
physiologique
nature
investigation
souveraineté
tolérance
vérité
rationnel
esthétique

lettre</b>

          token     slope         p
22      épernon -0.007224  0.004996
17     argental -0.005124  0.044331
13      bouhier -0.004859  0.026782
16       denise -0.004003  0.044633
24   compliment -0.002762  0.005779
5   ambassadeur -0.001978  0.006100
1       insérer -0.001806  0.008851
4    confession  0.001169  0.044680
3         texte  0.001204  0.031444
19       notice  0.001274  0.010345



épernon
argental