-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a41f14
commit 59ff257
Showing
2 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import json | ||
import urllib.parse | ||
import urllib.request | ||
|
||
_ZHWIKI_SOURCE_URL = "https://zh.wikipedia.org/w/api.php?action=parse&format=json&prop=wikitext&uselang=zh&formatversion=2&page=" | ||
_PAGE = "中国大陆网络用语列表" | ||
|
||
page = urllib.request.urlopen(_ZHWIKI_SOURCE_URL + urllib.parse.quote(_PAGE)).read() | ||
wikitext = json.loads(page)["parse"]["wikitext"] | ||
words = set() | ||
|
||
|
||
def add_word(word): | ||
if word.startswith("形容"): | ||
return | ||
for garbage in ("、", "[", "]", "…"): | ||
word = word.replace(garbage, "") | ||
words.add(word.strip()) | ||
|
||
|
||
def add_words(word): | ||
for word_separator in ("、", "/", "|", ",", "。"): | ||
if word_separator in word: | ||
for w in word.split(word_separator): | ||
# recursively resolve | ||
add_words(w.strip()) | ||
break | ||
else: | ||
add_word(word) | ||
|
||
|
||
for line in wikitext.split("\n"): | ||
if line.startswith("*"): | ||
# Lists | ||
for table_separator in (":", ":"): | ||
if table_separator in line: | ||
word = line.split(table_separator)[0].strip("*").strip() | ||
add_words(word) | ||
break | ||
elif line.startswith("|"): | ||
# Tables | ||
word = line.split("|")[1] | ||
add_words(word) | ||
|
||
for word in words: | ||
print(word) |