4  Mapping El Corrido de Gregorio

Modified

March 17, 2023

packages required

Code
import geopandas as gpd
from pyproj import CRS
import pandas as pd
import numpy as np

from geograpy import places
import re

import geopy
from geopy.geocoders import ArcGIS


import shapely
from shapely.geometry import Point
from shapely.wkt import loads
import plotly.express as px 

import contextily as cx
import xyzservices.providers as xyz
import matplotlib.pyplot as plt #to make sure there are no errors when plotting a graph
import pyproj

import spacy
from spacy import displacy

import googlemaps

import locationtagger
nlp = spacy.load("en_core_web_sm")

Extracting Name Enities (places) from text document

Code
text = "elcorridodegregoriocortez.txt"
with open(text, 'r', encoding='utf-8') as c:
    text = c.read()
    
def clean_text(text):
    cleaned= re.sub(r'[":;,.“”]', "", text)
    return(cleaned)
text = clean_text(text)
#print(text)


TxGPE=[]
nlp = spacy.load("tx_trained_ner")
doc =nlp(text) 
#print(doc)
for ent in doc.ents:
    #print(ent.text, ent.label_)
    if ent.label_ == "GPE":
        TxGPE.append(ent.text)
#print(TxGPE)

ents = [(e.text, e.start_char, e.end_char, e.label_)for e in doc.ents]
entsname=[(e.text) for e in doc.ents]
print(entsname)
['Karnes', 'Gonzales', 'Belmont', 'Laredo', 'Encinal']

Visulization: Highlighting Name Entities from text

Code
displacy.render(doc, style ='ent', jupyter=True, page=True)
displaCy
In the country of Karnes GPE
Look what has happened
The Major Sheriff died
Leaving Román badly wounded

It must have been two in the
afternoon
When people arrived
They said to one another
It is not known who killed him

They went around asking questions
About half an hour afterward
They found that the wrongdoer
Had been Gregorio Cortez

Now they have outlawed Cortez
Throughout the whole state
Let him be taken dead or alive
He has killed several men

They said Gregorio Cortez
With his pistol in his hand
I don’t regret that I killed him
I regret my brother’s death

Then said Gregorio Cortez
And his soul was all a flame
I don’t regret that I killed him
A man must defend himself

The Americans were coming
They were whiter than a dove
From the fear that they had
Of Cortez and of his pistol

Then the Americans said
Then they said fearfully
Come let us follow the trail
The wrongdoer is Cortez

They set the bloodhounds on him
So they could follow his trail
But trying to overtake Cortez
Was like following a star

He struck out for Gonzales GPE
Without showing any fear
Follow me cowardly rangers
I am Gregorio Cortez

From Belmont GPE he went to the
ranch
They succeeded in surrounding
him
Quite a few more than three hundred
But there he jumped their corral

When he jumped their corral
According to what we hear
They got into a gunfight
And he killed them another sheriff

Then said Gregorio Cortez
With his pistol in his hand
Don’t run you cowardly rangers
From just one Mexican

Gregorio Cortez went out
He went towards Laredo GPE
They decided not to follow
Because they were afraid of him

Then said Gregorio Cortez
What is the use of your scheming?
You cannot catch me
Even with those bloodhounds

Then the Americans said
If we catch you with him what
shall we do?
If we fight him man to man
Very few of us will return

Over by El Encinal GPE
According to what we hear
They made him a corral
And he killed them another sheriff

Then said Gregorio Cortez
Shooting out a lot of bullets
I have weathered thunderstorms
This little mist doesn’t bother me

Now he has met a Mexican
He says to him haughtily
tell me the news
I am Gregorio Cortez

It is said that because of me
Many people have been killed
I will surrender now
Because such things are not right

Cortez says to Jesús
At last you are going to see it
Go tell the rangers
To come and arrest me

All the rangers were coming
Coming so fast they even flew
For they wanted to get
The thousand dollars they were
offered

When they surrounded the house
Cortez suddenly appeared before
them
You will take me if I’m willing
But not any other way

Then the Major Sheriff said
As if he was going to cry
Cortez hand over your weapons
We are not going to kill

Then said Gregorio Cortez
Shouting to them in a loud voice
I won’t surrender my arms
Until I am in a cell

Then said Gregorio Cortez
He said in his godly voice
I won’t surrender my arms
Until I’m inside a jail

Now they have taken Cortez
Now matters are at an end
His poor family
Are suffering in their hearts

Now with this I say farewell
In the shade of a cypress tree
This is the end of the singing
of the ballad of Cortez

Creating Dataframe

Code
df = pd.DataFrame({'NER': entsname})
geolocator = ArcGIS(user_agent='CorridosMap')
geocode = lambda query: geolocator.geocode("%s, Texas" % query)
#print(geocode(entsname))
df['Coordinates'] = df['NER'].apply(geocode)
df
NER Coordinates
0 Karnes (Karnes County, Texas, (28.905630820000056, -9...
1 Gonzales (Gonzales, Texas, (29.500150000000076, -97.452...
2 Belmont (Belmont, Texas, (29.52287000000007, -97.68328...
3 Laredo (Laredo, Texas, (27.530920000000037, -99.50230...
4 Encinal (Encinal, Texas, (28.046230000000037, -99.3566...

Creating GeoDataframe

Code
gdf = gpd.tools.geocode(df.Coordinates, provider='ArcGIS')
gdf = gpd.GeoDataFrame(gdf, crs="EPSG:4326")
gdf["lat"]=gdf['geometry'].y
gdf ["lon"] = gdf['geometry'].x

gdf
geometry address lat lon
0 POINT (-97.85942 28.90563) Karnes County, Texas 28.905631 -97.859421
1 POINT (-97.45223 29.50015) Gonzales, Texas 29.500150 -97.452230
2 POINT (-97.68329 29.52287) Belmont, Texas 29.522870 -97.683290
3 POINT (-99.50231 27.53092) Laredo, Texas 27.530920 -99.502310
4 POINT (-99.35670 28.04623) Encinal, Texas 28.046230 -99.356700

Creating Mapping Visualization from El Corrido de Gregorio Cortez

Code
px.set_mapbox_access_token(open("mapboxtoken").read())
fig = px.line_mapbox(gdf, text="address",
                        lat=gdf.geometry.y,
                        lon=gdf.geometry.x,
                        labels="address",
                        )

fig.update_layout(mapbox_style="stamen-terrain", mapbox_zoom=6, mapbox_center_lat = 29,
    margin={"r":0,"t":0,"l":0,"b":0})



fig.show()