100 language processing knock-56: co-reference analysis

Language processing 100 knocks 2015 "Chapter 6: Processing English texts" It is a record of 56th "co-reference analysis" of .tohoku.ac.jp/nlp100/#ch6). Up until the last time in Chapter 6, the level of preparatory exercise was simple, but it's about to become something that makes you think.

Reference link

Link Remarks
056.Co-reference analysis.ipynb Answer program GitHub link
100 amateur language processing knocks:56 Copy and paste source of many source parts
Stanford Core NLP Official Stanford Core NLP page to look at first

environment

type version Contents
OS Ubuntu18.04.01 LTS It is running virtually
pyenv 1.2.16 I use pyenv because I sometimes use multiple Python environments
Python 3.8.1 python3 on pyenv.8.I'm using 1
Packages are managed using venv
Stanford CoreNLP 3.9.2 I installed it a year ago and I don't remember in detail ...
It was the latest even after a year, so I used it as it was
openJDK 1.8.0_242 I used the JDK that was installed for other purposes as it is

Chapter 6: Processing English Text

content of study

An overview of various basic technologies of natural language processing through English text processing using Stanford Core NLP.

Stanford Core NLP, Stemming, Part-of-speech tagging, Named entity recognition, Co-reference analysis, Parsing analysis, Phrase structure analysis, S-expressions

Knock content

For the English text (nlp.txt), execute the following processing.

56. Co-reference analysis

Based on the results of co-reference analysis of Stanford Core NLP, replace the reference expression (mention) in the sentence with the representative reference expression (representative mention). However, when replacing, be careful so that the original reference expression can be understood, such as "representative reference expression (reference expression)".

Problem supplement (about "common reference")

Coreference seems to refer to the same noun phrase. The image on Stanford CoreNLP Official is easy to understand. image.png The mechanism of Stanford Core NLP is detailed in Deterministic Coreference Resolution System.

Answer

Answer program [056. Co-reference analysis.ipynb](https://github.com/YoheiFukuhara/nlp100/blob/master/06.%E8%8B%B1%E8%AA%9E%E3%83%86%E3 % 82% AD% E3% 82% B9% E3% 83% 88% E3% 81% AE% E5% 87% A6% E7% 90% 86 / 056.% E5% 85% B1% E5% 8F% 82% E7% 85% A7% E8% A7% A3% E6% 9E% 90.ipynb)

import xml.etree.ElementTree as ET

#Parse xml of analysis result
root = ET.parse('./nlp.txt.xml')

#Create a dictionary of location information that enumerates coreferences and replaces them with representative reference expressions
#The dictionary is{(sentence id,Start token id), (End token id,Representative reference expression)}...
replaces = {}
for coreference in root.iterfind('./document/coreference/coreference'):

    #Acquisition of representative reference expression
    representative = coreference.findtext('./mention[@representative="true"]/text')

    #Mention enumeration other than representative reference expressions, added to dictionary
    for mention in coreference.iterfind('./mention'):
        if mention.get('representative') == None:
            sentence_id = mention.findtext('sentence')
            start = mention.findtext('start')
            end = int(mention.findtext('end')) - 1 #end stagger
            
            #Update without worrying even if it is already in the dictionary(Win after)
            replaces[(sentence_id, start)] = (end, representative)

#Display while replacing the text with replaces
for sentence in root.iterfind('./document/sentences/sentence'):
    sentence_id = sentence.get('id')

    for token in sentence.iterfind('./tokens/token'):
        token_id = token.get('id')

        #Replacement start
        if (sentence_id, token_id) in replaces:

            #Extract the end position and representative reference expression from the dictionary
            (end, representative) = replaces[(sentence_id, token_id)]

            #Representative reference expression + insert parentheses(end=''With no line breaks)
            print('「' + representative + '」 (', end='')

        #token output(end=''With no line breaks)
        print(token.findtext('word'), end='')

        #Insert closing parentheses at the end of the replacement(end=''With no line breaks)
        if int(token_id) == end:
            print(')', end='')
            end = 0
            
        #End of sentence(period)I don't care about the space being added before(end=''With no line breaks)
        print(' ', end='')

    print()     #Line breaks in sentence units

Answer commentary

XML file path

The following XML file path and co-reference information mapping. It's not a mistake that the same coordinates are in the 3rd and 4th layers (although the 3rd layer is likely to be coreferences ...). If the attribute representative is true in the mental tag of the 5th layer, it is a "representative reference expression". Others are referrers.

output 1st level Second level Third level 4th level 5th level 6th level
Co-reference sentence id root document coreference coreference mention sentence
Start of co-reference token id root document coreference coreference mention start
End of co-reference token id root document coreference coreference mention end
Co-reference text root document coreference coreference mention text

The XML file is [GitHub](https://github.com/YoheiFukuhara/nlp100/blob/master/06.%E8%8B%B1%E8%AA%9E%E3%83%86%E3%82%AD% It is located at E3% 82% B9% E3% 83% 88% E3% 81% AE% E5% 87% A6% E7% 90% 86 / nlp.txt.xml).

xml:nlp.txt.xml(Excerpt)


<root>
  <document>
    <docId>nlp.txt</docId>
    <sentences>

--Omission--

    <coreference>
      <coreference>
        <mention representative="true">
          <sentence>1</sentence>
          <start>7</start>
          <end>16</end>
          <head>12</head>
          <text>the free encyclopedia Natural language processing -LRB- NLP -RRB-</text>
        </mention>
        <mention>
          <sentence>1</sentence>
          <start>17</start>
          <end>22</end>
          <head>18</head>
          <text>a field of computer science</text>
        </mention>

Creating a reference expression dictionary

We have created a dictionary variable named replaces to store the data. {(sentence id, start token id), (end token id, representative reference expression)} How to enter data. There may be duplicate keys in the dictionary, but we are using the second-win method (the record that comes later overwrites the record that was earlier).

python


#Create a dictionary of location information that enumerates coreferences and replaces them with representative reference expressions
#The dictionary is{(sentence id,Start token id), (End token id,Representative reference expression)}...
replaces = {}
for coreference in root.iterfind('./document/coreference/coreference'):

    #Acquisition of representative reference expression
    representative = coreference.findtext('./mention[@representative="true"]/text')

    #Mention enumeration other than representative reference expressions, added to dictionary
    for mention in coreference.iterfind('./mention'):
        if mention.get('representative') == None:
            sentence_id = mention.findtext('sentence')
            start = mention.findtext('start')
            end = int(mention.findtext('end')) - 1 #end stagger
            
            #Update without worrying even if it is already in the dictionary(Win after)
            replaces[(sentence_id, start)] = (end, representative)

Statement output

After that, while reading the text of the sentences part, if there is a reference expression, I replace it and add parentheses.

python


#Display while replacing the text with replaces
for sentence in root.iterfind('./document/sentences/sentence'):
    sentence_id = sentence.get('id')

    for token in sentence.iterfind('./tokens/token'):
        token_id = token.get('id')

        #Replacement start
        if (sentence_id, token_id) in replaces:

            #Extract the end position and representative reference expression from the dictionary
            (end, representative) = replaces[(sentence_id, token_id)]

            #Representative reference expression + insert parentheses(end=''With no line breaks)
            print('「' + representative + '」 (', end='')

        #token output(end=''With no line breaks)
        print(token.findtext('word'), end='')

        #Insert closing parentheses at the end of the replacement(end=''With no line breaks)
        if int(token_id) == end:
            print(')', end='')
            end = 0
            
        #End of sentence(period)I don't care about the space being added before(end=''With no line breaks)
        print(' ', end='')

    print()     #Line breaks in sentence units

Output result (execution result)

When the program is executed, the following results will be output.

Output result


Natural language processing From Wikipedia , the free encyclopedia Natural language processing -LRB- NLP -RRB- is a field of computer science) , artificial intelligence , and linguistics concerned with the interactions between computers and human -LRB- natural -RRB- languages . 
As such , 「NLP」 (NLP) is related to the area of humani-computer interaction . 
Many challenges in 「NLP」 (NLP) involve natural language understanding , that is , enabling computers to derive meaning from human or natural language input , and others involve natural language generation . 
History The history of 「NLP」 (NLP) generally starts in the 1950s , although work can be found from earlier periods . 
In 1950 , Alan Turing published an article titled `` Computing Machinery and Intelligence '' which proposed what is now called the Turing test as a criterion of intelligence . 
The Georgetown experiment in 1954 involved fully automatic translation of more than sixty Russian sentences into English . 
The authors claimed that within three or five years , machine translation would be a solved problem . 
However , real progress was much slower , and after the ALPAC report in 1966 , which found that ten year long research had failed to fulfill the expectations , funding for 「machine translation」 (machine translation) was dramatically reduced . 
Little further research in 「machine translation」 (machine translation) was conducted until the late 1980s , when the first statistical machine translation systems were developed . 
Some notably successful NLP systems developed in the 1960s were SHRDLU , a natural language system working in restricted `` blocks worlds '' with restricted vocabularies , and ELIZA , a simulation of a Rogerian psychotherapist , written by Joseph Weizenbaum between 1964 to 「1966」 (1966) . 
Using almost no information about human thought or emotion , ELIZA sometimes provided a startlingly human-like interaction . 
When the `` patient '' exceeded the very small knowledge base , 「ELIZA」 (ELIZA) might provide a generic response , for example , responding to `` My head hurts '' with `` Why do you say 「My head」 (your head) hurts ? '' 
. 
During the 1970s many programmers began to write ` conceptual ontologies ' , which structured real-world information into computer-understandable data . 
Examples are MARGIE -LRB- Schank , 1975 -RRB- , SAM -LRB- Cullingford , 1978 -RRB- , PAM -LRB- Wilensky , 「1978」 (1978) -RRB- , TaleSpin -LRB- Meehan , 1976 -RRB- , QUALM -LRB- Lehnert , 1977 -RRB- , Politics -LRB- Carbonell , 1979 -RRB- , and Plot Units -LRB- Lehnert 1981 -RRB- . 
During this time , many chatterbots were written including PARRY , Racter , and Jabberwacky . 
Up to the 1980s , most 「NLP」 (NLP) systems were based on complex sets of hand-written rules . 
Starting in the late 1980s , however , there was a revolution in 「NLP」 (NLP) with the introduction of machine learning algorithms for language processing . 
This was due to both the steady increase in computational power resulting from Moore 's Law and the gradual lessening of the dominance of Chomskyan theories of linguistics -LRB- e.g. transformational grammar -RRB- , whose theoretical underpinnings discouraged the sort of corpus linguistics that underlies the machine-learning approach to language processing . 
Some of the earliest-used machine learning algorithms , such as decision trees , produced systems of hard if-then rules similar to existing hand-written rules . 
However , Part of speech tagging introduced the use of Hidden Markov Models to NLP , and increasingly , research has focused on statistical models , which make soft , probabilistic decisions based on attaching real-valued weights to the features making up the input data . 
The cache language models upon which many speech recognition systems now rely are examples of such statistical models . 
Such models are generally more robust when given unfamiliar input , especially input that contains errors -LRB- as is very common for real-world data -RRB- , and produce more reliable results when integrated into a larger system comprising multiple subtasks . 
Many of the notable early successes occurred in the field of machine translation , due especially to work at IBM Research , where successively more complicated statistical models were developed . 
「many speech recognition systems」 (These systems) were able to take advantage of existing multilingual textual corpora that had been produced by the Parliament of Canada and the European Union as a result of laws calling for the translation of all governmental proceedings into all official languages of the corresponding systems of government . 
However , most other systems depended on corpora specifically developed for the tasks implemented by these systems , which was -LRB- and often continues to be -RRB- a major limitation in the success of 「many speech recognition systems」 (these systems) . 
As a result , a great deal of research has gone into methods of more effectively learning from limited amounts of data . 
Recent research has increasingly focused on unsupervised and semi-supervised learning algorithms . 
Such algorithms are able to learn from data that has not been hand-annotated with the desired answers , or using a combination of annotated and non-annotated data . 
Generally , this task is much more difficult than supervised learning , and typically produces less accurate results for a given amount of input data . 
However , there is an enormous amount of non-annotated data available -LRB- including , among other things , the entire content of the World Wide Web -RRB- , which can often make up for the inferior results . 
NLP using machine learning Modern NLP algorithms are based on machine learning , especially statistical machine learning . 
「The machine-learning paradigm」 (The paradigm of machine learning) is different from that of most prior attempts at language processing . 
Prior implementations of language-processing tasks typically involved the direct hand coding of large sets of rules . 
The machine-learning paradigm calls instead for using general learning algorithms - often , although not always , grounded in statistical inference - to automatically learn such rules through the analysis of large corpora of typical real-world examples . 
A corpus -LRB- plural , `` corpora '' -RRB- is a set of documents -LRB- or sometimes , individual sentences -RRB- that have been hand-annotated with the correct values to be learned . 
Many different classes of machine learning algorithms have been applied to 「NLP」 (NLP) tasks . 
「machine learning algorithms」 (These algorithms) take as input a large set of `` features '' that are generated from 「the input data」 (the input data) . 
Some of 「machine learning algorithms」 (the earliest-used algorithms) , such as decision trees , produced systems of hard if-then rules similar to the systems of hand-written rules that were then common . 
Increasingly , however , research has focused on statistical models , which make soft , probabilistic decisions based on attaching real-valued weights to each input feature . 
Such models have the advantage that 「Such models」 (they) can express the relative certainty of many different possible answers rather than only one , producing more reliable results when such a model is included as a component of a larger system . 
Systems based on machine-learning algorithms have many advantages over hand-produced rules : The learning procedures used during machine learning automatically focus on the most common cases , whereas when writing rules by hand it is often not obvious at all where the effort should be directed . 
Automatic learning procedures can make use of statistical inference algorithms to produce models that are robust to unfamiliar input -LRB- e.g. containing words or structures that have not been seen before -RRB- and to erroneous input -LRB- e.g. with misspelled words or words accidentally omitted -RRB- . 
Generally , handling such input gracefully with hand-written rules -- or more generally , creating systems of hand-written rules that make soft decisions -- extremely difficult , error-prone and time-consuming . 
Systems based on automatically learning 「hand-written rules --」 (the rules) can be made more accurate simply by supplying more input data . 
However , systems based on hand-written rules can only be made more accurate by increasing the complexity of the rules , which is a much more difficult task . 
In particular , there is a limit to the complexity of systems based on hand-crafted rules , beyond which 「many speech recognition systems」 (the systems) become more and more unmanageable . 
However , creating more data to input to machine-learning systems simply requires a corresponding increase in the number of man-hours worked , generally without significant increases in the complexity of the annotation process . 
The subfield of 「NLP」 (NLP) devoted to learning approaches is known as Natural Language Learning -LRB- NLL -RRB- and 「NLP」 (its) conference CoNLL and peak body SIGNLL are sponsored by ACL , recognizing also their links with Computational Linguistics and Language Acquisition . 
When the aims of computational language learning research is to understand more about human language acquisition , or psycholinguistics , 「NLL」 (NLL) overlaps into the related field of Computational Psycholinguistics . 

Recommended Posts

100 language processing knock-56: co-reference analysis
100 Language Processing Knock-57: Dependency Analysis
100 Language Processing Knock (2020): 38
100 language processing knock 00 ~ 02
100 Language Processing Knock 2015 Chapter 5 Dependency Analysis (40-49)
100 Language Processing Knock 2020 Chapter 4: Morphological Analysis
100 Language Processing Knock Chapter 4: Morphological Analysis
100 Language Processing Knock 2020 Chapter 5: Dependency Analysis
100 Language Processing Knock-59: Analysis of S-expressions
100 Language Processing Knock 2015 Chapter 4 Morphological Analysis (30-39)
100 language processing knock 2020 [00 ~ 39 answer]
100 language processing knock 2020 [00-79 answer]
100 language processing knock 2020 [00 ~ 69 answer]
100 Language Processing Knock 2020 Chapter 1
100 Amateur Language Processing Knock: 17
100 language processing knock 2020 [00 ~ 49 answer]
100 Language Processing Knock-52: Stemming
100 Language Processing Knock Chapter 1
100 Amateur Language Processing Knock: 07
100 Language Processing Knock 2020 Chapter 2
100 Amateur Language Processing Knock: 47
100 Language Processing Knock-53: Tokenization
100 Amateur Language Processing Knock: 97
100 language processing knock 2020 [00 ~ 59 answer]
100 Amateur Language Processing Knock: 67
100 Language Processing with Python Knock 2015
100 Language Processing Knock-51: Word Clipping
100 Language Processing Knock-58: Tuple Extraction
100 language processing knock-50: sentence break
100 Language Processing Knock Chapter 2 (Python)
Natural language processing 1 Morphological analysis
100 Language Processing Knock-25: Template Extraction
100 Language Processing Knock-87: Word Similarity
Solving 100 Language Processing Knock 2020 (01. "Patatokukashi")
100 Amateur Language Processing Knock: Summary
100 language processing knock-30 (using pandas): reading morphological analysis results
100 Language Processing Knock 2020 Chapter 2: UNIX Commands
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock Chapter 1 in Python
100 language processing knocks 2020: Chapter 4 (morphological analysis)
100 Language Processing Knock 2020 Chapter 9: RNN, CNN
[Language processing 100 knocks 2020] Chapter 5: Dependency analysis
100 language processing knock-76 (using scikit-learn): labeling
100 language processing knock-55: named entity extraction
I tried 100 language processing knock 2020: Chapter 3
100 Language Processing Knock-82 (Context Word): Context Extraction
100 Language Processing Knock with Python (Chapter 3)
100 Language Processing Knock: Chapter 1 Preparatory Movement
100 Language Processing Knock 2020 Chapter 6: Machine Learning
Language processing 100 knock-86: Word vector display
100 Language Processing Knock 2020 Chapter 10: Machine Translation (90-98)
100 Language Processing Knock-28: MediaWiki Markup Removal
100 Language Processing Knock 2020 Chapter 7: Word Vector
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
100 Language Processing Knock-31 (using pandas): Verb
I tried 100 language processing knock 2020: Chapter 1
100 Language Processing Knock 2020 Chapter 1: Preparatory Movement
100 language processing knock-73 (using scikit-learn): learning
100 Language Processing Knock Chapter 1 by Python
100 Language Processing Knock 2020 Chapter 3: Regular Expressions
[Language processing 100 knocks 2020] Chapter 4: Morphological analysis