Windows7にTensorFlowを入れてみた

Python 3.5.3をインストール

その後Windowsコマンドプロンプトに下記のコマンドを入力

pip3 install --upgrade tensorflow

これだけでPythonでTensorFlowが使えるようになった。

うまくいかない人はこちら
windowsにネイティブサポートしたTensorflowを動かす - Qiita
が参考になると思います。

ちなみに上記を済ませた後に「R」にtensorflowパッケージをインストールすると「R」からも使えるようになった。

install.packages("tensorflow")

非常に簡単でした。

Pythonでフォルダ内のファイル一覧をパス無しで取得する

import glob
import os

files = glob.glob('D:/CEN_list/CEN_list/bin/Debug/file/*.txt')

for f in files:
    (dirname,filename)=os.path.split(f)
    print(filename)

ちなみにRでは

path <- "D:/CEN_list/CEN_list/bin/Debug/file/"
list.files(path)

Journalのタイトルをリストアップする

using System.Text;
using System.IO;
namespace html
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamWriter writer = new StreamWriter("article.html", true, Encoding.UTF8);
            writer.WriteLine("<ol>");
            for (int volume = 1; volume < 6; volume++)
            {
                for (int issue = 1; issue < 3; issue++)
                {
                    for (int page = 1; page < 3; page++)
                    {
                        string myfile = volume.ToString() + "_" + issue.ToString() + "_" + page.ToString() + ".html";
                        StreamReader sr = new StreamReader(myfile, Encoding.UTF8);
                        string mytext = sr.ReadToEnd();
                        sr.Close();
                        System.Text.RegularExpressions.Regex x =
                            new System.Text.RegularExpressions.Regex(
                                @"(<ol>)(.*?)(</ol>)", System.Text.RegularExpressions.RegexOptions.IgnoreCase
                                | System.Text.RegularExpressions.RegexOptions.Singleline);
                        System.Text.RegularExpressions.Match mat = x.Match(mytext);
                        string newtext = System.Text.RegularExpressions.Regex.Replace(mat.Value.ToString(), "<div class=\"actions\">(.*?)</div>", "", System.Text.RegularExpressions.RegexOptions.Singleline);
                        newtext = System.Text.RegularExpressions.Regex.Replace(newtext, "<p class=\"no-access-message\">(.*?)</p>", "", System.Text.RegularExpressions.RegexOptions.Singleline);
                        newtext = System.Text.RegularExpressions.Regex.Replace(newtext, "</ol>", "", System.Text.RegularExpressions.RegexOptions.Singleline);
                        newtext = System.Text.RegularExpressions.Regex.Replace(newtext, "<ol>", "", System.Text.RegularExpressions.RegexOptions.Singleline);
                        writer.WriteLine(newtext);
                    }
                }
            }
            writer.WriteLine("</ol>");
            writer.Close();
        }
    }
}

Journalのタイトルを一括ダウンロード

namespace download
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int volume = 1; volume < 6; volume++)
            {
                for (int issue = 1; issue < 3; issue++)
                {
                    for (int page = 1; page < 3; page++)
                    {
                        string url = "http://link.springer.com/journal/13730/" + volume.ToString() + "/" + issue.ToString() + "/page/" + page.ToString();
                        string output = volume.ToString() + "_" + issue.ToString() + "_" + page.ToString() + ".html";
                        System.Net.WebClient wc = new System.Net.WebClient();
                        wc.DownloadFile(url, output);
                        wc.Dispose();
                    }
                }
            }
        }
    }
}

英語学習のためのアプリを作る

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="英語問題" Height="350" Width="816" Initialized="Window_Initialized">
    <Grid>
        <TextBlock x:Name="textblock1" HorizontalAlignment="Left" Margin="42,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="111" Height="24" Grid.ColumnSpan="2"/>
        <TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="122" Margin="42,87,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="715" TextChanged="textbox1_TextChanged" FontSize="18"/>
        <Button x:Name="sound" Content="音声" Margin="40,245,0,0" VerticalAlignment="Top" Height="24" IsEnabled="False" Click="sound_Click" HorizontalAlignment="Left" Width="104"/>
        <Button x:Name="next" Content="新しい問題" HorizontalAlignment="Left" Margin="190,245,0,0" VerticalAlignment="Top" Width="104" Height="24" Click="next_Click"/>
        <Button x:Name="again" Content="やり直し" HorizontalAlignment="Left" Margin="340,245,0,0" VerticalAlignment="Top" Width="104" Height="24" Click="again_Click"/>
        <Button x:Name="answer" Content="正解をみる" HorizontalAlignment="Left" Margin="490,245,0,0" VerticalAlignment="Top" Width="104" Height="24" Click="answer_Click" IsEnabled="False"/>
    </Grid>
</Window>

Window1.xaml

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="解答" Height="300" Width="820">
    <Grid>
        <TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="122" Margin="42,87,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="715" FontSize="18">
        </TextBox>
    </Grid>
</Window>

