quanteda / spacyr

R wrapper to spaCy NLP
http://spacyr.quanteda.io
250 stars 38 forks source link

pos-tagging-problem #160

Closed ManfredBernhard closed 5 years ago

ManfredBernhard commented 5 years ago
R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)

R ist freie Software und kommt OHNE JEGLICHE GARANTIE.
Sie sind eingeladen, es unter bestimmten Bedingungen weiter zu verbreiten.
Tippen Sie 'license()' or 'licence()' für Details dazu.

R ist ein Gemeinschaftsprojekt mit vielen Beitragenden.
Tippen Sie 'contributors()' für mehr Information und 'citation()',
um zu erfahren, wie R oder R packages in Publikationen zitiert werden können.

Tippen Sie 'demo()' für einige Demos, 'help()' für on-line Hilfe, oder
'help.start()' für eine HTML Browserschnittstelle zur Hilfe.
Tippen Sie 'q()', um R zu verlassen.

[R.app GUI 1.70 (7612) x86_64-apple-darwin15.6.0]

[Arbeitsbereich wiederhergestellt aus /Users/MBS/.RData]
[Verlauf wiederhergestellt aus /Users/MBS/.Rapp.history]

> S2<-scan(what="c", sep="\n", file=file.choose())
Read 1 item
> S2
[1] "Vor einem großen Walde wohnte ein armer Holzhacker mit seiner Frau und seinen zwei Kindern; das Bübchen hieß Hänsel und das Mädchen Gretel. Er hatte wenig zu beißen und zu brechen, und einmal, als große Teuerung ins Land kam, konnte er das tägliche Brot nicht mehr schaffen."
> library(quanteda)
Package version: 1.4.1
Parallel computing: 2 of 4 threads used.
See https://quanteda.io for tutorials and examples.

Attache Paket: ‘quanteda’

The following object is masked from ‘package:utils’:

    View

> library(spacyr)
> spacy_initialize(model="de")
Found 'spacy_condaenv'. spacyr will use this environment
successfully initialized (spaCy Version: 2.0.18, language model: de)
(python options: type = "condaenv", value = "spacy_condaenv")
> S2.parsed<-spacy_parse(S2, tag=TRUE, pos=TRUE)
Warnmeldung:
In spacy_parse.character(S2, tag = TRUE, pos = TRUE) :
  lemmatization may not work properly in model 'de'
> S2.parsed
   doc_id sentence_id token_id      token      lemma   pos     tag entity
