セット3

セット3です。

 

(1)標準入力から英語のテキストを読み込み,ピリオドを文の区切りと見なし,1行1文の形式で標準出力に書き出す

f = open("medsamp2012h.txt","r")
data = f.read()

t=re.sub("\.",'.\n',data)
print(t)
f.close()

re.subでピリオドを改行(\n)に変える。

 

 

(2)標準入力から英語のテキストを読み込み,ピリオド→スペース→大文字を文の区切りと見なし,1行1文の形式で標準出力に書き出せ

#!usr/bin/env python3
# -*- coding: utf-8 -*-

import re

f = open("medsamp2012h.txt","r")
data = f.read()

t=re.sub("\. ([A-Z])",'.\n\\1',data)
print(t)
f.close()

re.subでピリオド→スペース→大文字の英語の正規表現(A-Z)を改行(\n)と\1でグループの正規表現を呼び出せるのでそれに変える。

 

 

(3) (2)の出力を標準入力から1行(1文)を読み込む毎に,スペースで単語列に分割し,1行1単語形式で標準出力に書き出せ。文が終わる毎に空行を出力

#!usr/bin/env python3
# -*- coding: utf-8 -*-

import re

f = open("medsamp2012h.txt","r")
data = f.read()

t=re.sub("\. ([A-Z])",'.\n\\1',data)

s=re.sub(" ",'\n',t)
u=re.sub("\.",'.\n',s)

print(u)

f.close()

(2)のやつにre.subでスペースを改行(\n)に入れ替え,ピリオドをピリオドと改行に入れ替えます。

(4) (3)のプログラムを修正し,各トークンの末尾が記号で終わる場合は,その記号を別のトークンとして分離せよ.

#!usr/bin/env python3
# -*- coding: utf-8 -*-

import re

f = open("medsamp2012h.txt","r")
data = f.read()

t=re.sub("\. ([A-Z])",'.\n\\1',data)
s=re.sub(" ",'\n',t)
u=re.sub('([^a-zA-Z0-9^n])\n','\n\\1\n',s)
t=re.sub('([\.])\n','\\1\n\n',u)
print(t)

f.close()

 

 英語か数字でないものが記号ということで(2)のやつにre.subで[^a-zA-Z0-9^n]と書き表示させました。

(5)(24)の出力を標準入力から1行(1単語)を読み込む毎に,その単語を小文字に変換した文字列を各行の最終列にタブ区切り形式で追加し,標準出力に書き出す

#!usr/bin/env python3
# -*- coding: utf-8 -*-

import re

f = open("medsamp2012h.txt","r")
data = f.read()

t=re.sub("\. ([A-Z])",'.\n\\1',data)
s=re.sub(" ",'\n',t)
u=re.sub('([^a-zA-Z0-9])\n','\n\\1\n',s)
t=re.sub('([\.])\n','\\1\n\n',u)

a=t.lower()

print(a)

f.close()

 

文字列を小文字に変換するlower()を使いました。

 

(6)-nessと-lyの両方の派生語尾をとる単語をすべて抜き出せ

#!usr/bin/env python3
# -*- coding: utf-8 -*-

import re

f = open("medsamp2012h.txt","r")
data = f.read()

t=re.sub("\. ([A-Z])",'.\n\\1',data)
s=re.sub(" ",'\n',t)
u=re.sub('([^a-zA-Z0-9])\n','\n\\1\n',s)
t=re.sub('([\.])\n','\\1\n\n',u)
a=t.lower()

nau=re.compile("([a-z]+?)ness")
kau=re.compile("([a-z]+?)ly")
    
am=kau.findall(a)
km=nau.findall(a)

dict1 = {}
dict2 = {}

if(km and am):
    for j in range(len(km)):
            dict1[km[j]]= 1

if(km and am):
    for j in range(len(am)):
            dict2[am[j]]= 1

for key in dict1.keys():
    if (dict2.get(key,False)) :
        print(key)

f.close()

 辞書オブジェクトを2つlyとnessを作り、両方ある方を表示させてます。

辞書に一つひとつに1をキーの値にしています。

getは該当するものがなかった時の値を設定できFalseにしています。

 

 

(7)セット1の(10)のプログラムを呼び出すことで,頻度の高い英単語トップ100(単語と頻度がソートされたもの)を作成

#!usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re

f = open("medsamp2012h.txt","r")
data = f.read()

t=re.sub("\. ([A-Z])",'.\n\\1',data)
s=re.sub(" ",'\n',t)
u=re.sub('([^a-zA-Z0-9])\n','\n\\1\n',s)
t=re.sub('([\.])\n','\\1\n\n',u)
a=t.lower()

f = open('col2.txt','w')

f.write(a)
f.close

os.system("python3 test10.py")

h = open("aaa.txt","r")
ta = h.read()

print(ta)

f.close()

os.systemでサブシェル内でコマンドを実行できますのでセット1の(10)のファイルを実行。

(10)の中でf = open('aaa.txt','w') とf.write(word+'\n')を足しておいて、また読み込んで表示させます。

(8)各単語から文字バイグラムを抽出するプログラムを実装せよ.また,(27)と同様の方法で,頻度の高い文字バイグラムトップ100(バイグラムと頻度がソートされたもの)を作成する

#!usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re

f = open("medsamp2012h.txt","r")
data = f.read()

t=re.sub("\. ([A-Z])",'.\n\\1',data)
s=re.sub(" ",'\n',t)
u=re.sub('([^a-zA-Z0-9])\n','\n\\1\n',s)
t=re.sub('([\.])\n','\\1\n\n',u)
nau=re.compile("([\s\S]*?\n)")
a=t.lower()

tm=nau.findall(a)

print(tm)
d = open('col2.txt','w')
for i in range(len(tm)):
    if(len(tm[i])>=2):
        km=tm[i].strip("\n")
        for j in range(len(km)-1):
            d.write(km[j:j+2])
            d.write("\n")
    d.write(" ")
d.close

os.system("python3 test10.py")
h = open("aaa.txt","r")
ta = h.read()
print(ta)

f.close()

単語数-1の数の分文字バイグラムがあるので、range(len(km)-1)でループさせます。

km[j:j+2]でjからj+2までということで2単語ずつ表すことができます。

 

(10) (5)の出力を標準入力から読み込み,stemming.porter2を用いて語幹(ステム)を最終列に追加し,medline.txt.sent.tok.stemというファイルに保存せよ.

#!usr/bin/env python3
# -*- coding: utf-8 -*-

import re
from nltk.stem.lancaster import LancasterStemmer

f = open("medsamp2012h.txt","r")
data = f.read()

t=re.sub("\. ([A-Z])",'.\n\\1',data)
s=re.sub(" ",'\n',t)
u=re.sub('([^a-zA-Z0-9])\n','\n\\1\n',s)
t=re.sub('([\.])\n','\\1\n\n',u)
nau=re.compile("([\s\S]*?\n)")

a=t.lower()
tm=nau.findall(a)

d=open("medline.txt.sent.tok.stem","w")

for i in range(len(tm)):
    a_stem=LancasterStemmer().stem(tm[i].strip())
    print(tm[i].strip()+"\t"+a_stem, file=d)

d.close()
f.close()

 語の語幹を取り出す処理のことをステミングっていうらしい。

 LancasterStemmer().stem()で語幹を抽出できます。