Saturday, October 28, 2017

ArrayList: add elements at once

This is how to add strings to ArrayList variable.
You can test here: https://www.compilejava.net/

import java.util.ArrayList;
import java.util.Arrays;

public class HelloWorld
{
  public static void main(String[] args)
  {
    ArrayList<String> str = new ArrayList<>(Arrays.asList("test1", "test2", "test3", "test4"));
   
    System.out.print(str.get(0) + ", " + str.get(1) + ", " + str.get(2) + ", " + str.get(3));
  }
}

"foreach" for Java

This is the "foreach" for Java.
You can test this code here: online java tester

import java.util.ArrayList;

public class HelloWorld
{
  public static void main(String[] args)
  {
    ArrayList<String> arry = new ArrayList<>();
    arry.add(new String("test1"));
    arry.add(new String("test2"));
    arry.add(new String("test3"));

    //foreach
    for(String value : arry) {
      System.out.println(value);
    }


  }
}

Sunday, October 22, 2017

Android app: a xml for a quiz display with 4 options.



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.blogspot.surlaprogrammation.frenchwordquiz.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Avoir"
        android:textAppearance="@android:style/TextAppearance.Material.Large"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="@+id/guideline-topword-h"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:weightSum="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline-topword-h">


        <Button
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:text="have"
            android:textSize="30sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/guideline-topword-h" />

        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:text="eat"
            android:textSize="30sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:text="see"
            android:textSize="30sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button2" />

        <Button
            android:id="@+id/button4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:text="go"
            android:textSize="30sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button3" />

    </LinearLayout>


    <android.support.constraint.Guideline
        android:id="@+id/guideline-topword-v"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline-topword-h"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />


</android.support.constraint.ConstraintLayout>

Saturday, October 14, 2017

Create mention tags

This is the html, css, js to create mention tags like slack.
To download all of them, see the github page.



<html>
    <head>
      <LINK rel="stylesheet" type="text/css" href="./css/textarealike.css">
    </head>
    <body>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <p>Something here</p>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <div contenteditable="true" class="form-control" style="overflow:auto;height:100px;" id="div-content"></div>
        <script
        src="https://code.jquery.com/jquery-3.2.1.min.js"
        integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
        crossorigin="anonymous"></script>
        <script
        src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"
        integrity="sha256-eGE6blurk5sHj+rmkfsGYeKyZx3M4bG+ZlFyA7Kns7E="
        crossorigin="anonymous"></script>
        <script src="./js/jquery.textcomplete.min.js"></script>
        <script>
            $(function($){
                $("[contenteditable]").focusout(function(){
                    var element = $(this);      
                    if (!element.text().trim().length) {
                        element.empty();
                    }
                });
            });
            $(document).ready(function () {
                $('#div-content').textcomplete([
                    {
                        id: 'members',
                        words: ['@john', '@jack', '@fredrick', '@ben', '@alice', '@anna', '@anne', '@all'],
                        match: /\B(\S{1,})$/,
                        search: function (term, callback) {
                            callback($.map(this.words, function (word) {
                                return word.indexOf(term) === 0 ? word : null;
                            }));
                        },
                        index: 1,
                        replace: function (word) {
                            return '<member name="mention-tag" class="mention-tag" contenteditable="false">' + word + ' </member> ';
                        }
                    }
                ], {
                    onKeydown: function (e, commands) {
                        if (e.ctrlKey && e.keyCode === 74) { // CTRL-J
                            return commands.KEY_ENTER;
                        }
                    },
                    placement: 'top'
                });
            });
        </script>
    </body>
</html>

Saturday, October 7, 2017

How to use DL4J for Linux

I am using Linux mint for my OS. The official quick start guide is here.
Suppose we are using Linux OS like Linux mint or Ubuntu.

At first, we will install JDK.
$ sudo apt-get update
$ sudo apt-get install openjdk-8-jdk
$ sudo apt-get install openjfx
$ java -version

openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

Then install Maven.
$ sudo apt-get install maven
$ mvn -version

Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_144, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.8.0-53-generic", arch: "amd64", family: "unix"

Maven is a project management tool, which is aimed for Java projects.
Available commands of Maven:
install
clean
compile
package
validate
test-compile
test
integration-test
verify
deploy

Install git.
$ sudo apt-get install git
$ git --version

git version 2.7.4

Create a workspace folder.
$ mkdir ~/dlj
$ mkdir ~/dlj/workspace
$ cd ~/dlj/workspace

Install dl4j from git.
$ git clone https://github.com/deeplearning4j/dl4j-examples.git
$ cd dl4j-examples/

If you want to use Nvidia's GPU for learning, change some of the POM in the root directory.
<name>DeepLearning4j Examples Parent</name>
        <description>Examples of training different data sets</description>
        <properties>
            <nd4j.backend>nd4j-cuda-8.0-platform</nd4j.backend>

like this


Then install.
$ mvn clean install

Install intellij (That is an integrated development environment for Java):
$ mkdir ~/intellij
$ cd ~/intellij
$ wget https://download.jetbrains.com/idea/ideaIC-2017.2.5.tar.gz
$ tar xfz ideaIC-2017.2.5.tar.gz
$ cd idea-IC-172.4343.14/bin
$ ./idea.sh
Or download intellij from the official website: https://www.jetbrains.com/idea/download/#section=linux

Open intellij and choose "import project".


Choose the folder of dl4j-examples which we downloaded just now and click "OK".


Choose "Maven" and click "Next".


Just click "Next" without touching anything.

Tick "open jfx" and click "Next".


Tick the URL of dl4j and click "Next".



Tick the SDK and click "Next".


Click "Finish".


Wait until the loading is finished.


Choose an example which you want to run and right-click on it. Then menu will be shown. Click "Run" from the menu. Then the deep learning will be started.

I will try LenetMnistExample:

The result of LenetMnistExample:
==========================Scores========================================
 # of classes:    10
 Accuracy:        0.9746
 Precision:       0.9748
 Recall:          0.9745
 F1 Score:        0.9744
Precision, recall & F1: macro-averaged (equally weighted avg. of 10 classes)
========================================================================
o.d.e.c.LenetMnistExample - ****************Example finished********************



Thursday, October 5, 2017

Cat and dog detector

This is a cat and dog detector which detects cat or dog on an image.
The code is on github:
https://github.com/lechatthecat/cat_dog_detector

This is created by a  pre-trained model by  this code.