This is a simple tutorial that allows to start using Lingtypology.
For more information you can consult docstrings (e.g. lingtypology.LingMap.__doc__
).
In this paragraph I will consider some basic features that you can use.
The simplest script that draws a map with Romanian and Ukrainian languages will be:
import lingtypology
m = lingtypology.LingMap(('Romanian', 'Ukrainian'))
m._create_map()
Be advised that _create_map()
returns a Folium
map. In this notebook it is used because the map may
be displayed this way.
To save your map as html run: m.save('map.html')
To get html as str
run: m.render()
Simply drawing languages on a map is not very interesting. Let's say that we want to mark some morphological features as different colors.
languages = ["Adyghe", "Kabardian", "Polish", "Russian", "Bulgarian"]
features = ["Agglutinative", "Agglutinative", "Inflected", "Inflected", "Analythic"]
m1 = lingtypology.LingMap(languages)
m1.add_features(features)
m1._create_map()
As you can see, the map is not centered properly. It can be fixed with passing coordinates to the start_location
attribute.
Also, you can change colors using the colors
attribute.
m1.start_location = (40, 40)
m1.colors = ("yellowgreen", "red", "blue")
m1._create_map()
You can also use lingtypology.glottolog
to get some data from it. For example, I want to add language affiliations to popups.
affs = lingtypology.glottolog.get_affiliations(languages)
m1.add_popups(affs)
m1._create_map()
You can pass additional parameters to the add_features
method.
If for some reason you do not wish to use colors, you could use shapes.
If you want to add controls, you can do it as well.
m1.add_features(features, use_shapes=True, control=True)
m1._create_map()
import pandas
circassian = pandas.read_csv(
'https://raw.githubusercontent.com/ropensci/lingtypology/master/database_creation/circassian.csv',
delimiter=',',
header=0,
)
coordinates = zip(list(circassian.latitude), list(circassian.longitude))
dialects = circassian.dialect
languages = circassian.language
popups = circassian.village
#Creating LingMap object
m2 = lingtypology.LingMap(languages)
#Setting up start location
m2.start_location = (44.21, 42.32)
#Setting up start zoom
m2.start_zoom = 7
#Inner features < dialect
m2.add_features(dialects)
#Outer features < language
m2.add_stroke_features(languages)
#Popups < village
m2.add_popups(popups)
#Tooltips < language
m2.add_tooltips(languages)
#Custom coordinates (override the ones from Glottolog)
m2.add_custom_coordinates(coordinates)
#Inner legend title and position
m2.legend_title = 'Dialects'
m2.legend_position = 'bottomright'
#Outer legend title and position
m2.stroke_legend_title = 'Languages'
m2.stroke_legend_position = 'topright'
m2._create_map()
If you are used to the R package, you may find this way to program the second example more appealing.
However, it is not recommended because most of customization is impossible.
coordinates = zip(list(circassian.latitude), list(circassian.longitude))
dialects = circassian.dialect
languages = circassian.language
popups = circassian.village
lingtypology.map_feature(
languages,
custom_coordinates = coordinates,
features = dialects,
stroke_features = languages,
popups = popups,
tooltips = languages,
legend_title = 'Dialects',
legend_position = 'bottomright',
stroke_legend_title = 'Languages',
stroke_legend_position = 'topright',
start_zoom = 7,
start_location = (44.21, 42.32),
save_html = 'circassian.html',
)
data = pandas.read_csv(
'https://raw.githubusercontent.com/ropensci/lingtypology/master/database_creation/ejective_and_n_consonants.csv',
delimiter=',',
header=0,
)
mc = lingtypology.LingMap(data.language)
mc.legend_title='Amount of consonants'
mc.add_tooltips(data.consonants)
#If numeric is True, it will look like this
mc.add_features(data.consonants, numeric=True)
mc._create_map()
It is possible to access Wals data (online) using lingtypology.db_apis
.
import lingtypology
from lingtypology.db_apis import Wals
wals_page = Wals('1a').get_df()
m3 = lingtypology.LingMap(wals_page.language)
m3.add_custom_coordinates(wals_page.coordinates)
m3.add_features(wals_page._1A)
m3.legend_title = 'Consonant Inventory'
m3._create_map()
It would seem unfaire if we could only draw circles.
Let's draw a heatmap (the more density, the more languages with Large consonant inventory).
import pandas
import lingtypology
from lingtypology.db_apis import Wals
wals_page = Wals('1a').get_df()
#First initialize LingMap without languages
m4 = lingtypology.LingMap()
#Add heatmap from the Wals data
m4.add_heatmap(wals_page[wals_page._1A == 'Large'].coordinates)
#Let's also add a title to the map
m4.title = 'Large Consonant Inventories'
m4._create_map()
from lingtypology import glottolog
Get genealogy information:
glottolog.get_affiliations(('Russian', 'English'))
Get coordinates of a language:
glottolog.get_coordinates('Russian')
Get the Glottolog ID (aka Glottocode):
glottolog.get_glot_id('Russian')
Get the macroarea:
glottolog.get_macro_area('Russian')
Get the ISO code:
glottolog.get_iso('Russian')
Get language by ISO code:
glottolog.get_by_iso('rus')
Get language by Glottocode:
glottolog.get_by_glot_id('russ1263')
Get Glottocode by ISO:
glottolog.get_glot_id_by_iso('rus')
Get ISO by Glottocode:
glottolog.get_iso_by_glot_id('russ1263')
Each new release of lingtypology
is shipped with the latest version of the Glottolog data. The information about the version of Glottolog can be retrieved using:
from lingtypology import glottolog
#Yet, I cannot guarantee that this tutorial is updated with each release.
#If you want to get your version, run this code on your machine.
glottolog.version
Nevertheless, if you want to get the newest release and do not wish to wait for the next version of lingtypology
, you can download the version you wish locally by following these steps:
1) Download glottolog
from here.
2) If you downloaded it as an archive, unpack it.
3) In your home directory make a folder and call it .lingtypology_data
(with .
in the beginning).
4) Move glottolog
into .lingtypology_data
.
5) Run the following command:
glottolog --repos=glottolog languoids
6) It will generate two small files (csv
and json
). Now you can delete everything except for these files from the directory.
This Python package is enspired by the R package made by agricolamz.
Also this module uses JavaScript library Leaflet and its Python-wrapper Folium and Branca.
Also, special thanks to Xrotwang for helping me with the package.
Copyright (C) 2018-2019 Michael Voronov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.