LoginSignup
6
8

More than 5 years have passed since last update.

言語処理100本ノック 第1章 (Python)

Last updated at Posted at 2016-05-12

言語処理100本ノック
http://www.cl.ecei.tohoku.ac.jp/nlp100/
から第1章 00〜09までを記載

00. 文字列の逆順

文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.

print('stressed'[::-1])

01. 「パタトクカシーー」

「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.

print('パタトクカシーー'[::2])

02. 「パトカー」+「タクシー」=「パタトクカシーー」

「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.

print(''.join(x+y for x, y in zip('パトカー', 'タクシー')))

03. 円周率

"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.

import re

s = 'Now I need a drink, alcoholic of course, after the heavy \
lectures involving quantum mechanics.'

s = re.sub(r'[^A-Za-z\ ]+', '', s)
print([len(x) for x in s.split()])
コメント頂いたもの
s = 'Now I need a drink, alcoholic of course, after the heavy \
lectures involving quantum mechanics.'

print([len(w.rstrip(',.')) for w in s.split()])
コメント頂いたもの
s = 'Now I need a drink, alcoholic of course, after the heavy \
lectures involving quantum mechanics.'

print([sum(c.isalpha() for c in w) for w in s.split()])

04. 元素記号

"Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."という文を単語に分解し,1, 5, 6, 7, 8, 9, 15, 16, 19番目の単語は先頭の1文字,それ以外の単語は先頭に2文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.

import re

s = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might \
Also Sign Peace Security Clause. Arthur King Can.'

s = re.sub(r'[^A-Za-z\ ]+', '', s)
print(
    {x[:1] if i in [1, 5, 6, 7, 8, 9, 15, 16, 19] else x[:2]: i+1 \
        for i, x in enumerate(s.split(' '), 1)}
)
コメント頂いたもの
s = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might \
Also Sign Peace Security Clause. Arthur King Can.'

print({w[:2-(i in (1,5,6,7,8,9,15,16,19))]:i for i,w in enumerate(s.split(),1)})

05. n-gram

与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.この関数を用い,"I am an NLPer"という文から単語bi-gram,文字bi-gramを得よ.

def n(s):
    return [s[i:i+2] for i in range(len(s) if len(s) % 2 == 0 else len(s)-1)]

s = 'I am an NLPer'

print(n(s))
print(n(s.split(' ')))

06. 集合

"paraparaparadise"と"paragraph"に含まれる文字bi-gramの集合を,それぞれ, XとYとして求め,XとYの和集合,積集合,差集合を求めよ.さらに,'se'というbi-gramがXおよびYに含まれるかどうかを調べよ.

def n(s):
    return [s[i:i+2] for i in range(len(s) if len(s) % 2 == 0 else len(s)-1)]

x = set(n('paraparaparaise'))
y = set(n('paragraph'))

print(x.union(y))
print(x.intersection(y))
print(x.difference(y))

print("se" in x)
print("se" in y)

07. テンプレートによる文生成

引数x, y, zを受け取り「x時のyはz」という文字列を返す関数を実装せよ.さらに,x=12, y="気温", z=22.4として,実行結果を確認せよ.

def f(x, y, z):
    return '%s時の%sは%s' % (x, y, z)

print(f(12, '気温', 22.4))

08. 暗号文

与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ.

英小文字ならば(219 - 文字コード)の文字に置換
その他の文字はそのまま出力
この関数を用い,英語のメッセージを暗号化・復号化せよ.

def cipher(s):
    r = ''
    for x in s:
        if 97 <= ord(x) <= 122:
            r += chr(219 - ord(x))
        else:
            r += x
    return r

s = "I couldn't believe that I could actually understand what I was reading : \
the phenomenal power of the human mind ."

print(cipher(s))
print(cipher(cipher(s)))
コメント頂いたもの
def cipher(s):
    return ''.join(c.islower() and chr(219-ord(c)) or c for c in s)

s = "I couldn't believe that I could actually understand what I was reading : \
the phenomenal power of the human mind ."

print(cipher(s))
print(cipher(cipher(s)))

09. Typoglycemia

スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,それ以外の文字の順序をランダムに並び替えるプログラムを作成せよ.ただし,長さが4以下の単語は並び替えないこととする.適当な英語の文(例えば"I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .")を与え,その実行結果を確認せよ.

import random

s = "I couldn't believe that I could actually understand what I was \
reading : the phenomenal power of the human mind ."

s = s.split(' ')
for i, x in enumerate(s):
    if len(x) > 4:
        r = x[1:-1]
        s[i] = x[0] + ''.join(random.sample(r, len(r))) + x[-1]

print(' '.join(s))
6
8
5

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
6
8