This article was published as a part of the Data Science Blogathon
Biographies of many famous personalities are very insightful and inspiring. Although, one may not want to read the whole document. In order to just get the important points from the biography, one can generate a summary of the biography. The summary is generated by giving weights to all the words. Sometimes, anaphoras can be predicted by the machine as a separate word which in return produces a less accurate result. For example, Jai is playing tennis. He plays very well. Here, Jai is taken as one word and he as another in the machine by semantic analysis when both he and Jai are referring to the same person.
Another scenario could be that one is reading a biography and they leave it in between. When they revisit the document, they do not remember who he refers to. Thus, they have no choice but to read the above lines again to understand the references. Anaphora resolution saves time for individuals and is also used in pre-processing these biographies.
source: https://meta-guide.com/wp-content/uploads/2020/06/storygenerator19.jpg
The reason for limiting the document input to biographies is that biographies focus on named entities, i.e real-world objects. Biographies consist of the name of the person whose biography it is, where he/she was born or where he/she moved to and where did he/she study or work. These refer to named entities name, location, and organization respectively.
If we talk about essays or text documents in general, there may or may not exist named entities in them. For example, there could be a story about a boy where the boy’s name is never disclosed. Anaphora resolution, in this case, requires the building of a corpus to identify the type of noun and then referring the pronoun attached to it. Using named entity recognition requires no pre-built corpus as whenever a named entity is identified, it is stored in the local storage and along with its subcategory, and when the pronoun of that category is read, it is automatically assigned to the named entity recognized.
For example, ‘Jai is playing tennis. He plays very well. Here Jai is identified as a named entity, it is stored in local storage as ‘Jai, Person’ where the person is the type of entity. When the next line is read, he is identified as a pronoun that is assigned to a person. After checking the local storage, he will be assigned to Jai.
source: https://www.adl.org/sites/default/files/styles/open_graph_image_1200_x_628_/public/2021-01/pronouns-in-english-language-istock-modified.jpg?itok=_3GxxCPw
There are many types of named entities but the ones used in biographies are person names, organization names, and location names. All these three can be identified using Stanford NER which can be downloaded from the link given below:
The python code for anaphora resolution and co-reference resolution of biographies is mentioned below. It can be used directly after changing the path of the Stanford NER file downloaded if required.
from nltk.tokenize import word_tokenize, sent_tokenize | |
from nltk.tag import StanfordNERTagger | |
st = StanfordNERTagger('/Users/Downloads/stanford-ner-2020-11-17/classifiers/english.all.3class.distsim.crf.ser.gz', | |
'/Users/Downloads/stanford-ner-2020-11-17/stanford-ner.jar', | |
encoding='utf-8') | |
text_string=''' | |
Sir Jagadish Chandra Bose was a scientist of many talents. He was born on 30 November, 1858 in Bikrampur, West Bengal. He was a polymath, physicist, biologist, botanist and archaeologist. Bose pioneered the study of radio and microwave optics. He joined the Presidency College of the University of Calcutta as a professor of physics. There, despite racial discrimination and a lack of funding and equipment, Bose carried on his scientific research. He made important contributions to the study of plants. He has laid the foundation of experimental science in the Indian sub-continent. He was the first person to use semiconductor junctions to detect radio signals. What’s more, he is also probably the father of open technology, as he made his inventions and work freely available for others to further develop and his reluctance for patenting his work is legendary. | |
Another of his well known inventions is the crescograph. He measured plant response to various stimuli and hypothesized that plants can feel pain, understand affection etc. At the time when Bose was a student at Cambridge, Prafulla Chandra Roy was a student at Edinburgh. They met in London and became intimate friends. | |
While most of us are aware of his scientific prowess, we might not be aware of his talent as an early writer of science fiction! He is in fact considered the father of Bengali science fiction. | |
''' | |
tokenized_text = sent_tokenize(text_string) | |
tokenized_tags_list=[] | |
new_text_string='' | |
anaphora_p_sing=['he','she','his','her','He','She','His','Her'] | |
anaphora_p_plural=['they','their','They','Their'] | |
anaphora_l=['this place','that place','the place','there,','there.','This place','That place','The place','There'] | |
person_list_last=[] | |
person_list_sentence=[] | |
org_list_last=[] | |
org_list_sentence=[] | |
loc_list_last=[] | |
loc_list_sentence=[] | |
coreference_list=[] | |
coreference_sol=[] | |
flag=0 | |
flag_org=0 | |
flag_loc=0 | |
for i in range(0,len(tokenized_text)): | |
print(tokenized_text[i]) | |
tokenized_text1 = word_tokenize(tokenized_text[i]) | |
classified_text = st.tag(tokenized_text1) | |
for j in range(0,len(tokenized_text1)): | |
a=tokenized_text1[j] | |
b=classified_text[j][1] | |
if b=='PERSON': | |
if flag==0: | |
if classified_text[j][0] in coreference_list: | |
for k in range(0,len(coreference_list)): | |
if coreference_list[k]==classified_text[j][0]: | |
if coreference_sol[k] in person_list_sentence: | |
pass | |
else: | |
person_list_sentence.append(coreference_sol[k]) | |
break | |
else: | |
try: | |
if classified_text[j+1][1]=='PERSON': | |
flag=1 | |
full_name=classified_text[j][0]+' ' | |
else: | |
if classified_text[j][0] in person_list_sentence: | |
pass | |
else: | |
person_list_sentence.append(classified_text[j][0]) | |
except: | |
if classified_text[j][0] in person_list_sentence: | |
pass | |
else: | |
person_list_sentence.append(classified_text[j][0]) | |
else: | |
full_name+=classified_text[j][0]+' ' | |
try: | |
if classified_text[j+1][1]=='PERSON': | |
flag=1 | |
else: | |
flag=0 | |
full_name=full_name[0:len(full_name)-1] | |
if full_name in person_list_sentence: | |
pass | |
else: | |
person_list_sentence.append(full_name) | |
if full_name not in coreference_sol: | |
individual=word_tokenize(full_name) | |
for k in individual: | |
coreference_list.append(k) | |
coreference_sol.append(full_name) | |
except: | |
flag=0 | |
full_name=full_name[0:len(full_name)-1] | |
person_list_sentence.append(full_name) | |
individual=word_tokenize(full_name) | |
for k in individual: | |
coreference_list.append(k) | |
coreference_sol.append(full_name) | |
elif b=='ORGANIZATION': | |
if flag_org==0: | |
if classified_text[j][0] in coreference_list: | |
for k in range(0,len(coreference_list)): | |
if coreference_list[k]==classified_text[j][0]: | |
if coreference_sol[k] in org_list_sentence: | |
pass | |
else: | |
org_list_sentence.append(coreference_sol[k]) | |
break | |
try: | |
if classified_text[j+1][1]=='ORGANIZATION': | |
flag_org=1 | |
full_name=classified_text[j][0]+' ' | |
else: | |
if classified_text[j][0] in org_list_sentence: | |
pass | |
else: | |
org_list_sentence.append(classified_text[j][0]) | |
except: | |
if classified_text[j][0] in org_list_sentence: | |
pass | |
else: | |
org_list_sentence.append(classified_text[j][0]) | |
else: | |
full_name+=classified_text[j][0]+' ' | |
try: | |
if classified_text[j+1][1]=='ORGANIZATION': | |
flag_org=1 | |
else: | |
flag_org=0 | |
full_name=full_name[0:len(full_name)-1] | |
if full_name in org_list_sentence: | |
pass | |
else: | |
org_list_sentence.append(full_name) | |
if full_name not in coreference_sol: | |
individual=word_tokenize(full_name) | |
for k in individual: | |
coreference_list.append(k) | |
coreference_sol.append(full_name) | |
except: | |
flag_org=0 | |
full_name=full_name[0:len(full_name)-1] | |
org_list_sentence.append(full_name) | |
individual=word_tokenize(full_name) | |
for k in individual: | |
coreference_list.append(k) | |
coreference_sol.append(full_name) | |
elif b=='LOCATION': | |
if flag_loc==0: | |
if classified_text[j][0] in coreference_list: | |
for k in range(0,len(coreference_list)): | |
if coreference_list[k]==classified_text[j][0]: | |
if coreference_sol[k] in loc_list_sentence: | |
pass | |
else: | |
loc_list_sentence.append(coreference_sol[k]) | |
break | |
try: | |
if classified_text[j+1][1]=='LOCATION': | |
flag_loc=1 | |
full_name=classified_text[j][0]+' ' | |
else: | |
if classified_text[j][0] in loc_list_sentence: | |
pass | |
else: | |
loc_list_sentence.append(classified_text[j][0]) | |
except: | |
if classified_text[j][0] in org_list_sentence: | |
pass | |
else: | |
loc_list_sentence.append(classified_text[j][0]) | |
else: | |
full_name+=classified_text[j][0]+' ' | |
try: | |
if classified_text[j+1][1]=='LOCATION': | |
flag_loc=1 | |
else: | |
flag_loc=0 | |
full_name=full_name[0:len(full_name)-1] | |
if full_name in loc_list_sentence: | |
pass | |
else: | |
loc_list_sentence.append(full_name) | |
if full_name not in coreference_sol: | |
individual=word_tokenize(full_name) | |
for k in individual: | |
coreference_list.append(k) | |
coreference_sol.append(full_name) | |
except: | |
flag_loc=0 | |
full_name=full_name[0:len(full_name)-1] | |
loc_list_sentence.append(full_name) | |
individual=word_tokenize(full_name) | |
for k in individual: | |
coreference_list.append(k) | |
coreference_sol.append(full_name) | |
else: | |
pass | |
a=[] | |
for l in anaphora_p_sing: | |
if l in tokenized_text1: | |
if len(person_list_sentence)!=0: | |
if person_list_sentence[len(person_list_sentence)-1] not in a: | |
a.append(person_list_sentence[len(person_list_sentence)-1]) | |
else: | |
try: | |
a.append(person_list_last[0]) | |
if person_list_last[0] not in person_list_sentence: | |
person_list_sentence.append(person_list_last[0]) | |
except: | |
pass | |
for l in anaphora_p_plural: | |
if l in tokenized_text1: | |
if len(person_list_sentence)>1: | |
for m in person_list_sentence: | |
if m not in a: | |
a.append(m) | |
elif len(person_list_sentence)==1: | |
if person_list_sentence[0] not in a: | |
a.append(person_list_sentence[0]) | |
try: | |
for n in person_list_last: | |
if n not in a: | |
a.append(n) | |
person_list_sentence.append(n) | |
except: | |
pass | |
else: | |
try: | |
for n in person_list_last: | |
if n not in a: | |
a.append(n) | |
person_list_sentence.append(n) | |
except: | |
pass | |
for l in anaphora_l: | |
if l in tokenized_text1 and ',' in tokenized_text1: | |
if len(loc_list_sentence)!=0: | |
if loc_list_sentence[len(loc_list_sentence)-1] not in a: | |
a.append(loc_list_sentence[len(loc_list_sentence)-1]) | |
else: | |
try: | |
a.append(loc_list_last[0]) | |
if loc_list_last[0] not in loc_list_sentence: | |
loc_list_sentence.append(loc_list_last[0]) | |
except: | |
pass | |
if len(org_list_sentence)!=0: | |
if org_list_sentence[len(org_list_sentence)-1] not in a: | |
a.append(org_list_sentence[len(org_list_sentence)-1]) | |
else: | |
try: | |
a.append(org_list_last[0]) | |
if org_list_last[0] not in org_list_sentence: | |
org_list_sentence.append(org_list_last[0]) | |
except: | |
pass | |
person_list_last=person_list_sentence | |
person_list_sentence=[] | |
org_list_last=org_list_sentence | |
org_list_sentence=[] | |
loc_list_last=person_list_sentence | |
loc_list_sentence=[] | |
print('Anaphora resolved:') | |
print(a) | |
print('\n') |
The code above creates two lists for each entity (name, organization, location). The lists refer to the named entities mentioned in the previous and current sentences respectively. These lists are maintained because sometimes the anaphora refers to the named entity mentioned in the previous line. For example, Jai is playing and he loved to play. Here, he refers to Jai which is mentioned in the current sentence. But the same sentence can be written as: Jai is playing. He loved to play. Here also, he refers to Jai but Jai is mentioned in the previous sentence.
Two other lists are maintained for a co-reference list and co-reference solutions. Co-reference resolution can be explained with the help of an example. Mary Lee is referred to as Mary sometimes and Lee sometimes. Identifying that Mary and Lee refer to the same person is co-reference resolution. Thus, if the sentence is: Lee loves to read. She is a bookworm. Here, if we run this sentence through Stanford NER tagger, it will store Lee as a person and when the machine reads the pronoun she, it resolves it to Lee.
If the co-reference resolution is included in the code, the machine will resolve she to Mary Lee, which is much better for understanding. Thus the lists stored for this purpose as per our code are [‘Mary’, ‘Lee’] and [‘Mary Lee’, ‘Mary Lee’] which implies that Mary is resolved to Mary Lee and Lee is also resolved to Mary Lee so that the machine is able to interpret that we are talking about the same person.
A hardcoded list for many possible pronouns is created in the code. If an element of the list occurs in the sentence, it is resolved. A special case used in the code is the word ‘There’. There is only resolved if it has a comma after in the sentence. This is to avoid errors as there can have two meanings:
1. There is no doubt that he is a great man.
2. He moved to Delhi. There, he learned music.
The ‘there’ in the second case needs to be resolved it refers to a location. The text string taken from the biography is also hardcoded. It can be changed according to requirements.
The code output is shared below in the form of screenshots. It can be seen that ‘he’, ‘his’ has been resolved to Jagadish Chandra Bose in all sentences. In many places, the name Jagadish Chandra Bose was referred to as Bose in the text string, it has been resolved to its full name while generating the desired output. ‘There has been resolved to an organization. ‘They’ has been resolved to Jagadish Chandra Bose and his friend. An empty list is returned where nothing had to be resolved.
The algorithm used in the code above gives better accuracy with respect to many algorithms used for anaphora resolution. For example, Jai is eating. Mary Joins him. They enjoy dinner. Here, using the Lapin and Lease algorithm, They will be resolved as null when it refers to Jai and Mary. This algorithm has been designed in a way such that it is best suited for biographies as mostly named entities are used and facts are provided.
Author name: Pearl Miglani
Contact info: miglanipearl@gmail.com
The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.
Automated Text Summarization with Sumy Library
Stemming vs Lemmatization in NLP: Must-Know Dif...
Part 3: Step by Step Guide to NLP – Text ...
Topic extraction From Prime Minister Modi’...
Interview Questions on NLP Fundamentals
A Brief Guide on how to build a Named Entity Ex...
Getting started with NLP using NLTK Library
Making Natural Language Processing easy with Te...
Part 10: Step by Step Guide to Master NLP ̵...
Introduction to Automatic Speech Recognition an...
We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.
Show details
This site uses cookies to ensure that you get the best experience possible. To learn more about how we use cookies, please refer to our Privacy Policy & Cookies Policy.
It is needed for personalizing the website.
Expiry: Session
Type: HTTP
This cookie is used to prevent Cross-site request forgery (often abbreviated as CSRF) attacks of the website
Expiry: Session
Type: HTTPS
Preserves the login/logout state of users across the whole site.
Expiry: Session
Type: HTTPS
Preserves users' states across page requests.
Expiry: Session
Type: HTTPS
Google One-Tap login adds this g_state cookie to set the user status on how they interact with the One-Tap modal.
Expiry: 365 days
Type: HTTP
Used by Microsoft Clarity, to store and track visits across websites.
Expiry: 1 Year
Type: HTTP
Used by Microsoft Clarity, Persists the Clarity User ID and preferences, unique to that site, on the browser. This ensures that behavior in subsequent visits to the same site will be attributed to the same user ID.
Expiry: 1 Year
Type: HTTP
Used by Microsoft Clarity, Connects multiple page views by a user into a single Clarity session recording.
Expiry: 1 Day
Type: HTTP
Collects user data is specifically adapted to the user or device. The user can also be followed outside of the loaded website, creating a picture of the visitor's behavior.
Expiry: 2 Years
Type: HTTP
Use to measure the use of the website for internal analytics
Expiry: 1 Years
Type: HTTP
The cookie is set by embedded Microsoft Clarity scripts. The purpose of this cookie is for heatmap and session recording.
Expiry: 1 Year
Type: HTTP
Collected user data is specifically adapted to the user or device. The user can also be followed outside of the loaded website, creating a picture of the visitor's behavior.
Expiry: 2 Months
Type: HTTP
This cookie is installed by Google Analytics. The cookie is used to store information of how visitors use a website and helps in creating an analytics report of how the website is doing. The data collected includes the number of visitors, the source where they have come from, and the pages visited in an anonymous form.
Expiry: 399 Days
Type: HTTP
Used by Google Analytics, to store and count pageviews.
Expiry: 399 Days
Type: HTTP
Used by Google Analytics to collect data on the number of times a user has visited the website as well as dates for the first and most recent visit.
Expiry: 1 Day
Type: HTTP
Used to send data to Google Analytics about the visitor's device and behavior. Tracks the visitor across devices and marketing channels.
Expiry: Session
Type: PIXEL
cookies ensure that requests within a browsing session are made by the user, and not by other sites.
Expiry: 6 Months
Type: HTTP
use the cookie when customers want to make a referral from their gmail contacts; it helps auth the gmail account.
Expiry: 2 Years
Type: HTTP
This cookie is set by DoubleClick (which is owned by Google) to determine if the website visitor's browser supports cookies.
Expiry: 1 Year
Type: HTTP
this is used to send push notification using webengage.
Expiry: 1 Year
Type: HTTP
used by webenage to track auth of webenagage.
Expiry: Session
Type: HTTP
Linkedin sets this cookie to registers statistical data on users' behavior on the website for internal analytics.
Expiry: 1 Day
Type: HTTP
Use to maintain an anonymous user session by the server.
Expiry: 1 Year
Type: HTTP
Used as part of the LinkedIn Remember Me feature and is set when a user clicks Remember Me on the device to make it easier for him or her to sign in to that device.
Expiry: 1 Year
Type: HTTP
Used to store information about the time a sync with the lms_analytics cookie took place for users in the Designated Countries.
Expiry: 6 Months
Type: HTTP
Used to store information about the time a sync with the AnalyticsSyncHistory cookie took place for users in the Designated Countries.
Expiry: 6 Months
Type: HTTP
Cookie used for Sign-in with Linkedin and/or to allow for the Linkedin follow feature.
Expiry: 6 Months
Type: HTTP
allow for the Linkedin follow feature.
Expiry: 1 Year
Type: HTTP
often used to identify you, including your name, interests, and previous activity.
Expiry: 2 Months
Type: HTTP
Tracks the time that the previous page took to load
Expiry: Session
Type: HTTP
Used to remember a user's language setting to ensure LinkedIn.com displays in the language selected by the user in their settings
Expiry: Session
Type: HTTP
Tracks percent of page viewed
Expiry: Session
Type: HTTP
Indicates the start of a session for Adobe Experience Cloud
Expiry: Session
Type: HTTP
Provides page name value (URL) for use by Adobe Analytics
Expiry: Session
Type: HTTP
Used to retain and fetch time since last visit in Adobe Analytics
Expiry: 6 Months
Type: HTTP
Remembers a user's display preference/theme setting
Expiry: 6 Months
Type: HTTP
Remembers which users have updated their display / theme preferences
Expiry: 6 Months
Type: HTTP
Used by Google Adsense, to store and track conversions.
Expiry: 3 Months
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
These cookies are used for the purpose of targeted advertising.
Expiry: 6 Hours
Type: HTTP
These cookies are used for the purpose of targeted advertising.
Expiry: 1 Month
Type: HTTP
These cookies are used to gather website statistics, and track conversion rates.
Expiry: 1 Month
Type: HTTP
Aggregate analysis of website visitors
Expiry: 6 Months
Type: HTTP
This cookie is set by Facebook to deliver advertisements when they are on Facebook or a digital platform powered by Facebook advertising after visiting this website.
Expiry: 4 Months
Type: HTTP
Contains a unique browser and user ID, used for targeted advertising.
Expiry: 2 Months
Type: HTTP
Used by LinkedIn to track the use of embedded services.
Expiry: 1 Year
Type: HTTP
Used by LinkedIn for tracking the use of embedded services.
Expiry: 1 Day
Type: HTTP
Used by LinkedIn to track the use of embedded services.
Expiry: 6 Months
Type: HTTP
Use these cookies to assign a unique ID when users visit a website.
Expiry: 6 Months
Type: HTTP
These cookies are set by LinkedIn for advertising purposes, including: tracking visitors so that more relevant ads can be presented, allowing users to use the 'Apply with LinkedIn' or the 'Sign-in with LinkedIn' functions, collecting information about how visitors use the site, etc.
Expiry: 6 Months
Type: HTTP
Used to make a probabilistic match of a user's identity outside the Designated Countries
Expiry: 90 Days
Type: HTTP
Used to collect information for analytics purposes.
Expiry: 1 year
Type: HTTP
Used to store session ID for a users session to ensure that clicks from adverts on the Bing search engine are verified for reporting purposes and for personalisation
Expiry: 1 Day
Type: HTTP
Cookie declaration last updated on 24/03/2023 by Analytics Vidhya.
Cookies are small text files that can be used by websites to make a user's experience more efficient. The law states that we can store cookies on your device if they are strictly necessary for the operation of this site. For all other types of cookies, we need your permission. This site uses different types of cookies. Some cookies are placed by third-party services that appear on our pages. Learn more about who we are, how you can contact us, and how we process personal data in our Privacy Policy.
Edit
Resend OTP
Resend OTP in 45s