Skip to content

Instantly share code, notes, and snippets.

@todays-mitsui
Last active June 3, 2017 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save todays-mitsui/0ff24d6a2df9023de1556505f82a0dd4 to your computer and use it in GitHub Desktop.
Save todays-mitsui/0ff24d6a2df9023de1556505f82a0dd4 to your computer and use it in GitHub Desktop.
回答 - 言語処理100本ノック 2015 - 第1章
# -*- coding: utf-8 -*-
"""
00. 文字列の逆順
文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.
"""
print("stressed"[::-1])
# => desserts
# -*- coding: utf-8 -*-
"""
01. 「パタトクカシーー」
「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.
"""
print("パタトクカシーー"[::2])
# => パトカー
# -*- coding: utf-8 -*-
"""
02. 「パトカー」+「タクシー」=「パタトクカシーー」
「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.
"""
print("".join(s1+s2 for s1, s2 in zip("パトカー", "タクシー")))
# => パタトクカシーー
# -*- coding: utf-8 -*-
"""
03. 円周率
"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,
各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.
"""
import re
sentence = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
print([len(word) for word in re.split(r"[\s,.]+", sentence) if "" != word])
# => [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]
# -*- coding: utf-8 -*-
"""
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
specific_indexes = (1, 5, 6, 7, 8, 9, 15, 16, 19)
sentence = "Hi He Lied Because Boron Could Not Oxidize Fluorine." \
" New Nations Might Also Sign Peace Security Clause. Arthur King Can."
print({word[:1] if index in specific_indexes else word[:2]: index for index, word in enumerate(re.split(r"[\s,.]+", sentence), start=1) if "" != word})
# =>
# {
# 'Si': 14, 'He': 2, 'Ar': 18, 'O': 8, 'K': 19, 'C': 6,
# 'N': 7, 'Li': 3, 'B': 5, 'Mi': 12, 'Cl': 17, 'S': 16,
# 'Be': 4, 'Al': 13, 'F': 9, 'Ca': 20, 'P': 15, 'H': 1,
# 'Ne': 10, 'Na': 11
# }
# -*- coding: utf-8 -*-
"""
05. n-gram
与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.この関数を用い,"I am an NLPer"という文から単語bi-gram,文字bi-gramを得よ.
"""
import re
def n_gram(seq, n=2):
seq_set = (seq[i:] for i in range(n))
return tuple("".join(chars) for chars in zip(*seq_set))
sentence = "I am an NLPer"
char_bi_gram = n_gram(sentence)
print("char_bi_gram:", char_bi_gram)
# => char_bi_gram: ('I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er')
words = re.split(r"[\s,.]", sentence)
word_bi_gram = n_gram(words)
print("word_bi_gram:", word_bi_gram)
# => word_bi_gram: ('Iam', 'aman', 'anNLPer')
# -*- coding: utf-8 -*-
"""
06. 集合
"paraparaparadise"と"paragraph"に含まれる文字bi-gramの集合を,それぞれ, XとYとして求め,
XとYの和集合,積集合,差集合を求めよ.さらに,'se'というbi-gramがXおよびYに含まれるかどうかを調べよ.
"""
def n_gram(seq, n=2):
seq_set = (seq[i:] for i in range(n))
return tuple("".join(chars) for chars in zip(*seq_set))
word_x = "paraparaparadise"
word_y = "paragraph"
x = set(n_gram(word_x))
y = set(n_gram(word_y))
union = x | y
print("union:", union)
# => union: {'pa', 'se', 'ad', 'is', 'ar', 'ap', 'gr', 'ag', 'ph', 'ra', 'di'}
intersection = x & y
print("intersection:", intersection)
# => intersection: {'pa', 'ra', 'ar', 'ap'}
difference_x_y = x - y
print("difference (x-y):", difference_x_y)
# => difference (x-y): {'di', 'is', 'se', 'ad'}
difference_y_x = y - x
print("difference (y-x):", difference_y_x)
# => difference (y-x): {'ph', 'ag', 'gr'}
print("'se' in X ?:", "se" in x)
# => 'se' in X ?: True
print("'se' in Y ?:", "se" in y)
# => 'se' in Y ?: False
# -*- coding: utf-8 -*-
"""
07. テンプレートによる文生成
引数x, y, zを受け取り「x時のyはz」という文字列を返す関数を実装せよ.さらに,x=12, y="気温", z=22.4として,実行結果を確認せよ.
"""
def template(x, y, z):
return u"{x}時の{y}は{z}".format(x=x, y=y, z=z)
print(template(x=12, y="気温", z=22.4))
# => 12時の気温は22.4
# -*- coding: utf-8 -*-
"""
08. 暗号文
与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ.
- 英小文字ならば(219 - 文字コード)の文字に置換
- その他の文字はそのまま出力
この関数を用い,英語のメッセージを暗号化・復号化せよ.
"""
import re
def cipher(plaintext):
return re.sub(r"[a-z]", lambda m: chr(219 - ord(m.group(0))), plaintext)
plaintext = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor" \
" incididunt ut labore et dolore magna aliqua."
print("Plaintext:", plaintext)
# => Plaintext:
# Lorem ipsum dolor sit amet, consectetur adipiscing elit,
# sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
encrypt = cipher(plaintext)
print("Encryption:", encrypt)
# => Encryption:
# Llivn rkhfn wloli hrg znvg, xlmhvxgvgfi zwrkrhxrmt vorg,
# hvw wl vrfhnlw gvnkli rmxrwrwfmg fg ozyliv vg wloliv nztmz zorjfz.
decrypt = cipher(encrypt)
print("Decryption:", decrypt)
# => Decryption:
# Lorem ipsum dolor sit amet, consectetur adipiscing elit,
# sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
# -*- coding: utf-8 -*-
"""
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
def stir(word):
if 5 > len(word):
return word
head = word[0]
last = word[-1]
body = word[1:-1]
return head + "".join(random.sample(body ,len(body))) + last
def genTypoglycemia(sentence):
return " ".join(map(stir, sentence.split(" ")))
plaintext = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."
print("Plaintext:", plaintext)
# => Plaintext:
# I couldn't believe that I could actually understand what I was reading :
# the phenomenal power of the human mind .
typoglycemia = genTypoglycemia(plaintext)
print("Typoglycemia:", typoglycemia)
# => Typoglycemia:
# I clon'udt bleivee that I cloud aatlulcy unredantsd what I was raiedng :
# the penonehmal pewor of the hmaun mind .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment