授業概要¶
- Pythonの基礎的な技術を習得する。
- 自然言語処理、言語学の初歩的な知識を身に付ける。
- Pythonを用いて学習者言語を分析する。
- プログラミングの技術も、自然言語処理、言語学、第二言語習得の知識も必要ありません。
以下の2つの文は同じ状況を説明したものです。
- A student hurriedly entered the classroom, clearly flustered and out of breath, indicating that he was running late for the class.
- A student came into the room with a big bag on his back. He looked really tired and everyone looked at him. He sat down at his desk.
1の文と2の文の違いはなんでしょうか。
AとBではどちらが高い評価を受けるでしょう?
A
Education problem I would like to talk about 'school education'. As a university student, I study now. And I studied very hard to pass the entrance examination. I spent such time without feeling that there is a problem. But some professionals tell that today's education system lends us bad future. That is why; I am interested in education problem. I study 'education' in the common education now. In that class, my teacher gives me many ideas. They are new for me. For example, we must take the entrance examination to enter the university and high school. Some students take the entrance examination to enter the junior high school. Such system may have to be changed. The teacher said 'If university are changed, high school, junior high school and elementary school. Or, to change university, there is a strategy that we change high school, junior high school and elementary school. But high school or junior high school have entrance examinations. That is why; elementary school seems to be changed. I think that there is not an answer in the education. But thinking that time, students will be taught in needs. There seems to be many things to change.
B
The characteristics of Japanese Education system in Tertiary level. The Japanese education system has some significant characteristics which vary from education system in other countries. To compare between Japanese educational system and American educational system, the author discuss the characteristics of each countries. Before, the author limits the educational level to tertiary education for this essay. To begin with, the Japanese educational system can be explained with two perspectives. First of all, the entrance and graduation of university in Japanese system is different. Regarding the entrance of the university, it require much effort for entering to the universities than the universities in America. Here, it means the high school students have to suffer from studying to pass the entrance exam with the preparation of almost 2 years. For passing the exams, these days, students in high school go to academies, or cram schools for supplementing the studies. Of course, their parents pay extra investment for this kind of tuition in return of their expectation for entering to high-level universities.
A
- 文の数: 20
- 単語の数: 230
- 1文あたりの単語の数: 11.5
B
- 文の数: 30
- 単語の数: 691
- 1文あたりの単語の数: 22.3
1.1 数値演算1¶
# プログラミング言語は、人間が命令を書いて、コンピュータがその命令を評価します(出力もします)。
# 以下の例では"12 + 12"という命令を人間が書いてPythonがそれを評価し、出力しています。
# "12 + 12"と入力し、Shift+enterしてみましょう。
12 + 12
24
Pythonでは以下の数学演算子が使えます(他にもあります)。
優先順位 | 演算子 | 説明 | 例 | 評価の値 |
---|---|---|---|---|
1 | ** | 累乗 | 2 ** 3 | 8 |
2 | * | 掛け算 | 3 * 5 | 15 |
3 | / | 割り算 | 22 / 8 | 2.75 |
4 | // | 整数の割り算 | 22 / 8 | 2 |
5 | % | 剰余 | 22 % 8 | 6 |
6 | + | 足し算 | 12 + 12 | 24 |
4 | - | 引き算 | 12 - 2 | 10 |
1.2 文法エラー¶
# 以下のように命令を間違うとPythonは命令を評価できず、エラーを出力します。
# 間違いがありそうなところが"^"で示されます。
# "SyntaxError:"の後ろにエラーの種類が示されます。
12 /
Cell In[2], line 5 12 / ^ SyntaxError: invalid syntax
1.3 変数への代入¶
# 以下の文はaという変数に3という値を代入しています。
a = 3
# a = 3を実行したあとに以下のコマンドを入力すると"a"に代入された値が出力されます。
a
3
# 変数同士の演算も可能です。
# 以下の例ではaに3を、bに7を、a * bの結果をcに代入し
# cを出力しています。
a = 5
b = 7
c = a * b
c
35
# もちろん変数には文字列も代入できます。
# 文字列は""で囲みます。
a = "good" # a = goodはダメ!
a
'good'
# 文字列は以下のように連結ができます。
a = "Robert"
b = "Plant"
a + b
'RobertPlant'
# 文字列は以下のように複製ができます。
a * 3
'RobertRobertRobert'
# ここまで暗示的でしたが変数は上書きされます。
a = 3 # aに3が代入される。
a = "good" #上書きされて3はどこかに行って、aの中身は"good"になる
a
'good'
正しい変数名と正しくない変数名
正しい変数名 | 正しくない変数名| -----------|-------------| good | good-thing (ハイフンは使えない) goodThing | good thing (スペースは使えない) good_thing | 2thing (数字から始めてはいけない) _good | 22 (もちろん、数字だけもダメ) GOOD | good_$ing (特殊な記号は使えない
1.4 数値の演算2¶
a = 12
b = 3
# 割り算
a/b
4.0
# 剰余
a % b
0
# 結果を変数に代入
ab = a * b
# べき乗
3**2
9
# aにbを足し入れる
a += b
# 掛け算の結果も代入できる
ab = a*b
1.5 オブジェクト(変数)のタイプ(型)¶
# ここまで出てきた変数の型は以下の3つ
a = 3 # int(整数)型
type(a)
int
b = 3.141592
type(b)
float
c = "good"
type(c)
str
- 小数点以下の値がある場合とない場合で、同じ数値でもない場合はint型(整数)、ある場合はfloat型(浮動小数)
1.6 関数とメソッド¶
# 関数
# 小数点以下を切り捨てる関数は
a = 2.5
round(a)
# 命令文と考えたら、roundが命令(動詞)で()内に目的語を書く感じ。
# 「aの小数点以下を切り捨てろ」みたいな。
# ()内に「目的語」の後ろに「どうやって」を表す副詞みたいな表現が入るときもある。
2
# メソッド
# 関数と同じ命令だけど、「文法」が少し違う」
# 大文字を小文字にするメソッドは
a = "I LOVE YOU MORE THAN ANYTHING IN THE WORLD."
a.lower()
# .(ドット)の前が「目的語」でその後ろに命令(動詞)を書く。
# ()の中に「どうやって」を表す副詞みたいな表現が入るときもある。
'i love you more than anything in the world.'
1.7 print()関数¶
- jupyter notebookでは変数名のみでも出力されますが、明示的に出力する場合はprint()関数を使用します。
a = "みかんは"
b = "円です。"
x = 100
print(a,x,b)
みかんは 100 円です。
1.8 リスト¶
# 変数(オブジェクト)を要素として持つデータ構造。順番を保持する。
# 文字列のリスト
L = ["I'm","your","father","."]
# 整数のリスト
M = [2,3,4,5]
# 混在させることも可能
N = [2,"apple",2.4,55]
# リストを要素として持つリスト
P = [[2,3],[4,5],[6,7]]
1.9 リストの操作¶
Q = ["A","B","C","D"]
# Qの末尾に"E"を追加 -> ['A', 'B', 'C', 'D', 'E']
Q.append("E")
# リストの末尾にリストを追加 -> ['A', 'B', 'C', 'D', 'E', 'F', 'G']
Q.extend(['F', 'G'])
# リストの連結 -> ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
Q + ['H','I']
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
# 掛け算もできる。 -> ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C']
['A', 'B', 'C'] * 3
# インデクスを指定して参照することができる。
# 要素の順番を示す数字は0から始まる。
Q[0]
# 後ろから数えるときは-(マイナス)
Q[-1] #リストQの一番後ろの要素
# インデクスを指定して要素を削除
del Q[0] # "A"が削除される
# 重複の削除(でも順番は保持されない)
S = ["A","A","B","B","C"]
list(set(S))
['C', 'B', 'A']
# 長さ(個数)を取得
L = [1,2,3,4,5]
len(L)
# 結合
M = ["May","the","force","be","with","you"]
" ".join(M)
'May the force be with you'
# 要素の有無
L = [1,2,3,4,5]
i = 3
i in L
True
# 個数のカウント
L = ["a","b","a","a","b","c"]
L.count("c")
1
# データの総和
L = [12,25,32,65,44,89,35]
sum(L)
302
# 並べ替え
L = ["Edano","Ueda","Ota","Abe","Iida"]
#非破壊
sorted(L)
#破壊
L.sort()
# range()
# 0から5より小さい整数を生成
L = list(range(5))
1.10 リストの要素とアクセス¶
# 4つの文字を要素としてもつリスト
L = ["A","B","C","D"]
# 先頭の要素へ
L[0]
'A'
# 負の値は「後ろから」という意味
L[-1]
'D'
# 範囲を指定して複数の要素にアクセス
L[0:2]
['A', 'B']
範囲指定の値が示す場所
# リストを要素としてもつリストの場合
P = [[2,3],[4,5],[6,7]]
# 先頭のリストの先頭の要素へアクセス
P[0][0]
2
1.11 文字列はリスト¶
a = "Hello"
# 先頭の文字
a[0]
# 最後の文字
a[-1]
# 掛け算
a * 3
'HelloHelloHello'
1.12 文字列の操作(その他)¶
# 足し算
s1 = "py"
s2 = "thon"
s1 + s2
'python'
# 置換
s3 = "I got three apples."
s3.replace("three","five")
'I got five apples.'
# 分割
s4 = "Use the force"
s4.split(" ")
['Use', 'the', 'force']
# スライス
s5 = "ambidextrous"
s5[4:8]
'dext'
1.13 Jaccard係数¶
- 2つの集合の類似度を計算する方法。
- 以下の式を用いて文と文の類似度を計算することができる。
$$ Jaccard(A,B) = \frac{A \cap B}{A \cup B} $$
- $A \cap B$はAとBで共通する単語の個数(積集合)、$A \cup B$はAとBの単語の総和(和集合)。
A = "The adventurous boy decided to climb the tall tree in his backyard, despite his mother's warnings."
B = "The boy's determination to climb the tree was evident as he gripped the branches tightly, using his agile limbs to navigate his way up, fueled by his sense of adventure and curiosity."
Asplit = set(A.split(" "))
Bsplit = set(B.split(" "))
inter_set = Asplit.intersection(Bsplit)
union_set = Asplit.union(B)
len(inter_set) / len(union_set)
0.14634146341463414
練習問題¶
問題1
12 + 22 - (3 × 7) ÷ 2
問題2
sent = ["Wars","not","make","one","great"]を文字列に変換して出力する。
問題3
問題2で文字列に変換したものをリストに変換する。
問題4
a = "Use the force."とb="Feel it."を結合して"Use the force. Feel it."にする。
問題5
"You don't know the power of the darkside."の語数を出力する。
問題6
"But you can't stop the change, any more than you can stop the suns from setting."の単語の種類数を出力する。
問題7
以下のデータの平均値(データの総和を個数で割る)を求めましょう。
L = [12,25,32,65,44,89,35]
問題8
以下のリストの"C"を出力しなさい。
M = [["A","B",["C","D"],"E"],"F",["G","H"],"I"]
問題9
以下のリストからA,B,C,Dを出力しなさい。
M = [["A","B",["C","D"],"E"],"F",["G","H"],"I"]
問題10
以下の文、XとYのJaccard係数を求めなさい。
X = "The mischievous dog and curious cat had a playful chase on the soft carpet, leaving tufts of fur flying in their wake. As the cat leapt onto the windowsill, the dog skidded to a stop, lapping up the spilled milk from a knocked-over glass, adding to the chaos of their impromptu game."
Y = "The cat, with its tail fluffed up in excitement, eagerly lapped at the bowl of milk on the carpet while the dog watched on, wagging its tail in anticipation, hoping for a share of the treat."