Created
December 10, 2012 05:55
-
-
Save miyanishi/4248684 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
### 言語処理100本ノック第一セット(1)~(6) ### | |
###モジュール宣言### | |
import re, pprint,codecs,sys | |
###辞書型オブジェクトを日本語で正しく表示する関数:pp### | |
def pp(obj): | |
pp = pprint.PrettyPrinter(indent=4, width=160) | |
str = pp.pformat(obj) | |
return re.sub(r"\\u([0-9a-f]{4})", lambda x: unichr(int("0x"+x.group(1), | |
16)), str) | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
sys.stdout=codecs.getwriter('utf_8')(sys.stdout)#←文字化けないように(無意味?) | |
###課題番号(1)~(3)### | |
out1=codecs.open("col1.txt","w","utf-8")#書き込みファイルcol1 | |
out2=codecs.open("col2.txt","w","utf-8")#書き込みファイルcol2 | |
count=0 | |
for line in codecs.open("address.txt", "r","utf-8"):#ファイルを一行ずつ読み込み | |
line_split=line.split("\t") | |
out1.write(line_split[0]+"\n")#(3)一列目をout1に | |
out2.write(line_split[1])#(3)二列目をout2に | |
substitution=line.replace("\t"," ")#(2)タブをスペースに置換 | |
print substitution | |
count+=1#(1)行数カウント | |
print u"行数=%d行" %count | |
out1.close() | |
out2.close() | |
###ここまで(1)~(3)### | |
### (4) (3)で作ったcol1とcol2を結合し、タブ区切りテキストを復元 ### | |
out3=codecs.open("restration.txt","w","utf-8")#書き込みファイル | |
in1=codecs.open("col1.txt","r","utf-8")#col1.txtファイルを読み込み | |
in2=codecs.open("col2.txt","r","utf-8")#col2.txtファイルを読み込み | |
while True:#※col1.txtの中身がなくなるまで | |
col1=in1.readline()#col1.txtを一行ずつ読む | |
col2=in2.readline()#col2.txtを一行ずつ読む | |
if not col1:#※の条件文 | |
break | |
col1=col1.rstrip()#改行文字を取り除く | |
col2=col2.rstrip() | |
combine=col1+"\t"+col2#結合させて元の形式に戻す | |
out3.write(combine+"\n")#書き込み | |
in1.close() | |
in2.close() | |
out3.close() | |
###ここまで(4)### | |
###コマンドライン引数Nを取得### | |
args=sys.argv | |
print args | |
args_int=int(args[1]) | |
i=1 | |
in1=codecs.open("address.txt", "r","utf-8") | |
###(5)ファイル先頭からN行出力 ※(6)を試すときはコメントアウト### | |
#while i <= args_int: | |
# print in1.readline() | |
# i=i+1; | |
###ここまで(5)### | |
###(6)ファイル末尾からN行出力 ※(5)を試すときはコメントアウト### | |
while i <=count: | |
line=in1.readline() | |
if i > count-args_int : | |
print line | |
i=i+1; | |
###ここまで(6)### | |
in1.close() | |
###(7)1コラム目の文字列の異なり数### | |
##LIST使用## | |
list1=[]#空のリスト作成 | |
for line in codecs.open("address.txt","r","utf-8"): | |
carm=line.split("\t") | |
print carm[0] | |
#print str(list1).decode("unicode-escape")#日本語を含むリストを出力する | |
if not carm[0] in list1:#要素が被っていなかったら | |
list1.append(carm[0])#リスト(list1)に追加 | |
print "list使用=%d" %len(list1) | |
##SET使用## | |
for line in codecs.open("address.txt","r","utf-8"): | |
carm=line.split("\t") | |
list1.append(carm[0])#リスト(list1)に追加 | |
set1=set(list1) | |
print "set使用=%d" %len(set1) | |
###ここまで(7)### | |
dic=[] | |
###リストを作成(8)~(9)で使用### | |
for line in codecs.open("address.txt","r","utf-8"): | |
carm=line.split("\t") | |
key=carm[0].rstrip() | |
value=carm[1].rstrip() | |
dic.append((carm[0],carm[1])) | |
###(8)各行を2コラム目の辞書順にソートしたもの### | |
for ken,yomi in sorted(dic,key=lambda x:x[1]): | |
print pp(ken+"\t"+yomi) | |
###ここまで(8)### | |
###(9)各行を2コラム目、1コラム目の優先度順位で辞書の逆順にソートしたもの### | |
##まずは2コラム目で逆順ソート## | |
for ken,yomi in sorted(dic,key=lambda x:x[1],reverse=True): | |
print pp(ken+"\t"+yomi) | |
##1コラム目で逆順ソート## | |
for ken,yomi in sorted(dic,key=lambda x:x[0],reverse=True): | |
print pp(ken+"\t"+yomi) | |
###ここまで(9)### | |
dic2={} | |
###(10)各行の2コラム目の文字列の出現頻度を求め,出現頻度の高い順に並べよ.### | |
###ただし、col2.txtを読み込むプログラムとして実装### | |
for line in codecs.open("col2.txt","r","utf-8"): | |
#col2を読み込み、単語をkey値、出現頻度をvalue値とした辞書を作る | |
key=line.rstrip() | |
if key in dic2.keys():#出現頻度をカウント | |
dic2[key]=dic2[key]+1 | |
else: | |
dic2[key]=1 | |
#出現頻度を用いてソートする | |
for key,value in sorted(dic2.items(),key=lambda x:x[1],reverse=True): | |
#昇順だと小さいものvalue値が小さいもの順にソートされるので、今回は降順にソート | |
print str(value)+":"+key | |
###ここまで(10)### | |
###第1セット終了### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment