LoginSignup
1
2

More than 5 years have passed since last update.

Jupyterの小技3

Posted at

Jupyterの小技3
Jupyterの小技2の続き

世界一のものを表示させる

準備として、まず、下記を実行してください。

jupyter_notebook
import random, re, urllib, IPython.core.getipython
from itertools import takewhile
with urllib.request.urlopen('https://ja.wikipedia.org/wiki/'
    '%E4%B8%96%E7%95%8C%E4%B8%80%E3%81%AE%E4%B8%80%E8%A6%A7') as fp:
    s0 = fp.read().decode('utf8')
s = re.sub('<img.*?/>', '', s0.replace('\u3000', ''))
le = 0
while le != len(s):
    le = len(s)
    s = re.sub(r'<(a|b|i|span|strong|sup)[^>]*>(.*?)</\1>', r'\2', s)
s = re.sub(r'\[[0-9]*]', '', s)
ll = re.findall('<li>(.*?)</li>', s[10000:])
ll = [i for i in takewhile(lambda s: not s.startswith('ギネス世界記録'), ll)
    if '最' in i or '世界一' in i or len(i) > 15]
def G_impl(s, lst=ll):
    print(random.choice(lst))
ip = IPython.core.getipython.get_ipython()
if ip:
    ip.register_magic_function(G_impl, magic_name='G')

実行してみる

jupyter_notebook
G
>>>
音節が世界一少ない言語の一つ)→ ハワイ語 - 35音節
jupyter_notebook
G
>>>
不規則動詞が世界一多い言語 フランス語 - 570

以上

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2