哪個國家的維基百科內容最多?

王林
發布: 2024-08-29 18:31:00
原創
456 人瀏覽過

介紹

當我在網路上搜尋某些內容時,我經常發現英文內容比法語內容全面得多。

雖然考慮到世界上講英語的人數與講法語的人數相比(大約多 4 到 5 倍),這似乎是顯而易見的,但我想測試這個假設並對其進行量化。

TLDR:平均而言,維基百科上的英文文章比法文文章包含的資訊多 19%

此分析的原始程式碼可在此處取得:https://github.com/jverneaut/wikipedia-analysis/

協定

維基百科是全球網路上最大的優質內容來源之一。

在撰寫本文時,英文版擁有超過 6,700,000 篇獨特文章,而法文版只有 2,500,000 篇。我們將使用這個語料庫作為我們學習的基礎。

使用蒙特卡羅方法,我們將從維基百科中針對每種語言隨機抽取文章,併計算該語料庫的平均字元長度。有了足夠的樣本,我們應該得到接近現實的結果。

由於Wikimedia API沒有提供取得文章字元長度的方法,我們將透過以下方式取得此資訊:

  1. 透過維基媒體 API 檢索大量文章樣本的位元組大小。
  2. 使用蒙特卡羅方法從一小部分文章樣本中估計每個字元的位元組數。
  3. 使用步驟 2 中獲得的每個字元位元組估計值來擷取大量文章的字元數。

由於我們使用蒙特卡羅方法來估計每個字元的位元組數,因此我們需要盡可能多的文章數來盡量減少與實際數量的偏差。

維基媒體 API 文件指定了這些限制:

  • 每個請求的隨機文章不超過 500 篇。
  • 每個請求的文章內容不得超過 50 則。

考慮到這些限制,並作為精度和查詢執行時間之間的折衷,我選擇對每種語言採樣100,000 篇文章作為文章字節長度的參考,並選擇500 篇文章來估計每種語言每個字元的位元組數。

限制

目前,當要求提供文章內容時,維基媒體 API 會傳回自己的維基文字格式。這種格式不是純文本,更接近 HTML。由於維基媒體上的所有語言都使用相同的格式,我估計我們可以依賴它而不影響我們最終結果的方向。

但是,某些語言比其他語言更冗長。例如,在法語中,我們說“Comment ça va?” (15 個字元)與“你好嗎?”相比(12 個字元)英文。這項研究沒有解釋這種現象。如果我們想解決這個問題,我們可以比較同一本書語料庫的不同翻譯,以建立語言的「密度」校正變數。在我的研究中,我沒有找到任何數據提供適用於每種語言的比率。

不過,我確實發現了一篇非常有趣的論文,它比較了 17 種不同語言的資訊密度以及它們的說話速度。其結論是,最「高效」的語言比效率最低的語言說得更慢,導致口頭訊息傳輸速率始終保持在每秒 39 位元左右。

有趣。

取得每種語言文章的平均位元組長度

如協定所述,我們將使用維基百科 API 來擷取給定語言的 500 篇隨機文章。

def getRandomArticlesUrl(locale):
    return "https://" + locale + ".wikipedia.org/w/api.php?action=query&generator=random&grnlimit=500&grnnamespace=0&prop=info&format=json"

def getRandomArticles(locale):
    url = getRandomArticlesUrl(locale)
    response = requests.get(url)
    return json.loads(response.content)["query"]["pages"]
登入後複製

這會給我們一個類似{ "id1": { "title": "...", "length": 1234 }, "id2": { "title": "...", "length " : 5678 }, ... } 我們可以用它來檢索大量文章的大小(以位元組為單位)。

然後對這些數據進行重新處理以獲得下表:

Language Average length ...
EN 8865.33259
FR 7566.10867
RU 10923.87673
JA  9865.59485
...

乍一看,英文文章的位元組長度似乎比法文文章的位元組長度還要大。同樣,俄語的位元組長度比任何其他語言的位元組長度都大。

Which country has the most content on Wikipedia?

我們應該停止這個結論嗎?不完全是。由於維基百科報告的長度是以位元組為單位的長度,因此我們需要更深入地研究字元的編碼方式以理解這些初始結果。

信件如何編碼:UTF-8 簡介

什麼是位元組?

與你我不同,計算機沒有字母的概念,更不用說字母表了。對於它來說,一切都被表示為 0 和 1 的序列。

在我們的十進制系統中,我們從 0 到 1,然後從 1 到 2,依此類推,直到 10。

