首页  >  文章  >  后端开发  >  哪个国家的维基百科内容最多?

哪个国家的维基百科内容最多?

王林
王林原创
2024-08-29 18:31:00461浏览

介绍

当我在互联网上搜索某些内容时,我经常发现英语内容比法语内容全面得多。

虽然考虑到世界上讲英语的人数与讲法语的人数相比(大约多 4 到 5 倍),这似乎是显而易见的,但我想测试这个假设并对其进行量化。

TLDR:平均而言,维基百科上的英文文章比法文文章包含的信息多 19%

此分析的源代码可在此处获取:https://github.com/jverneaut/wikipedia-analysis/

协议

维基百科是全球网络上最大的优质内容来源之一。

在撰写本文时,英文版拥有超过 6,700,000 篇独特文章,而法文版只有 2,500,000 篇。我们将使用这个语料库作为我们学习的基础。

使用蒙特卡洛方法,我们将从维基百科中针对每种语言随机抽取文章,并计算该语料库的平均字符长度。有了足够多的样本,我们应该得到接近现实的结果。

由于Wikimedia API没有提供获取文章字符长度的方法,我们将通过以下方式获取此信息:

  1. 通过维基媒体 API 检索大量文章样本的字节大小。
  2. 使用蒙特卡罗方法从一小部分文章样本中估计每个字符的字节数。
  3. 使用步骤 2 中获得的每字符字节估计值检索大量文章的字符数。

由于我们使用蒙特卡洛方法来估计每个字符的字节数,因此我们需要尽可能多的文章数来尽量减少与实际数量的偏差。

Wikimedia 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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn