0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

言語処理100本ノック 第1章 解いてみた

Posted at

はじめに

  • YouTubeでPythonの基礎文法の動画見たことあるくらいのレベル
  • 開発経験はほぼありません。業務でJavaを3週間程度触りました
  • Google Colaboratoryを使用しています
  • 誤りや変なコーディングあればご指摘頂けると幸いです
  • 第2章以降も頑張りたいと思っています。

00. 文字列の逆順

str = "stressed"
print(str[::-1])

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

str = "パタトクカシー"
print(str[::2])

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

str1 = "パトカー"
str2 = "タクシー"
for c1, c2 in zip(str1, str2):
  print(c1 + c2, end='')

03. 円周率

s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
result = []
for word in s.replace(",", "").replace(".", "").split(" "):
  result.append(len(word))
print(result)

04. 元素記号

s = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
word_list = s.replace(".", "").split(" ")
num_list = [1, 5, 6, 7, 8, 9, 15, 16, 19]
result = {}
for index, word in enumerate(word_list):
  if (index + 1) in num_list:
    result[word[:1]] = index + 1
  else:
    result[word[:2]] = index + 1
print(result)

05. n-gram

def make_n_gram(n, text_list):
  n_gram = []
  for i in range(len(text_list) - (n - 1)):
    n_gram.append(text_list[i:i+n])
  return n_gram

text = "I am an NLPer"
c_bi_gram = make_n_gram(2, list((text.replace(" ", ""))))
w_bi_gram = make_n_gram(2, text.split(" "))
print(c_bi_gram)
print(w_bi_gram)

06. 集合

text1 = "paraparaparadise"
text2 = "paragraph"
bi_gram1 = make_n_gram(2, list(text1))
bi_gram2 = make_n_gram(2, list(text2))
# listはset(list)でsetに変換できるという記事を読んだができなかったので、list→tuple→setに変換
bi_gram1 = [ tuple(item) for item in bi_gram1]
bi_gram2 = [ tuple(item) for item in bi_gram2]
X = set(bi_gram1)
Y = set(bi_gram2)
# 和集合
print(X | Y)
# 積集合
print(X & Y)
# 差集合
print(X - Y)
print("se is ", "" if ("s", "e") in X else "not ", "in X", sep="")
print("se is ", "" if ("s", "e") in Y else "not ", "in Y", sep="")

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

def template(x, y, z):
  return "{}時の{}は{}".format(x, y, z)

print(template(12, "気温", 22.4))

08. 暗号文

def cipher(text):
  result = []
  for c in text:
    if c.islower():
      result.append(chr(219 - ord(c)))
    else:
      result.append(c)
  return "".join(result)

message = "Apple Banana Chocolate"
encoded = cipher(message)
decoded = cipher(encoded)
print("暗号化:{}".format(encoded))
print("複合化:{}".format(decoded))

09. Typoglycemia

import random

def typoglycemia(text):
  l = []
  for w in text.split(" "):
    if len(w) <= 4:
      l.append(w)
    else:
      tmp = list(w[1:-1])
      l.append(w[0:1] + "".join(random.sample(tmp, len(tmp))) + w[-1])
  return " ".join(l)

  text = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."
print(typoglycemia(text))
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?