1   text1           1        1        Vor        Vor   ADP    APPR       
2   text1           1        2      einem      einer   DET     ART       
3   text1           1        3     großen       groß   ADJ    ADJA       
4   text1           1        4      Walde       Wald  NOUN      NN       
5   text1           1        5     wohnte     wohnen  VERB   VVFIN       
6   text1           1        6        ein      einen   DET     ART       
7   text1           1        7      armer        arm   ADJ    ADJA       
8   text1           1        8 Holzhacker Holzhacker  NOUN      NN       
9   text1           1        9        mit        mit   ADP    APPR       
10  text1           1       10     seiner       sich   DET  PPOSAT       
11  text1           1       11       Frau       Frau  NOUN      NN       
12  text1           1       12        und        und  CONJ     KON       
13  text1           1       13     seinen     seinen   DET  PPOSAT       
14  text1           1       14       zwei       zwei   NUM    CARD       
15  text1           1       15    Kindern       Kind  NOUN      NN       
16  text1           1       16          ;          ; PUNCT      $.       
17  text1           2        1        das        der   DET     ART       
18  text1           2        2    Bübchen    Bübchen  NOUN      NN       
19  text1           2        3       hieß     heißen  VERB   VVFIN       
20  text1           2        4     Hänsel     Hänsel PROPN      NE  PER_B
21  text1           2        5        und        und  CONJ     KON       
22  text1           2        6        das        der   DET     ART       
23  text1           2        7    Mädchen    Mädchen  NOUN      NN  LOC_B
24  text1           2        8     Gretel     Gretel  NOUN      NN  LOC_I
25  text1           2        9          .          . PUNCT      $.       
26  text1           3        1         Er        ich  PRON    PPER       
27  text1           3        2      hatte      haben   AUX   VAFIN       
28  text1           3        3      wenig      wenig  PRON     PIS       
29  text1           3        4         zu         zu  PART   PTKZU       
30  text1           3        5     beißen     beißen  VERB   VVINF       
31  text1           3        6        und        und  CONJ     KON       
32  text1           3        7         zu         zu  PART   PTKZU       
33  text1           3        8    brechen    brechen  VERB   VVINF       
34  text1           3        9          ,          , PUNCT      $,       
35  text1           3       10        und        und  CONJ     KON       
36  text1           3       11     einmal     einmal   ADV     ADV       
37  text1           3       12          ,          , PUNCT      $,       
38  text1           3       13        als        als   ADP    APPR       
39  text1           3       14      große       groß   ADJ    ADJA       
40  text1           3       15   Teuerung   Teuerung  NOUN      NN       
41  text1           3       16        ins        ins   ADP APPRART       
42  text1           3       17       Land       Land  NOUN      NN       
43  text1           3       18        kam     kommen  VERB   VVFIN       
44  text1           3       19          ,          , PUNCT      $,       
45  text1           3       20     konnte     können  VERB   VMFIN       
46  text1           3       21         er        ich  PRON    PPER       
47  text1           3       22        das        der   DET     ART       
48  text1           3       23   tägliche   tägliche   ADJ    ADJA       
49  text1           3       24       Brot       Brot  NOUN      NN       
50  text1           3       25      nicht      nicht  PART  PTKNEG       
51  text1           3       26       mehr       mehr   ADV     ADV       
52  text1           3       27   schaffen   schaffen  VERB   VVINF       
53  text1           3       28          .          . PUNCT      $.       
> #### see "Mädel" und "Gretel": both are "NOUN" in the analysis, though "Gretel", just as well as "Hänsel" should be  "PROPN".
> S2.parsed.noun<-with(S2.parsed, subset(token, pos=="NOUN"))
> S2.parsed.noun
 [1] "Walde"      "Holzhacker" "Frau"       "Kindern"    "Bübchen"    "Mädchen"    "Gretel"     "Teuerung"   "Land"       "Brot"      
> S2.parsed.proper.noun<-with(S2.parsed, subset(token, pos=="PROPN"))
> S2.parsed.proper.noun
[1] "Hänsel"

Question: What is the most efficient and economic way to correct this problem either inside or outside R/spacyr. It is clear to me that taggers are not foolproof and that we therefore need a good strategy to correct the mistakes. Also: Lemmatization has several mistakes, e.g. token: das Mädchen, and lemma "der Mädchen" (=genitive and dative plural). Best, ManfredBernhard

kbenoit commented 5 years ago

This is a limitation in the underlying spaCy language model for German (see https://spacy.io/models/de). While spacyr can identify the proper names, it wrongly tags Mädchen Gretel as a location. The model also warns that lemmatization may not work correctly, although it's also possible that it will not lemmatize named entities.

So: I am afraid that this is not something we would fix in spacyr, although you could contact the authors of spaCy. The spaCy team is based in Berlin and @ines is German, so they are likely to understand these issues with the German language model very well.

library("spacyr")

txt <- "Vor einem großen Walde wohnte ein armer Holzhacker mit seiner Frau und seinen zwei Kindern; 
        das Bübchen hieß Hänsel und das Mädchen Gretel. Er hatte wenig zu beißen und zu brechen, 
        und einmal, als große Teuerung ins Land kam, konnte er das tägliche Brot nicht mehr schaffen."

spacy_initialize(model = "de")
## Found 'spacy_condaenv'. spacyr will use this environment
## successfully initialized (spaCy Version: 2.0.18, language model: de)
## (python options: type = "condaenv", value = "spacy_condaenv")
sp <- spacy_parse(txt, tag = TRUE, pos = TRUE)
## Warning in spacy_parse.character(txt, tag = TRUE, pos = TRUE):
## lemmatization may not work properly in model 'de'
entity_consolidate(sp)
##    doc_id sentence_id token_id          token          lemma    pos
## 1   text1           1        1            Vor            Vor    ADP
## 2   text1           1        2          einem          einer    DET
## 3   text1           1        3         großen           groß    ADJ
## 4   text1           1        4          Walde           Wald   NOUN
## 5   text1           1        5         wohnte         wohnen   VERB
## 6   text1           1        6            ein          einen    DET
## 7   text1           1        7          armer            arm    ADJ
## 8   text1           1        8     Holzhacker     Holzhacker   NOUN
## 9   text1           1        9            mit            mit    ADP
## 10  text1           1       10         seiner           sich    DET
## 11  text1           1       11           Frau           Frau   NOUN
## 12  text1           1       12            und            und   CONJ
## 13  text1           1       13         seinen         seinen    DET
## 14  text1           1       14           zwei           zwei    NUM
## 15  text1           1       15        Kindern           Kind   NOUN
## 16  text1           1       16              ;              ;  PUNCT
## 17  text1           1       17     \n             \n          SPACE
## 18  text1           2        1            das            der    DET
## 19  text1           2        2        Bübchen        Bübchen   NOUN
## 20  text1           2        3           hieß         heißen   VERB
## 21  text1           2        4         Hänsel         Hänsel ENTITY
## 22  text1           2        5            und            und   CONJ
## 23  text1           2        6            das            der    DET
## 24  text1           2        7 Mädchen_Gretel Mädchen_Gretel ENTITY
## 25  text1           2        8              .              .  PUNCT
## 26  text1           3        1             Er            ich   PRON
## 27  text1           3        2          hatte          haben    AUX
## 28  text1           3        3          wenig          wenig   PRON
## 29  text1           3        4             zu             zu   PART
## 30  text1           3        5         beißen         beißen   VERB
## 31  text1           3        6            und            und   CONJ
## 32  text1           3        7             zu             zu   PART
## 33  text1           3        8        brechen        brechen   VERB
## 34  text1           3        9              ,              ,  PUNCT
## 35  text1           3       10     \n             \n          SPACE
## 36  text1           3       11            und            und   CONJ
## 37  text1           3       12         einmal         einmal    ADV
## 38  text1           3       13              ,              ,  PUNCT
## 39  text1           3       14            als            als    ADP
## 40  text1           3       15          große           groß    ADJ
## 41  text1           3       16       Teuerung       Teuerung   NOUN
## 42  text1           3       17            ins            ins    ADP
## 43  text1           3       18           Land           Land   NOUN
## 44  text1           3       19            kam         kommen   VERB
## 45  text1           3       20              ,              ,  PUNCT
## 46  text1           3       21         konnte         können   VERB
## 47  text1           3       22             er            ich   PRON
## 48  text1           3       23            das            der    DET
## 49  text1           3       24       tägliche       tägliche    ADJ
## 50  text1           3       25           Brot           Brot   NOUN
## 51  text1           3       26          nicht          nicht   PART
## 52  text1           3       27           mehr           mehr    ADV
## 53  text1           3       28       schaffen       schaffen   VERB
## 54  text1           3       29              .              .  PUNCT
##        tag entity_type
## 1     APPR            
## 2      ART            
## 3     ADJA            
## 4       NN            
## 5    VVFIN            
## 6      ART            
## 7     ADJA            
## 8       NN            
## 9     APPR            
## 10  PPOSAT            
## 11      NN            
## 12     KON            
## 13  PPOSAT            
## 14    CARD            
## 15      NN            
## 16      $.            
## 17     _SP            
## 18     ART            
## 19      NN            
## 20   VVFIN            
## 21  ENTITY         PER
## 22     KON            
## 23     ART            
## 24  ENTITY         LOC
## 25      $.            
## 26    PPER            
## 27   VAFIN            
## 28     PIS            
## 29   PTKZU            
## 30   VVINF            
## 31     KON            
## 32   PTKZU            
## 33   VVINF            
## 34      $,            
## 35     _SP            
## 36     KON            
## 37     ADV            
## 38      $,            
## 39    APPR            
## 40    ADJA            
## 41      NN            
## 42 APPRART            
## 43      NN            
## 44   VVFIN            
## 45      $,            
## 46   VMFIN            
## 47    PPER            
## 48     ART            
## 49    ADJA            
## 50      NN            
## 51  PTKNEG            
## 52     ADV            
## 53   VVINF            
## 54      $.
ManfredBernhard commented 5 years ago

Dear Ken, thank you very much for the comment and the explanation of the problem. As suggested, I'll contact the providers of the spacyr-package. Best, ManfredBernhard