Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maruyama #33

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions chap_1/ex00.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
s="stressed"
print(s[::-1])
3 changes: 3 additions & 0 deletions chap_1/ex01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding:utf-8 -*-
s = "パタトクカシーー"
print(s[::2])
7 changes: 7 additions & 0 deletions chap_1/ex02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from itertools import chain
word1 = "パトカー"
word2 = "タクシー"

merged_word = ''.join(chain(*zip(word1, word2)))
print(merged_word)
9 changes: 9 additions & 0 deletions chap_1/ex03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
s = """Now I need a drink, alcoholic of course, after the heavy lectures
involving quantum mechanics."""
s = s.replace(",", "").replace(".", "")
s = s.split(' ')
print(s)

num = [len(s[a]) for a in range(15)]
print(num)
17 changes: 17 additions & 0 deletions chap_1/ex04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
s = """Hi He Lied because Boron Could Not Oxidize Fluorine. New Nations Might
Also Sign Peace Security Clause. Arthur King Can"""

s = s.replace(",", "").replace(".", "")
s = s.split(' ')
el = {}

indexs = [0, 4, 5, 6, 7, 8, 14 ,15 ,18]

for index, word in enumerate(s):
if index in indexs:
el[index+1] = word[0]
else:
el[index+1] = word[0]+word[1]

print(el)
17 changes: 17 additions & 0 deletions chap_1/ex05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
def ngram_str(words, n):
yield from zip(*(words[n:] for n in range(n)))


def ngram_word(words, n):
words = words.split(" ")
yield from zip(*(words[n:] for n in range(n)))


for num in ngram_str("I am a NLPer", 2):
print(num)

print("\n")

for num in ngram_word("I am a NLPer", 2):
print(num)
20 changes: 20 additions & 0 deletions chap_1/ex06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
def n_gram_str(s, n):
length = len(s)
return [s[i:i+n] for i in range(0, length, n)]


s1 = "paraparaparadise"
s2 = "paragraph"

X = set(n_gram_str(s1, 2))
Y = set(n_gram_str(s2, 2))

print("X = "+str(X))
print("Y = "+str(Y))

print("intersection = "+str(X & Y))
print("union = "+str(X | Y))
print("difference = "+str(X - Y))

print("se" in list(X | Y))
6 changes: 6 additions & 0 deletions chap_1/ex07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
def weather(x, y, z):
return("{}時の{}は{}".format(x, y, z))


print(weather("12", "気温", "22.4℃"))
7 changes: 7 additions & 0 deletions chap_1/ex08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
def cipher(s):
return "".join([chr(219-ord(c)) if c.islower() else c for c in s])

sentence = "abCdeFg"
print(cipher(sentence))
print(cipher(cipher(sentence)))
18 changes: 18 additions & 0 deletions chap_1/ex09.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding: utf-8
import random

def rand1(word):
if len(word) <= 4:
return word
m = list(word[1:-1])
random.shuffle(m)
return word[0] + "".join(m) + word[-1]



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

print(" ".join(map(rand1, sentence.split())))
24 changes: 24 additions & 0 deletions chap_2/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
高知県 江川崎 41 2013-08-12
埼玉県 熊谷 40.9 2007-08-16
岐阜県 多治見 40.9 2007-08-16
山形県 山形 40.8 1933-07-25
山梨県 甲府 40.7 2013-08-10
和歌山県 かつらぎ 40.6 1994-08-08
静岡県 天竜 40.6 1994-08-04
山梨県 勝沼 40.5 2013-08-10
埼玉県 越谷 40.4 2007-08-16
群馬県 館林 40.3 2007-08-16
群馬県 上里見 40.3 1998-07-04
愛知県 愛西 40.3 1994-08-05
千葉県 牛久 40.2 2004-07-20
静岡県 佐久間 40.2 2001-07-24
愛媛県 宇和島 40.2 1927-07-22
山形県 酒田 40.1 1978-08-03
岐阜県 美濃 40 2007-08-16
群馬県 前橋 40 2001-07-24
千葉県 茂原 39.9 2013-08-11
埼玉県 鳩山 39.9 1997-07-05
大阪府 豊中 39.9 1994-08-08
山梨県 大月 39.9 1990-07-19
山形県 鶴岡 39.9 1978-08-03
愛知県 名古屋 39.9 1942-08-02
7 changes: 7 additions & 0 deletions chap_2/un10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
line_number = 0
with open('input.txt') as input_file:
for line in input_file:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line_numberを使うよりもenumerateを使う

for num, line in enumerate(input_file):
    # hogehoge

line_number += 1

print("行数:"+str(line_number))
6 changes: 6 additions & 0 deletions chap_2/un11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
with open('input.txt') as input_file:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ネストが多くなるのは、できるだけ避ける
with構文の場合は, 複数の構文も下の様に書ける

with open('file1') as fp1, open('file2') as fp2:
   #hogehoge 

with open('un11_output.txt','w') as output_file:
for line in input_file:
line = line.replace("\t", " ")
output_file.write(line)
7 changes: 7 additions & 0 deletions chap_2/un12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
with open('input.txt') as input_file:
with open('col1.txt', 'w') as output1_file:
with open('col2.txt', 'w') as output2_file:
for line in iter(input_file.readlines()):
output1_file.writelines(line.split('\t')[0]+'\n')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3個以上になると \を使って改行しないといけないので ならcontextlib.ExitStackが個人的にはオススメ
ExitStack

output2_file.writelines(line.split('\t')[1]+'\n')