DEV Community

Ercan Canhasi
Ercan Canhasi

Posted on Originally published at Medium on

Can a Machine Tell Where an Albanian Is From, Just by Reading One Tweet?

How we built a geotagged multi-dialect corpus of Albanian from Twitter, and why the machine beat native speakers


Albanian dialects in the western Balkans. Figure 1 from Canhasi and Shijaku (2023), PLOS ONE, CC BY 4.0.

Every Albanian knows the feeling. You read a few lines from someone online and after a moment you think: this person is from Kosovo. Or from Tirana. Or maybe from Tetovo. We are quite sure about it, but if somebody asks us why, it is hard to explain.

My colleague Rexhep Shijaku and I wanted to check if a machine can do the same thing, and if it can, what exactly it is looking at. The result was a paper in PLOS ONE and a public dataset, the AlbDialectCorpus. In this story I want to explain the work in simple words, without the formulas.

A small language with big differences

Albanian is spoken by a few million people, but it is spread over several countries: Albania, Kosovo, North Macedonia, and a large diaspora in Europe and America. Traditionally the language is divided into two big dialect groups. Gheg is spoken in the north, and Tosk in the south, with the Shkumbin river in central Albania as the usual border line. Standard Albanian, the language of schools, newspapers and official documents, is based mostly on Tosk.

In Kosovo and North Macedonia people speak Gheg varieties. In daily life, and especially on social media, people mix standard language and their local dialect all the time. Since Albanian is written “as it is spoken”, these differences are visible in text. Sometimes two words differ by only one letter.

For NLP this is a real problem. Most tools treat Albanian as one thing. A spell checker, a translation system or a chatbot trained on standard Albanian will often fail on text from Prizren or Gostivar. To build better tools, first we need data that shows how people really write in each region. Such data did not exist, so we decided to build it.

Why Twitter

We chose Twitter (today X) for practical reasons. People write there in a natural and informal way, and at that time the API was open, so we could collect data at scale. Our main assumption was simple: where a user lives is a strong signal of which dialect they write in. Linguists have known this connection between language and geography since the nineteenth century.

The problem is that almost nobody tells Twitter where they live. Only 1 to 2 percent of tweets carry a location, and the “location” field in a profile is free text. People write whatever they want there.

You are where your friends are

So we used a different idea. A local politician, journalist or influencer is mostly followed by people from the same place. If you follow many Kosovar journalists, there is a good chance you are in Kosovo.

We started with 60 hand-picked “hub” accounts, 20 from each region: politicians from different parties, journalists, entrepreneurs, influencers and also ordinary users. From their followers we got 25,939 accounts, which became 18,619 after removing duplicates. Then we removed spam accounts (accounts that follow more than two thousand others) and were left with 13,812 valid users.

Next we had to read the location fields. Here we saw how creative people are. The city of Prizren, for example, appeared as “Prizren”, as “Pz”, and even as “04”, which is its car plate code. We wrote a small geoparsing algorithm to handle such cases, and it doubled the number of located users in Kosovo and Albania, and increased it about seven times for North Macedonia.

For the users who still had no location, we trained a classifier. It looks only at the social network: which of the 60 hubs a user follows, how important each hub is, plus some profile information like account age. With XGBoost this model guessed the country correctly 87.4% of the time, without reading a single tweet. “You are where your friends are” really works.

When language detectors do not recognize your language

The next surprise came during cleaning. Users in these regions do not tweet only in Albanian. In Kosovo you find English, Turkish, Serbian, Bosnian and German. In North Macedonia, Macedonian and Bulgarian. In Albania, a lot of Italian. We wanted to remove all non-Albanian tweets.

The obvious solution was to use a standard language identification tool like fastText. But it had one big flaw: it often did not recognize Gheg tweets as Albanian at all. A tweet in pure Kosovo dialect looked to the tool like some unknown language. If we had trusted the tool, we would have thrown away exactly the tweets that were most valuable for us.

We solved this with a combination. When fastText was very confident that a tweet is Albanian, we took it as standard Albanian. When fastText was confused, we passed the tweet to a small dialect model we trained ourselves, based on word n-grams. For me this is an important lesson for every low-resource language: the tools that work fine for the standard language can quietly erase the dialects.

We also replaced names of people, cities and countries with a generic token. Otherwise the model would learn that “Prishtina” means Kosovo and “Tirana” means Albania, which is true but not very interesting for dialect research.

The final corpus

In the end the AlbDialectCorpus has 2,503 users (835 from North Macedonia, 867 from Kosovo and 801 from Albania), almost 169,000 tweets, and about 2.25 million tokens. It is publicly available on GitHub and Zenodo, so anyone can use it.

The machine against native speakers

Then we trained many classifiers to predict the dialect: logistic regression, Naive Bayes, decision trees, random forests, KNN, XGBoost, support vector machines and fastText. The best one was a simple linear SVM. When it could see all tweets of a user, it reached an F1 score of 0.941. When it had to decide from a single tweet , often only ten or twelve words long, it still reached about 0.82.

To compare, we gave samples to four native-speaker annotators. Their average accuracy was 0.73 at the user level and 0.69 at the tweet level. The machine was clearly better.

The interesting part is where humans failed. They recognized standard Albanian and Tosk almost perfectly. Kosovo Gheg was also mostly fine. But the Gheg of North Macedonia was a real problem: annotators recognized it correctly in less than one third of cases, and most of the time they took it for Kosovo. For the classifier too, Kosovo and North Macedonia were the two classes it confused most. This makes sense, as both are Gheg and the regions are close to each other, but the machine still separated them much better than we did.

What the model is looking at

A good accuracy number is nice, but I wanted to know why the model decides as it does. We used two tools to open the black box: SHAP values, and archetypal analysis, a method I have worked with for many years.

The model found small words that people use without thinking. For North Macedonian Gheg, words like “xhi”, “majr”, “ama” and “ene” were among the strongest signals. For the southern, standard side, words like “qenë” and “pranë” stood out. Most native speakers will recognize these patterns when they see them. But many of them are not well described in the scientific literature. In a way, the model found knowledge that people carry in their heads but never wrote down.

Limitations, honestly

This work has limits. Geotagging through friends makes mistakes, especially for users whose network crosses borders. Tweets are short, and Twitter users are not a perfect sample of the population. Dialects also change over time, so a dataset from social media will slowly get old. And since Twitter closed its free API, building such a corpus today would be much harder and more expensive.

Why this matters now

When we wrote this paper, large language models were just becoming popular. Today everybody uses them, and the question of dialects is even more important. If AI systems learn Albanian only from standard, formal text, they will serve people from Tirana better than people from Prizren or Tetovo. They will also be worse at understanding the way most of us really write online.

Small languages need their own data, and dialects inside small languages need it even more. This corpus is one small step in that direction, and I hope other researchers will use it, improve it, and build on it.

The paper: Canhasi E., Shijaku R. (2023). Using Twitter to collect a multi-dialectal corpus of Albanian using advanced geotagging and dialect modeling. PLOS ONE 18(11): e0294284. https://doi.org/10.1371/journal.pone.0294284

The dataset: https://github.com/rexshijaku/albanian-dialect-corpus (DOI 10.5281/zenodo.8349099)

Top comments (0)