Main.Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Diagnostics;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        string[] all_data;
        string[] kugiri = { "\r\n" };
        int all_q;
        int q_num;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            string txt = File.ReadAllText("english.txt",System.Text.Encoding.GetEncoding("Shift_JIS"));
            all_data = txt.Split(kugiri, StringSplitOptions.RemoveEmptyEntries);
            all_q = all_data.Length;
            next.Focus();
        }

        private void textbox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textbox1.Text == all_data[q_num])
            {
                MessageBox.Show("正解");
                next.Focus();
            }
        }

        private void next_Click(object sender, RoutedEventArgs e)
        {
            sound.IsEnabled = true;
            answer.IsEnabled = true;
            Random rand = new Random();
            q_num = rand.Next(all_q);
            textblock1.Text = (q_num + 1).ToString();
            textbox1.Text = "";
            textbox1.Focus();
        }

        private void sound_Click(object sender, RoutedEventArgs e)
        {
            string pass;
            pass = (q_num + 1).ToString() + ".mp3";
            Process.Start(pass);
        }

        private void again_Click(object sender, RoutedEventArgs e)
        {
            textbox1.Text = "";
            textbox1.Focus();
        }

        private void answer_Click(object sender, RoutedEventArgs e)
        {
            Window1 win = new Window1();
            win.textbox1.Text = all_data[q_num];
            win.ShowDialog();
        }
    }
}

Rの結果をPDFに出力する(文字化け対応)

Rの結果をテキストファイルとpngファイルに出力する。
それらをWordで受け取ってPDFファイルに出力する。
Rのコード

data(iris)
mydata <- iris

比較 <- c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")

counter=1
for (hikaku in 1:length(比較)){
		
		セトサ <- subset(mydata,Species=="setosa")[[比較[hikaku]]]
		バージーカラー <- subset(mydata,Species=="versicolor")[[比較[hikaku]]]

		if (mean(セトサ)>mean(バージーカラー)) side <-"greater"
		if (mean(セトサ)<mean(バージーカラー)) side <-"less"
		if (mean(セトサ)==mean(バージーカラー)) side <-"two.sided"

		sink(paste("R結果1_",counter,".txt",sep=""))
		cat(比較[hikaku])
		cat("\n")
		cat("\n")
		cat("セトサ\n")
		print(summary(セトサ))
		cat("\n")
		cat("バージーカラー\n")
		print(summary(バージーカラー))
		sink()

		sink(paste("R結果2_",counter,".txt",sep=""))
		print(t.test(セトサ,バージーカラー,alternative=side))
		print(wilcox.test(セトサ,バージーカラー,alternative=side))
		sink()

		png(paste("R図_",counter,".png",sep=""),width = 360, height = 360)
		boxplot(セトサ,バージーカラー,names=c("セトサ","バージーカラー"))
		dev.off()
		counter <- counter+1
	}

shell.exec("PDF.docm")

Word VBAのコード

Option Explicit

Private Sub Document_Open()
        
        Dim current_path As String
        Dim picture_path As String
        Dim output_path As String
        
        current_path = ThisDocument.Path
        output_path = current_path & "/" & "検定結果.pdf"
        
        Dim counter As Integer
        Dim myFileName As String
        
        myFileName = Dir(ThisDocument.Path & "/" & "R図_" & "*.png")
        counter = 0
        Do Until myFileName = ""
            counter = counter + 1
            myFileName = Dir()
        Loop
        
        Dim i As Integer
        For i = 1 To counter
            Dim FSO As Object, TextFile As Object, buf As String
            Dim FSO2 As Object, TextFile2 As Object, buf2 As String
            Set FSO = CreateObject("Scripting.FileSystemObject")
            Set FSO2 = CreateObject("Scripting.FileSystemObject")
            Set TextFile = FSO.OpenTextFile(current_path & "/" & "R結果1_" & i & ".txt")
            Set TextFile2 = FSO2.OpenTextFile(current_path & "/" & "R結果2_" & i & ".txt")
            buf = TextFile.ReadAll
            buf2 = TextFile2.ReadAll

            Selection.InsertAfter (buf)
            Selection.EndKey unit:=wdStory
            
            picture_path = current_path & "/" & "R図_" & i & ".png"
            Selection.InlineShapes.AddPicture FileName:=picture_path
            Selection.EndKey unit:=wdStory
            
            Selection.InsertAfter (buf2)
            Selection.EndKey unit:=wdStory
        
            Set TextFile = Nothing
            Set FSO = Nothing
            
            Set TextFile2 = Nothing
            Set FSO2 = Nothing           
        
            If i < counter Then
               Selection.InsertBreak Type:=wdPageBreak
            End If
        Next i

        ActiveDocument.ExportAsFixedFormat _
        OutputFileName:=output_path, _
        ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=True, _
        Range:=wdExportAllDocument
        
        Dim d As Integer
        For d = 1 To counter
            Kill current_path & "/" & "R結果1_" & d & ".txt"
            Kill current_path & "/" & "R結果2_" & d & ".txt"
            Kill current_path & "/" & "R図_" & d & ".png"
        Next d
        
        Application.Quit (wdDoNotSaveChanges)
End Sub

「InsertFile」を使うと文字ばけするようである。

            Selection.InsertFile FileName:=current_path & "/" & "R結果1_" & n & ".txt"

そのため以下の方法に変更したら問題なかった。

            Dim FSO As Object, TextFile As Object, buf As String
            Set FSO = CreateObject("Scripting.FileSystemObject")
            Set TextFile = FSO.OpenTextFile(current_path & "/" & "R結果1_" & i & ".txt")
            buf = TextFile.ReadAll

            Selection.InsertAfter (buf)

これでknitrを勉強しないで済むのかな?

Excelのコマンドボタンの右クリックを有効にする方法

以下の二つは共存可能。

Private Sub CommandButton1_Click()
    
    MsgBox ("左クリック")
    
End Sub

Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    If Button = 2 Then MsgBox ("右クリック")
    
End Sub