Skip to content

Instantly share code, notes, and snippets.

@m-note
m-note / Test.R
Last active August 29, 2015 14:23
Test for R
library(coefplot)
results <- fitsur$eq[1]
coefplot(results)
coefplot(fitsur)
summary(fitsur)$eq[1]
coefplot(fitsur$eq[[1]], title="Elections and Political Processes (AID110) and Civil\n Society (AID130) measured by Voice and Accountability",
coefficients=c("AID110","AID130","RAID110","RAID130", "DIF02"), newNames=c("AID110"="AID110(†)","RAID110"="RAID110(*)","DIF02"="DIF02"), zeroColor = "black", decreasing=T, intercept=F)
coefplot(fitsur$eq[[2]], title="Rule of Law measured by Rule of Law in WGI",
@m-note
m-note / Create Diet URL.py
Last active August 29, 2015 14:24
Create Diet URL
import urllib.request
import urllib.parse #参考: http://d.hatena.ne.jp/motie/20100728/1280335730
search = "startRecord=1&maximumRecords=5&any=アベノミクス&speaker=安倍晋三"
def DietSearch(search):
request = "http://kokkai.ndl.go.jp/api/1.0/speech?" + urllib.parse.quote_plus(search,encoding="utf-8")
@m-note
m-note / DietRecordv1.py
Created July 14, 2015 07:15
Get From Diet Record
import urllib.request
import urllib.parse #参考: http://d.hatena.ne.jp/motie/20100728/1280335730
search = "startRecord=1&maximumRecords=5&any=アベノミクス&speaker=安倍晋三"
def DietSearch(search):
request = "http://kokkai.ndl.go.jp/api/1.0/speech?" + urllib.parse.quote_plus(search,encoding="utf-8")
returnXML = ""
with urllib.request.urlopen(request) as page:
for line in page.readlines():
@m-note
m-note / DietRecodv2.py
Last active August 29, 2015 14:24
DietRecod
import httplib2
def DietSearch(search):
request = "http://kokkai.ndl.go.jp/api/1.0/speech?" + urllib.parse.quote_plus(search,encoding="utf-8")
h = httplib2.Http('.cache') #by httplib2
response, content = h.request(request)
#content = content.decode("utf-8") #etree.fromstringをするときに、"Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration."というエラーを回避するためにこの行は使わない
return content
@m-note
m-note / XMLextractSpeech.py
Created July 15, 2015 18:44
XMLextractSpeech
XMLcontent = DietSearch(search)
from lxml import etree
def XMLextractSpeech(XMLcontent):
root = etree.fromstring(XMLcontent) # 文字列からの読み込み
##第二階層の情報を取得##
numberOfRecords = root.find('numberOfRecords').text #リクエストされた検索条件に該当する総件数
numberOfReturn = root.find('numberOfReturn').text #当該リクエストで返戻した件数
startRecord = root.find('startRecord').text #当該リクエストの返戻結果に含まれる最初のレコードの総結果件数の中での出現位置
@m-note
m-note / recordDiet.py
Last active August 29, 2015 14:25
recordDiet
def recordDiet(speaker, startRec=1, any="", Dfrom="0000-01-01", Duntil="9999-12-31"):
search = searchKeyword(speaker, startRec, any, Dfrom, Duntil)
XMLcontent = DietSearch(search)
temp = XMLextractSpeech(XMLcontent)
newStart = int(temp[0])
extracted = temp[1]
while newStart != 0: #newStartが0じゃない、つまり先がある限り続ける
search = searchKeyword(speaker, newStart, any, Dfrom, Duntil)
XMLcontent = DietSearch(search)
@m-note
m-note / searchKeyword.py
Created July 15, 2015 18:47
searchKeyword
def searchKeyword(speaker, startRec=1, any="", Dfrom="0000-01-01", Duntil="9999-12-31"):
if len(any) > 0:
search = "startRecord=" + str(startRec) + "&" + "any=" + any + "&" + "speaker=" + speaker + "&from=" + Dfrom + "&until=" + Duntil
else:
search = "startRecord=" + str(startRec) + "&" + "speaker=" + speaker + "&from=" + Dfrom + "&until=" + Duntil
return search
library(WDI)
x <- WDI(country="all", indicator=c("AG.AGR.TRAC.NO"),
start=1980, end=2011)
y <- na.exclude(x[,3])
mean(y)
# 881532.3
sd(y)
# 2925623
var(y)
# recordDietの結果をPandasにまとめる
import pandas as pd
import os.path
def cleanText(text):
cleaned = text.replace('\n', ' ') # 改行記号を除く
cleaned = cleaned.strip() # 半角スペースを除く
cleaned = cleaned.replace(" ", "") # 全角スペースを除く
return cleaned
def resultsSave(ext_df, path="/Users/S/Desktop/", name="diet",type="csv"):
# 2種類の形式での保存を可能にする
if type=="csv":
save_path = os.path.join(path, (name + ".csv"))
ext_df.to_csv(save_path) # Excelでは開くと文字化けしたが、Google Spreadsheetで見る限り問題なし
elif type=="pandas":
save_path = os.path.join(path, (name + ".pkl"))
ext_df.to_pickle(save_path)
else:
print("File type you specified is not defined")