-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from tmu-nlp/megumi
megumi_commit
- Loading branch information
Showing
39 changed files
with
14,521 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#00.文字列の逆順 | ||
text00='stressed' | ||
print(text00[::-1]) | ||
#スライスして逆順にする |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#01.パタトクカシーー | ||
text01='パタトクカシーー' | ||
print(text01[::2]) | ||
#スライスして一文字目から一つ飛ばしで取得 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#02.「パトカー」+「タクシー」=「パタトクカシー」 | ||
text02a='パトカー' | ||
text02b='タクシー' | ||
ans='' | ||
|
||
for i,j in zip(text02a, text02b): | ||
ans +=i | ||
ans +=j | ||
|
||
print(ans) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#03.円周率 | ||
text03='Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.' | ||
#句読点を除去 | ||
text03=text03.replace(",","").replace(".","") | ||
#単語を分割 | ||
text03=text03.split() | ||
print([len (i) for i in text03]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#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文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ. | ||
text04='Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.' | ||
text04=text04.replace(".","") | ||
text04=text04.split() | ||
|
||
num=[1,5,7,8,9,15,16,19] | ||
dict={} #空の辞書用意 | ||
|
||
for i,j in enumerate(text04): #enumerate()で各文字のインデックスと要素を取得 | ||
if i+1 in num: #保存してある番号と各文字列のインデックスインデックス+1を比較して、同じであれば一文字、それ以外は2文字に変換し、インデックス+1を要素にして辞書に追加 | ||
dict[j[0]]=i+1 | ||
else: | ||
dict[j[:2]]=i+1 | ||
print(dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#05.n_gram | ||
#与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.この関数を用い,”I am an NLPer”という文から単語bi-gram,文字bi-gramを得よ. | ||
|
||
text05="I am an NLPer" | ||
|
||
def ngram(n, word): | ||
list=[] | ||
for i in range(len(word) - n + 1):#len(word)-n+1回、文字列を1文字ずつずらしながらリストに追加 | ||
list.append(word[i:i+n]) | ||
return list | ||
|
||
print(f"単語bi-gram:{ngram(2,text05.split()}")) | ||
print(f"文字bi-gram:{ngram(2,text05)}") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#06.集合Permmalink | ||
#“paraparaparadise”と”paragraph”に含まれる文字bi-gramの集合を,それぞれ, XとYとして求め,XとYの和集合,積集合,差集合を求めよ.さらに,’se’というbi-gramがXおよびYに含まれるかどうかを調べよ. | ||
|
||
text6a="paraparaparradise" | ||
text6b="paragragh" | ||
|
||
def ngram(n, word): | ||
list=[] | ||
for i in range(len(word) - n + 1):#len(word)-n+1回、文字列を1文字ずつずらしながらリストに追加 | ||
list.append(word[i:i+n]) | ||
return list | ||
|
||
X = set(ngram(2, text6a))#set()関数(組み込み):集合オブジェクトに変換。集合オブジェクトに変換することで、集合演算子を使用できるようになる。 | ||
Y = set(ngram(2, text6b)) | ||
|
||
print(f"X:{X}") | ||
print(f"Y:{Y}") | ||
|
||
#和集合、積集合、差集合を求める。 | ||
print(f"和集合:{X|Y}") | ||
print(f"積集合:{X&Y}") | ||
print(F"差集合:{X-Y}") | ||
|
||
#"se"が含まれるかどうか判定 | ||
if "se" in X: | ||
print("seはYに含まれる") | ||
else: | ||
print("seはYに含まれない") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#07.テンプレートによる文生成 | ||
#引数x, y, zを受け取り「x時のyはz」という文字列を返す関数を実装せよ. | ||
#さらに,x=12, y=”気温”, z=22.4として,実行結果を確認せよ. | ||
|
||
def fun(x,y,z): | ||
print("{x}時の{y}は{z}") | ||
fun(12,"気温",22.4) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#08.暗号文 | ||
#与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ. | ||
#英小文字ならば(219 – 文字コード)の文字に置換,その他の文字はそのまま出力 | ||
#この関数を用い,英語のメッセージを暗号化・復号化せよ. | ||
|
||
text08="Yoshioka Megumi" | ||
|
||
def cipher(w): | ||
list=[] #空のリストを定義する | ||
for i in w: | ||
if i .islower(): #.islower()で小文字の判定を行う | ||
i=chr(219-ord(i)) #条件式がTrueの場合、ord()を使用して、小文字を文字コードに変換。219で引く。 | ||
list.append(i) #文字リストに追加していく | ||
return"".join(list) #join()でリストの要素を結合。 | ||
|
||
enc=cipher(text08) #複合化をするときは、再び関数をコールする必要がある。 | ||
|
||
print(f"暗号化:{enc}") | ||
print(f"複合化:{cipher(enc)}") | ||
|
||
#ord()関数(組み込み):文字列を文字コードに変換 | ||
#chr()関数(組み込み):文字コードを文字列に変換 | ||
#str.islower()メソッド:小文字判定を行う。文字列が全て小文字であれば、Trueを返す | ||
#文字コードを219で引いて、文字列に変換すると、元の小文字(英字)からアルファベット順が逆の小文字(英字)が出力される。 | ||
#a→97,b→98,…z→122 | ||
#219-97=122→z,219-98=121→y… |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#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 | ||
|
||
text09="I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ." | ||
|
||
list=[] | ||
for i in text09.split(): | ||
if len (i)>4: | ||
i=i[0]+"".join(random.sample(i[1:-1], len(i)-2))+i[-1] | ||
list.append(i) | ||
|
||
print(" ".join(list)) |
Oops, something went wrong.