對於使用二進位系統的計算機,我們從 0 到 1,然後從 1 到 10,然後從 10 到 11、100,等等。

這裡有一個比較表,可以更清楚地說明:

Decimal Binary
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
...

學習二進位遠遠超出了本文的範圍,但是您可以看到,隨著數字變大,其二進位表示形式比十進位表示形式「更寬」。

由於電腦需要區分數字,因此它將它們儲存在稱為位元組的 8 個單位的小資料包中。一個位元組由 8 位元組成,例如 01001011。

UTF-8如何儲存字符

我們已經了解如何儲存數字,儲存字母會稍微複雜一些。

我們在許多西方國家使用的拉丁字母使用 26 個字母的字母表。難道我們不能使用一個參考表,其中從 0 到 25 的每個數字對應一個字母嗎?

Letter Index Binary index
a 0 00000000
b 1 00000001
c 2 00000010
... ... ...
z 25 00011001

但是我們的字元不只是小寫字母。在這個簡單的句子中,我們還有大寫字母、逗號、句點等。我們創建了一個標準化列表,將所有這些字元包含在一個位元組中,稱為 ASCII 標準。

在計算的初期,ASCII 足以滿足基本用途。但是如果我們想使用其他字元怎麼辦?我們如何用西里爾字母(33 個字母)書寫?這就是創建 UTF-8 標準的原因。

UTF-8 代表 Unicode(通用編碼字元集)T轉換 Format - 8 位元。它是一種編碼系統,允許電腦使用一個或多個位元組儲存字元。

為了指示資料使用了多少字節,此編碼的第一位用於表示此資訊。

First UTF-8 bits Number of bytes used
0xxxxxx 1
110xxxxx ... 2
1110xxxx ... ... 3
11110xxx ... ... ... 4

The following bits also have their purpose, but once again, this goes beyond the scope of this article. Just note that, at a minimum, a single bit can be used as a signature in cases where our character fits within the x1111111 = 127 remaining possibilities.

For English, which does not use accents, we can assume that most characters in an article will be encoded this way, and therefore the average number of bytes per character should be close to 1.

For French, which uses accents, cedillas, etc., we assume that this number will be higher.

Finally, for languages with a more extensive alphabet, such as Russian and Japanese, we can expect a higher number of bytes, which provides a starting point for explaining the results obtained earlier.

Get the average character length in bytes of articles for each language

Now that we understand what the value returned earlier by the Wikipedia API means, we want to calculate the number of bytes per character for each language in order to adjust these results.

To do this, we use a different way of accessing the Wikipedia API that allows us to obtain both the content of the articles and their byte length.

Why not use this API directly?

This API only returns 50 results per request, whereas the previous one returns 500. Therefore, in the same amount of time, we can get 10 times more results this way.

More concretely, if the API calls took 20 minutes with the first method, they would take 3 hours and 20 minutes with this approach.

def getRandomArticlesUrl(locale):
    return "https://" + locale + ".wikipedia.org/w/api.php?action=query&generator=random&grnlimit=50&grnnamespace=0&prop=revisions&rvprop=content|size&format=json"

def getRandomArticles(locale):
    url = getRandomArticlesUrl(locale)
    response = requests.get(url)
    return json.loads(response.content)["query"]["pages"]
登入後複製

Once this data is synthesized, here is an excerpt of what we get:

Language Bytes per character ...
EN 1.006978892420735
FR 1.0243214042939228
RU 1.5362439940531318
JA 1.843857157700553
...

So our intuition was correct: countries with a larger alphabet distort the data because of the way their content is stored.

We also see that French uses more bytes on average to store its characters than English as we previously assumed.

Results

We can now correct the data by changing from a size in bytes to a size in characters which gives us the following graph:

Which country has the most content on Wikipedia?

Our hypothesis is therefore confirmed.

On average, English is the language with the most content per page on Wikipedia. It is followed by French, then Russian, Spanish, and German.

The standard deviation (shown with the black bars) is large for this dataset, which means that the content size varies greatly from the shortest to the longest article. Therefore, it is difficult to establish a general truth for all articles, but this trend still seems consistent with my personal experience of Wikipedia.

If you want all the results from this experiment, I have also created this representation, which compares each language with its percentage of additional/less content relative to the others.

Which country has the most content on Wikipedia?

Thanks to this, we therefore find our conclusion that on average, an English article on Wikipedia contains 19% more information than its equivalent in French.

The source code for this analysis is available here: https://github.com/jverneaut/wikipedia-analysis/

以上是哪個國家的維基百科內容最多?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!