다운로드
http://git-scm.com/download/win
path에 설치폴더의 바이너리 경로를 등록
명령프롬프트에서 실행하면 된다
2014년 5월 30일 금요일
perl mechanize example
use LWP::UserAgent;
use autodie qw/ open close /;
use WWW::Mechanize;
use LWP::Simple qw($ua get);
use Encode;
$ua->agent('My agent/1.0');
# create WWW::Mechanize object
# autocheck 1 checks each request to ensure it was successful
my $mech = WWW::Mechanize->new();
$filename = "ilbe.txt";
if(-e $filename)
{
unlink($filename);
}
$mech->get("http://www.ilbe.com/ilbe");
@link = $mech->find_all_links( url_regex => qr/\d{10}/);
for $new_link (@link){
$url = $new_link->url_abs();
$no = $url;
$no =~ m/\d{10}/;
$no = $&;
$mech->get($url);
$cont = $mech->content();
print $no." article is extracted...\n";
@files=();
# paste from url of webbrowser manually later modify
#@temp = split(/og:url/, $cont);
#@temp = split(/science&no=/, $temp[1]);
#@temp = split(/&page/, $temp[1]);
#$no = $temp[0];
$cont=encode("euc-kr", $cont);
@temp = split(/author/, $cont);
@temp = split(/<\/div>/, $temp[1]);
@temp = split(/<\/div>/, $temp[0]);
@temp = split(/margin-right:3px;/, $temp[0]);
$name = $temp[1];
$name =~ s/"//;
$name =~ s/\/>//;
$name =~ s/^\s+//;
@temp = split(/<\/span>/, $name);
$name =~ s/\s+$//;
$name = $temp[0];
#print $name;
@temp = split(/기사 제목/, $cont);
@temp = split(/-->/, $temp[1]);
@temp = split(/<!--/, $temp[1]);
$subject = $temp[0];
print $subject;
@temp = split(/기사 본문/, $cont);
@temp = split(/serverLog/, $temp[1]);
$memo = $temp[0];
$memo =~ s/-->//;
$memo =~ s/^\s+//;
#print $memo;
@img = $mech->find_all_links( text_regex => qr/png/i );
$i = 0;
for my $img_url (@img) {
$i++;
print $img_url->url_abs;
$mech->mirror($img_url->url_abs, $no."-".$i.".png");
push (@files, $no."-".$i.".png");
}
@img = $mech->find_all_links( text_regex => qr/jpg/i );
for my $img_url (@img) {
$i++;
print $img_url->url_abs;
$mech->mirror($img_url->url_abs, $no."-".$i.".jpg");
push (@files, $no."-".$i.".jpg");
#Opens FROM_DIR, ao I can read from it
}
$attached = "file:";
for ($i=0; $i<=$#files; $i++){
$attached = $attached.$files[$i]." ";
}
$name = "name: ".$name;
$subject = "subject: ".$subject;
$memo = "memo: ".$memo;
open MYFILE, ">>$filename" or die ;
print MYFILE "$no";
print MYFILE "------------------------------------------------------------------------------\n";
print MYFILE "$name\n";
print MYFILE "$subject\n";
print MYFILE "$memo\n";
print MYFILE "$attached\n";
}
use autodie qw/ open close /;
use WWW::Mechanize;
use LWP::Simple qw($ua get);
use Encode;
$ua->agent('My agent/1.0');
# create WWW::Mechanize object
# autocheck 1 checks each request to ensure it was successful
my $mech = WWW::Mechanize->new();
$filename = "ilbe.txt";
if(-e $filename)
{
unlink($filename);
}
$mech->get("http://www.ilbe.com/ilbe");
@link = $mech->find_all_links( url_regex => qr/\d{10}/);
for $new_link (@link){
$url = $new_link->url_abs();
$no = $url;
$no =~ m/\d{10}/;
$no = $&;
$mech->get($url);
$cont = $mech->content();
print $no." article is extracted...\n";
@files=();
# paste from url of webbrowser manually later modify
#@temp = split(/og:url/, $cont);
#@temp = split(/science&no=/, $temp[1]);
#@temp = split(/&page/, $temp[1]);
#$no = $temp[0];
$cont=encode("euc-kr", $cont);
@temp = split(/author/, $cont);
@temp = split(/<\/div>/, $temp[1]);
@temp = split(/<\/div>/, $temp[0]);
@temp = split(/margin-right:3px;/, $temp[0]);
$name = $temp[1];
$name =~ s/"//;
$name =~ s/\/>//;
$name =~ s/^\s+//;
@temp = split(/<\/span>/, $name);
$name =~ s/\s+$//;
$name = $temp[0];
#print $name;
@temp = split(/기사 제목/, $cont);
@temp = split(/-->/, $temp[1]);
@temp = split(/<!--/, $temp[1]);
$subject = $temp[0];
print $subject;
@temp = split(/기사 본문/, $cont);
@temp = split(/serverLog/, $temp[1]);
$memo = $temp[0];
$memo =~ s/-->//;
$memo =~ s/^\s+//;
#print $memo;
@img = $mech->find_all_links( text_regex => qr/png/i );
$i = 0;
for my $img_url (@img) {
$i++;
print $img_url->url_abs;
$mech->mirror($img_url->url_abs, $no."-".$i.".png");
push (@files, $no."-".$i.".png");
}
@img = $mech->find_all_links( text_regex => qr/jpg/i );
for my $img_url (@img) {
$i++;
print $img_url->url_abs;
$mech->mirror($img_url->url_abs, $no."-".$i.".jpg");
push (@files, $no."-".$i.".jpg");
#Opens FROM_DIR, ao I can read from it
}
$attached = "file:";
for ($i=0; $i<=$#files; $i++){
$attached = $attached.$files[$i]." ";
}
$name = "name: ".$name;
$subject = "subject: ".$subject;
$memo = "memo: ".$memo;
open MYFILE, ">>$filename" or die ;
print MYFILE "$no";
print MYFILE "------------------------------------------------------------------------------\n";
print MYFILE "$name\n";
print MYFILE "$subject\n";
print MYFILE "$memo\n";
print MYFILE "$attached\n";
}
2014년 5월 23일 금요일
펄 모듈 설치 모듈이 서브 타입으로 되어 있을 때
모듈이름이
WWW::Mechanize::Firefox
다 써주어야 한다.
cpan install WWW::Mechanize::Firefox
대소문자도 구분하니까 유의
WWW::Mechanize::Firefox
다 써주어야 한다.
cpan install WWW::Mechanize::Firefox
대소문자도 구분하니까 유의
2014년 5월 8일 목요일
하위 폴더의 모든 파일에서 특정 문자열을 검색하는 펄스크립트
use strict;
use warnings;
use Cwd;
use File::Find;
my $search_pattern=$ARGV[0];
my $file_pattern =$ARGV[1];
if (@ARGV <2){
print "Useage: $0 [string to search] [file type]\n";
exit 1;
}
find(\&d, cwd);
sub d {
my $file = $File::Find::name;
$file =~ s,/,\\,g;
return unless -f $file;
return unless $file =~ /$file_pattern/;
open F, $file or print "couldn't open $file\n" && return;
while (<F>) {
if (my ($found) = m/($search_pattern)/o) {
print "found $found in $file\n";
last;
}
}
close F;
}
use warnings;
use Cwd;
use File::Find;
my $search_pattern=$ARGV[0];
my $file_pattern =$ARGV[1];
if (@ARGV <2){
print "Useage: $0 [string to search] [file type]\n";
exit 1;
}
find(\&d, cwd);
sub d {
my $file = $File::Find::name;
$file =~ s,/,\\,g;
return unless -f $file;
return unless $file =~ /$file_pattern/;
open F, $file or print "couldn't open $file\n" && return;
while (<F>) {
if (my ($found) = m/($search_pattern)/o) {
print "found $found in $file\n";
last;
}
}
close F;
}
2014년 4월 14일 월요일
2014년 4월 12일 토요일
마르코브
library(stringr)
setwd("C:/rdata")
source("DTMC.R")
###here kospi data
dat <- read.csv('port3.csv', header=TRUE, na="")
odat<-dat[c(1:nrow(dat)),]
attach(odat)
x<-odat[order(일자),]
price <- as.numeric(str_replace_all(x$리홈쿠첸, ",", ""))
d <- diff(price)
up2up <-0
up2down <-0
down2up <-0
down2down <-0
for (i in 2:length(d)){
if (d[i-1]> 0 && d[i]>0){
up2up <- up2up+1
}
if (d[i-1]> 0 && d[i]<0){
up2down <- up2down +1
}
if (d[i-1]< 0 && d[i]>0){
down2up <- down2up+1
}
if (d[i-1]< 0 && d[i]<0){
down2down <- down2down+1
}
}
u2u <- up2up/(up2up+up2down)
u2d <- up2down/(up2up+up2down)
d2u <- down2up/(down2up+down2down)
d2d <- down2down/(down2up+down2down)
mattrans <- matrix(c(u2u, d2u, u2d, d2d),nrow=2,ncol=2)
colnames(mattrans) <- c("U","D")
row.names(mattrans) <- c("U","D")
rm(temp)
rm(sim)
temp<-0
sim <-data.frame(vector())
#now set states
state<- c(1,2,3,4,5)
nstate<-c(8000,9000,10000,11000,12000)
#current state
temp[1]<- 5
for (i in 1:100){
ins<- simula(mattrans,10)
for (j in 2:20){
if (ins[j] == "U"){
temp[j] <- temp[j-1]+1
}
else { temp[j] <- temp[j-1]-1}
}
sim <-rbind(sim, temp)
}
p1<-sum(rowSums(sim <=1))/(10*100)
p2<-sum(rowSums(sim == 2))/(10*100)
p3<-sum(rowSums(sim == 3))/(10*100)
p4<-sum(rowSums(sim == 4))/(10*100)
p5<-sum(rowSums(sim >= 5))/(10*100)
p <- c(p1,p2,p3,p4,p5)
cat("tansition matex is:\n")
matrix(mattrans, nrow = 2,ncol = 2)
f <- data.frame(nstate, p)
colnames(f)<-c("주가", "확률")
f[order(-f[,1]),]
cat("current state is:",temp[1], sep="\n")
setwd("C:/rdata")
source("DTMC.R")
###here kospi data
dat <- read.csv('port3.csv', header=TRUE, na="")
odat<-dat[c(1:nrow(dat)),]
attach(odat)
x<-odat[order(일자),]
price <- as.numeric(str_replace_all(x$리홈쿠첸, ",", ""))
d <- diff(price)
up2up <-0
up2down <-0
down2up <-0
down2down <-0
for (i in 2:length(d)){
if (d[i-1]> 0 && d[i]>0){
up2up <- up2up+1
}
if (d[i-1]> 0 && d[i]<0){
up2down <- up2down +1
}
if (d[i-1]< 0 && d[i]>0){
down2up <- down2up+1
}
if (d[i-1]< 0 && d[i]<0){
down2down <- down2down+1
}
}
u2u <- up2up/(up2up+up2down)
u2d <- up2down/(up2up+up2down)
d2u <- down2up/(down2up+down2down)
d2d <- down2down/(down2up+down2down)
mattrans <- matrix(c(u2u, d2u, u2d, d2d),nrow=2,ncol=2)
colnames(mattrans) <- c("U","D")
row.names(mattrans) <- c("U","D")
rm(temp)
rm(sim)
temp<-0
sim <-data.frame(vector())
#now set states
state<- c(1,2,3,4,5)
nstate<-c(8000,9000,10000,11000,12000)
#current state
temp[1]<- 5
for (i in 1:100){
ins<- simula(mattrans,10)
for (j in 2:20){
if (ins[j] == "U"){
temp[j] <- temp[j-1]+1
}
else { temp[j] <- temp[j-1]-1}
}
sim <-rbind(sim, temp)
}
p1<-sum(rowSums(sim <=1))/(10*100)
p2<-sum(rowSums(sim == 2))/(10*100)
p3<-sum(rowSums(sim == 3))/(10*100)
p4<-sum(rowSums(sim == 4))/(10*100)
p5<-sum(rowSums(sim >= 5))/(10*100)
p <- c(p1,p2,p3,p4,p5)
cat("tansition matex is:\n")
matrix(mattrans, nrow = 2,ncol = 2)
f <- data.frame(nstate, p)
colnames(f)<-c("주가", "확률")
f[order(-f[,1]),]
cat("current state is:",temp[1], sep="\n")
2014년 4월 11일 금요일
markov chain for stock price
## NAME: markovchain.py
##
## USAGE: From shell prompt: python -i markovchain.py
## or within interactive python environment, scrape and paste
## REQUIRED ARGUMENTS: none
##
## OPTIONS: none
## DESCRIPTION: Simulation of the cash management model as a Markov chain
## DIAGNOSTICS: None
## CONFIGURATION AND ENVIRONMENT: None
## DEPENDENCIES: None
## INCOMPATIBILITIES: None known
## PROVENANCE: Tue Aug 28, 2012 5:38 AM
## BUGS AND LIMITATIONS: None Known
## FEATURES AND POTENTIAL IMPROVEMENTS:
## For the innermost for-loop, you can simply use
## newState = scipy.random.choice(1, 1, prob=T[state,])
## That shows more clearly what's going on
## On the other hand, if other languages don't have a function comparable to sample, then
## the current formulation is easier to adapt and make the algorithms similar.
## AUTHOR: Steve Dunbar
## VERSION: 1.0 Tue Aug 28, 2012 5:38 AM
## 1.1 added some ouput formatting Mon Nov 12, 2012 6:03 AM
## KEYWORDS: Markov chain, simulation, reflecting boundary
#! /usr/bin/env python
import scipy
n = 6; # Top boundary, number of states 0..n is n+1
s = 4; # Start and Reset state number 1 <= s <= n-1
p = 0.8;
steps = 1000;
T = scipy.diag( p*scipy.ones( n ), 1) + scipy.diag( (1-p)*scipy.ones( n ), -1);
T[0,1] = 0; T[0,s] = 1;
T[n,n-1] = 0; T[n,s] = 1;
# vector to hold the count of visits to each state during a cycle
count = scipy.zeros(n+1, float);
# Initialize the cyclelength
numberCycles = 0;
# Start in the state s
state = s;
# Make steps through the markov chain
for i in range(1, steps+1):
x = 0;
u = scipy.random.random(1);
newState = state;
for j in range(n+1):
x = x + T[state, j];
if (x >= u):
newState = j;
break;
## newState = scipy.random.choice(1, 1, prob=T[state,])
state = newState;
count[state] = count[state] + 1;
if (state == n or state == 0):
numberCycles = numberCycles + 1;
avgVisits = count/numberCycles;
avgCycleLength = i/numberCycles;
Wsk = avgVisits
print Wsk
theoreticalWsk = 2*( s*(1-scipy.arange(0,n+1,dtype=float)/n) - scipy.maximum(s-scipy.arange(0,n+1), scipy.zeros(n+1, int) ) )
prob = [0] * (n+1) # initialize probility as list data type
for i in range(1, n+1):
prob[i] = Wsk[i]/sum(Wsk);
price = [2000, 2900, 3500, 4500, 5100, 5300, 5500] # this is list not array
print "each price state in a cycle:\n", price
print "Average number of visits to each state in a cycle:\n ", [round(n, 2) for n in prob]
##
## USAGE: From shell prompt: python -i markovchain.py
## or within interactive python environment, scrape and paste
## REQUIRED ARGUMENTS: none
##
## OPTIONS: none
## DESCRIPTION: Simulation of the cash management model as a Markov chain
## DIAGNOSTICS: None
## CONFIGURATION AND ENVIRONMENT: None
## DEPENDENCIES: None
## INCOMPATIBILITIES: None known
## PROVENANCE: Tue Aug 28, 2012 5:38 AM
## BUGS AND LIMITATIONS: None Known
## FEATURES AND POTENTIAL IMPROVEMENTS:
## For the innermost for-loop, you can simply use
## newState = scipy.random.choice(1, 1, prob=T[state,])
## That shows more clearly what's going on
## On the other hand, if other languages don't have a function comparable to sample, then
## the current formulation is easier to adapt and make the algorithms similar.
## AUTHOR: Steve Dunbar
## VERSION: 1.0 Tue Aug 28, 2012 5:38 AM
## 1.1 added some ouput formatting Mon Nov 12, 2012 6:03 AM
## KEYWORDS: Markov chain, simulation, reflecting boundary
#! /usr/bin/env python
import scipy
n = 6; # Top boundary, number of states 0..n is n+1
s = 4; # Start and Reset state number 1 <= s <= n-1
p = 0.8;
steps = 1000;
T = scipy.diag( p*scipy.ones( n ), 1) + scipy.diag( (1-p)*scipy.ones( n ), -1);
T[0,1] = 0; T[0,s] = 1;
T[n,n-1] = 0; T[n,s] = 1;
# vector to hold the count of visits to each state during a cycle
count = scipy.zeros(n+1, float);
# Initialize the cyclelength
numberCycles = 0;
# Start in the state s
state = s;
# Make steps through the markov chain
for i in range(1, steps+1):
x = 0;
u = scipy.random.random(1);
newState = state;
for j in range(n+1):
x = x + T[state, j];
if (x >= u):
newState = j;
break;
## newState = scipy.random.choice(1, 1, prob=T[state,])
state = newState;
count[state] = count[state] + 1;
if (state == n or state == 0):
numberCycles = numberCycles + 1;
avgVisits = count/numberCycles;
avgCycleLength = i/numberCycles;
Wsk = avgVisits
print Wsk
theoreticalWsk = 2*( s*(1-scipy.arange(0,n+1,dtype=float)/n) - scipy.maximum(s-scipy.arange(0,n+1), scipy.zeros(n+1, int) ) )
prob = [0] * (n+1) # initialize probility as list data type
for i in range(1, n+1):
prob[i] = Wsk[i]/sum(Wsk);
price = [2000, 2900, 3500, 4500, 5100, 5300, 5500] # this is list not array
print "each price state in a cycle:\n", price
print "Average number of visits to each state in a cycle:\n ", [round(n, 2) for n in prob]
피드 구독하기:
글 (Atom)
