カテゴリー: tech

  • ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

    ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

    はじめに

    パスワードを更新を促されるメッセージ。パスワードポリシーに則った形で変更するパスワードを必要がある。

    パスワード変更方法

    set password for root@localhost=password('hogeHoge@Hog123');

    結果以下の通りなれば成功

    mysql> set password for root@localhost=password('hogeHoge@Hog123');
    Query OK, 0 rows affected, 1 warning (0.05 sec)
    

    参考情報

    https://qiita.com/RyochanUedasan/items/9a49309019475536d22a

     

  • mmap() failed: [12] Cannot allocate memory

    mmap() failed: [12] Cannot allocate memory

    はじめに

    laravelをinstall時、memoryが不十分だとinstallでエラーになる。

    twitterで投稿したところ、アドバイスもらった。確かにswapの設定してなかった。。

    クラウドであれば一時的にインスタンスタイプを変更することでインストールできるが、今回はインスタンスタイプを変更せず対応する。

    swapを設定する

    実は、GCPやAWSの場合、デフォルトでswapの設定がされていない。

    # free
                  total        used        free      shared  buff/cache   available
    Mem:         601172      383328       48068       14160      169776      107824

    手動でswapを設定する

    cat /proc/swaps
    dd if=/dev/zero of=/swapfile bs=1M count=2048
    chmod 600 /swapfile
    mkswap /swapfile
    swapon /swapfile
    free -m

    再起動しても設定されるよう、fstabに設定を追加する

    /swapfile   swap        swap    defaults        0   0

    作業後

    無事swapが追加された

    free -m
                  total        used        free      shared  buff/cache   available
    Mem:            587         375          43          13         167         103
    Swap:          2047           0        2047

    @kondonator さん、ありがとうございます🙏

     

  • Amazon Transcribe が日本語に対応したので早速試した

    Amazon Transcribe が日本語に対応したので早速試した

    はじめに

    11/22 Amazon Transcribe が日本語に対応したという記事がリリースされました。

    https://aws.amazon.com/jp/about-aws/whats-new/2019/11/amazon-transcribe-now-supports-speech-to-text-in-7-additional-languages/

    議事録とったりVoice memo作ったり何かと活用の場所がある機能。早速試してみました。

    音声ファイルの準備

    今回はiPhoneのボイスメモを利用することにします。アプリを消してしまった人もいると思うので念の為リンクを貼ります。

    https://itunes.apple.com/jp/app/%E3%83%9C%E3%82%A4%E3%82%B9%E3%83%A1%E3%83%A2/id1069512134?mt=8

    このアプリで録音した音声ファイルはm4aフォーマットになっているので、wavフォーマットに変換します。

    macでは標準で付属しているafconvertというソフトを使う事で手軽に変換できます。

    afconvert -f WAVE -d LEI16 sample.m4a sample.wav

    音声ファイルのアップロード

    解析対象の音源をs3に置く必要があり、作業用にバケットを作ります。

    名前は今回以下のバケット名にしました。

    今回は全部デフォルトでポチポチと進ませバケットを作成しました。

    作成されたバケットに変換後の音源をupload。

    ここまでで準備は完了です。
    Amazon Transcribeを開き、Create jobを押し、文字起こしのジョブを登録します。

    今回は以下の通り設定することにします。
    Languageを今回追加されたJapaneseを指定することを忘れないでください。

    inport dataは、S3に置いた音源のパスを。
    Formatは変換後のフォーマットであるwavとしました。

    Output dataでいくつか指定できますが、特に役立ちそうな機能はAlternative resultsです。
    今回はこれを有効にして進めることにします。

    createを押します。

    無事登録されたようです。

    詳細情報を確認すると、StatusはIn progressになりました。暫くすると処理が始まりました。

    Transcriotion previewに、ちゃんと文字として表示されました。漢数字なのがちょっと読みづらいですね。

    結果

    jsonファイルで出力されます。
    start_time, end_timeが表示され、実際どの部分が文字起こしされたのか非常にわかりやすいと感じました。

    単語ごとにconfidenceが表示され、どれだけ的確かの指標にすることができるようです。

      "results": {
        "transcripts": [
          {
            "transcript": "今日 は 二 千 十 九 年 六月 十 一 日 です"
          }
        ],
        "items": [
          {
            "start_time": "0.84",
            "end_time": "1.27",
            "alternatives": [
              {
                "confidence": "0.9588",
                "content": "今日"
              }
            ],
            "type": "pronunciation"
          },
          {
            "start_time": "1.27",
            "end_time": "1.4",
            "alternatives": [
              {
                "confidence": "1.0",
                "content": "は"
              }
            ],
            "type": "pronunciation"
          },

    まとめ

    GoogleにもGoogle Speech APIという似たような機能があるがそちらは、今回のAWSはほとんどブラウザのみで完結できるので非エンジニアでも比較的簡単に利用することができそうです。

    今回文字起こししたのはシンプルな言葉だったけど、長文を文字起こしした際、どれだけ読みやすいかなどは注視したいと思います。

    参考情報

    GCPで似たような機能があり、それは以前実施した。

    https://tsukada.sumito.jp/2019/06/11/google-speech-api-japanese/

     

     

     

     

     

  • Unable to obtain Outpost ARN from EC2 Metadata: EC2MetadataError: failed to make EC2Metadata request

    Unable to obtain Outpost ARN from EC2 Metadata: EC2MetadataError: failed to make EC2Metadata request

    はじめに

    ECSで割り当てたEC2が、クラスタに参加できない場合の対処。

    What to do when EC2 assigned by ECS cannot join the cluster.

    tail -f  /var/log/ecs/ecs-agent.log.2019-11-12-04 
    
    2019-11-12T04:20:19Z [INFO] Restored from checkpoint file. I am running as 'arn:aws:ecs:ap-northeast-1:xxx:container-instance/xxx:container-4f04-xxx:container-9da7-xxx:container' in cluster 'account-web-ec2'
    2019-11-12T04:20:19Z [INFO] Remaining mem: 3885
    2019-11-12T04:20:19Z [ERROR] Unable to register as a container instance with ECS: ClientException: Referenced container instance xxx:container-4f04-xxx:container-9da7-xxx:container not registered.
    	status code: 400, request id: xxx:container-xxx:container-47a3-90a6-xxx:container
    2019-11-12T04:20:19Z [ERROR] Error re-registering: ClientException: Referenced container instance xxx:container-4f04-xxx:container-9da7-xxx:container not registered.
    	status code: 400, request id: ea19fb3f-06e3-47a3-90a6-xxx:container
    
    
    2019-11-12T04:20:35Z [INFO] Loading configuration
    2019-11-12T04:20:35Z [INFO] Image excluded from cleanup: amazon/amazon-ecs-agent:latest
    2019-11-12T04:20:35Z [INFO] Image excluded from cleanup: amazon/amazon-ecs-pause:0.1.0
    2019-11-12T04:20:35Z [INFO] Amazon ECS agent Version: 1.32.1, Commit: 4285f58f
    2019-11-12T04:20:35Z [INFO] Creating root ecs cgroup: /ecs
    2019-11-12T04:20:35Z [INFO] Creating cgroup /ecs
    2019-11-12T04:20:35Z [INFO] Loading state! module="statemanager"
    2019-11-12T04:20:35Z [INFO] Event stream ContainerChange start listening...
    2019-11-12T04:20:35Z [INFO] Restored cluster 'account-web-ec2'
    2019-11-12T04:20:35Z [WARN] Unable to obtain Outpost ARN from EC2 Metadata: EC2MetadataError: failed to make EC2Metadata request

    対処

    待てども待てどもクラスタに参加できない場合は、

    If you can’t join the cluster,

    “` /var/lib/ecs/data/ecs_agent_data.json “`

    を削除(もしくは退避)させることで、クラスタに参加できるようになる。
    成功すると以下の通りになる。

    By deleting (or evacuating), you can join the cluster.
    If successful output is below

     tail -f  /var/log/ecs/ecs-agent.log.2019-11-12-04 
    2019-11-12T04:25:31Z [INFO] Registration completed successfully. I am running as 'arn:aws:ecs:ap-northeast-1:xxxx:container-instance/xxxx-xxxx-xxxx-xxxx-xxxx' in cluster 'account-web-ec2'
    2019-11-12T04:25:31Z [INFO] Saving state! module="statemanager"
    2019-11-12T04:25:31Z [INFO] Beginning Polling for updates
    2019-11-12T04:25:31Z [INFO] Event stream DeregisterContainerInstance start listening...
    2019-11-12T04:25:31Z [INFO] Initializing stats engine

    参考情報

    https://dev.classmethod.jp/etc/ecsec2_cluster_failed/

     

     

     

  • docker v7 install

    docker v7 install

    はじめに

    公式リポジトリをclonesに、従来のコマンドでredash v7をインストール

    $ docker-compose run --rm server create_db
    
    $ docker-compose up -d

    を試みたところ以下のようなエラーが発生した。

    Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist`
    Killed
    npm ERR! code ELIFECYCLE
    npm ERR! errno 137
    npm ERR! redash-client@8.0.0-beta build: `npm run clean && NODE_ENV=production node --max-old-space-size=4096 node_modules/.bin/webpack`
    npm ERR! Exit status 137
    npm ERR! 
    npm ERR! Failed at the redash-client@8.0.0-beta build script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /root/.npm/_logs/2019-09-03T05_01_25_105Z-debug.log
    ERROR: Service 'server' failed to build: The command '/bin/sh -c npm run build' returned a non-zero code: 137

    node.js系のエラー?

    docker-compose.ymlを覗いてみると、Dockerfileからビルドする作りになっている。
    コンテナ内部でうまくインストールできなかったのだろうか。

    対処

    インストールで時間を使いたくないのでDocker hubから出来合いのDocker imageをpullして動かすことにした。

    作成したdocker-compose.ymlはこちら。

    https://github.com/GitSumito/redash-v7

    使い方は簡単。

    git clone https://github.com/GitSumito/redash-v7.git
    cd redash-v7
    docker-compose run --rm server create_db
    docker-compose up -d

    これでとりあえず起動することができる。

    http://localhost/setup

    参考情報

    docker-compose.yml を作成する際、非常にお世話になった。カックさんのhandson資料。

    https://github.com/kakakakakku/redash-hands-on

     

     

  • ERROR: Service ‘server’ failed to build: Error parsing reference: “node:10 as frontend-builder” is not a valid repository/tag: invalid reference format

    ERROR: Service ‘server’ failed to build: Error parsing reference: “node:10 as frontend-builder” is not a valid repository/tag: invalid reference format

    はじめに

    git cloneしたリポジトリで “` docker build “` を行ったところ、“` invalid reference format “`というエラーが発生したので、原因と対策について記載する。

    エラー内容

    docker build .
    Sending build context to Docker daemon 7.027 MB
    Step 1/18 : FROM node:10 as frontend-builder
    Error parsing reference: "node:10 as frontend-builder" is not a valid repository/tag: invalid reference format

    原因

    Dockerにmulti stage buildという事ができるようになった事をきっかけに、Dockerfileの書き方が数年前に変わった。

    この記事(https://qiita.com/minamijoyo/items/711704e85b45ff5d6405)にわかりやすく解説してある。

    multi stage buildの名前の通り、docker buildを複数のビルドに分割して実行できる。

    手元のdockerのversionを確認すると、確かに古かった。

    Client:
     Version:         1.13.1
     API version:     1.26
     Package version: docker-1.13.1-102.git7f2769b.el7.centos.x86_64
     Go version:      go1.10.3
     Git commit:      7f2769b/1.13.1
     Built:           Mon Aug  5 15:09:42 2019
     OS/Arch:         linux/amd64
    
    Server:
     Version:         1.13.1
     API version:     1.26 (minimum version 1.12)
     Package version: docker-1.13.1-102.git7f2769b.el7.centos.x86_64
     Go version:      go1.10.3
     Git commit:      7f2769b/1.13.1
     Built:           Mon Aug  5 15:09:42 2019
     OS/Arch:         linux/amd64
     Experimental:    false

    対処

    dockerをremoveし、最新のdocker(docker-ce)をインストールした。

    # rpm -qa | grep docker
    docker-common-1.13.1-102.git7f2769b.el7.centos.x86_64
    docker-client-1.13.1-102.git7f2769b.el7.centos.x86_64
    docker-1.13.1-102.git7f2769b.el7.centos.x86_64

    削除

    rpm -e docker
    rpm -e docker-client
    rpm -e docker-common

    その後、インストール。
    インストール手順は以下公式ドキュメントを参照。

    https://weblabo.oscasierra.net/docker-ce-install-centos7/

     

     

  • Cloud Natural Language API を試した

    Cloud Natural Language API を試した

    はじめに

    Googleがトレーニング済みモデルとして提供している自然言語処理(Natural Language Processing)を使うことで、文字を元に感情分析、エンティティ分析、エンティティ感情分析、コンテンツ分類、構文分析などの自然言語理解の機能がAPI経由で利用できるとのこと。

    Cloud Natural Language APIで、どのような結果を得る事ができるか試してみた。

    どのような事ができるのか

    公式ドキュメントでは以下の通り記載されている

    https://cloud.google.com/sdk/gcloud/reference/ml/language/

    analyze-entitiesUse Google Cloud Natural Language API to identify entities in text.

    analyze-entity-sentimentUse Google Cloud Natural Language API to identify entity-level sentiment.

    analyze-sentimentUse Google Cloud Natural Language API to identify sentiments in a text.

    analyze-syntaxUse Google Cloud Natural Language API to identify linguistic information.

    classify-textClassifies input document into categories.

    上から

    • エンティティ分析
    • エンティティ感情分析
    • 感情分析
    • 構文解析
    • コンテンツ分類

    だ。ひとつひとつ試していったので、実行コマンドと結果とともに解説していく。

    解析対象

    著作権フリーのドキュメントを解析対象とした。

    learningenglish.voanews.comというサイトは著作権フリーでテキスト、MP3を公開しているとのことだったので、今回はそれをコンテンツを利用することにした。

    その中でも「我々のコンテンツは著作権フリーですよ」と記載されているページを解析することにした。

    https://learningenglish.voanews.com/p/6861.html

    https://learningenglish.voanews.com/p/6861.html

    Requesting usage of VOA Learning English content

    Learning English texts, MP3s and videos are in the public domain. You are allowed to reprint them for educational and commercial purposes, with credit to learningenglish.voanews.com. VOA photos are also in the public domain. However, photos and video images from news agencies such as AP and Reuters are copyrighted, so you are not allowed to republish them.

    If you are requesting one-time use of VOA Learning English content, please fill out the information in this form and we will respond to you as soon as possible. For repeat use, please see the Content Usage FAQs on the page.

    High-resolution audio and video files can be downloaded for free through USAGM Direct an online service providing original multimedia content from Voice of America for publication across all platforms: online, mobile, print and broadcast. Access to USAGM Direct requires user registration. If you have any questions about our policies, or to let us know that you plan to use our materials, write to learningenglish@voanews.com.

    各種コマンドを実施した後、リダイレクトとしてテキストに出力させ、結果が膨大なので、上位100桁のみ表示させる。

    なお、Natural Language APIの基本について書かれているドキュメントはこちら。

    https://cloud.google.com/natural-language/docs/basics?hl=ja

    エンティティ分析

    テキストデータからエンティティ(人、組織、場所、イベント、商品、メディアなど)を特定できるようだ。

    実施コマンド

    gcloud ml language analyze-entities --content-file=/tmp/voa.original > /tmp/voa.analyze-entities

    結果

    # head -n100 /tmp/voa.analyze-entities
    {
      "entities": [
        {
          "mentions": [
            {
              "text": {
                "beginOffset": 90,
                "content": "content"
              },
              "type": "COMMON"
            },
            {
              "text": {
                "beginOffset": 518,
                "content": "content"
              },
              "type": "COMMON"
            }
          ],
          "metadata": {},
          "name": "content",
          "salience": 0.1703016,
          "type": "OTHER"
        },
        {
          "mentions": [
            {
              "text": {
                "beginOffset": 60,
                "content": "usage"
              },
              "type": "COMMON"
            }
          ],
          "metadata": {},
          "name": "usage",
          "salience": 0.077866085,
          "type": "OTHER"
        },
        {
          "mentions": [
            {
              "text": {
                "beginOffset": 132,
                "content": "videos"
              },
              "type": "COMMON"
            }
          ],
          "metadata": {},
          "name": "videos",
          "salience": 0.07223342,
          "type": "WORK_OF_ART"
        },
        {
          "mentions": [
            {
              "text": {
                "beginOffset": 0,
                "content": "https://learningenglish.voanews.com/p/6861.html"
              },
              "type": "PROPER"
            },
            {
              "text": {
                "beginOffset": 253,
                "content": "learningenglish.voanews.com"
              },
              "type": "PROPER"
            },
            {
              "text": {
                "beginOffset": 282,
                "content": "VOA"
              },
              "type": "PROPER"
            },
            {
              "text": {
                "beginOffset": 831,
                "content": "Voice of America"
              },
              "type": "PROPER"
            },
            {
              "text": {
                "beginOffset": 1083,
                "content": "learningenglish@voanews.com"
              },
              "type": "PROPER"
            }
          ],
          "metadata": {
            "mid": "/m/0q0r9",
            "wikipedia_url": "https://en.wikipedia.org/wiki/Voice_of_America"
          },
          "name": "https://learningenglish.voanews.com/p/6861.html",
          "salience": 0.07165857,
          "type": "OTHER"
        },

    結果の見方は以下の通り。

    name解析対象の文字列

     

    beginOffset: 指定したテキスト内の文の開始位置を表す(0 から始まる)文字オフセットを示します。このオフセットは、リクエストで渡した encodingType を使用して計算される。

     

    salienceドキュメントのテキスト全体に対するこのエンティティの重要性または関連性を示します。情報の取得や要約の際にエンティティを優先するのに役立ちます。スコアが 0.0 に近いほど重要性が低くなり、1.0 に近いほど重要性が高くなる。

     

    typeドキュメントの種類(HTML または PLAIN_TEXT)などが書かれる。

     

    metadatawikipediaにリンクがあればwikipedia_urlに書かれる。midはGoogle Knowledge GraphのMID(Machine-generated Identifier)が格納される

    エンティティ感情分析

    エンティティ分析と感情分析の両方を組み合わせたものであり、テキスト内でエンティティについて表現された感情(ポジティブかネガティブか)の特定ができるようだ

    実施コマンド

    gcloud ml language analyze-entity-sentiment --content-file=/tmp/voa.original > /tmp/voa.analyze-entity-sentiment
    

    結果

    # head -n100 /tmp/voa.analyze-entity-sentiment
    {
      "entities": [
        {
          "mentions": [
            {
              "sentiment": {
                "magnitude": 0.2,
                "score": 0.2
              },
              "text": {
                "beginOffset": 90,
                "content": "content"
              },
              "type": "COMMON"
            },
            {
              "sentiment": {
                "magnitude": 0.1,
                "score": 0.1
              },
              "text": {
                "beginOffset": 518,
                "content": "content"
              },
              "type": "COMMON"
            }
          ],
          "metadata": {},
          "name": "content",
          "salience": 0.1703016,
          "sentiment": {
            "magnitude": 0.3,
            "score": 0.1
          },
          "type": "OTHER"
        },
        {
          "mentions": [
            {
              "sentiment": {
                "magnitude": 0.5,
                "score": 0.5
              },
              "text": {
                "beginOffset": 60,
                "content": "usage"
              },
              "type": "COMMON"
            }
          ],
          "metadata": {},
          "name": "usage",
          "salience": 0.077866085,
          "sentiment": {
            "magnitude": 0.5,
            "score": 0.5
          },
          "type": "OTHER"
        },
        {
          "mentions": [
            {
              "sentiment": {
                "magnitude": 0.4,
                "score": 0.4
              },
              "text": {
                "beginOffset": 132,
                "content": "videos"
              },
              "type": "COMMON"
            }
          ],
          "metadata": {},
          "name": "videos",
          "salience": 0.07223342,
          "sentiment": {
            "magnitude": 0.4,
            "score": 0.4
          },
          "type": "WORK_OF_ART"
        },
        {
          "mentions": [
            {
              "sentiment": {
                "magnitude": 0.0,
                "score": 0.0
              },
              "text": {
                "beginOffset": 0,
                "content": "https://learningenglish.voanews.com/p/6861.html"
              },
              "type": "PROPER"
            },
            {
              "sentiment": {
                "magnitude": 0.1,
                "score": 0.1
              },
    

     

    magnitude: 指定したテキストの全体的な感情の強度(ポジティブとネガティブの両方)が 0.0+inf の値で示されるscore と違って magnitude は正規化されていないため、テキスト内で感情(ポジティブとネガティブの両方)が表現されるたびにテキストの magnitude の値が増加

    と、公式にはあるが、ドキュメントは正直よくわからないが、以下の表は非常にわかりやすかった。

    感情 サンプル値
    明らかにポジティブ* "score": 0.8、"magnitude": 3.0
    明らかにネガティブ* "score": -0.6、"magnitude": 4.0
    ニュートラル "score": 0.1、"magnitude": 0.0
    混合 "score": 0.0、"magnitude": 4.0

     

    感情分析

    指定されたテキストを調べて、そのテキストの背景にある感情的な考え方を分析することができる。

    実施コマンド

    gcloud ml language analyze-sentiment --content-file=/tmp/voa.original > /tmp/voa.analyze-sentiment

    結果

    # head -n100 /tmp/voa.analyze-sentiment
    {
      "documentSentiment": {
        "magnitude": 4.6,
        "score": 0.2
      },
      "language": "en",
      "sentences": [
        {
          "sentiment": {
            "magnitude": 0.0,
            "score": 0.0
          },
          "text": {
            "beginOffset": 0,
            "content": "https://learningenglish.voanews.com/p/6861.html"
          }
        },
        {
          "sentiment": {
            "magnitude": 0.8,
            "score": 0.8
          },
          "text": {
            "beginOffset": 49,
            "content": "Requesting usage of VOA Learning English content"
          }
        },
        {
          "sentiment": {
            "magnitude": 0.8,
            "score": 0.8
          },
          "text": {
            "beginOffset": 99,
            "content": "Learning English texts, MP3s and videos are in the public domain."
          }
        },
        {
          "sentiment": {
            "magnitude": 0.0,
            "score": 0.0
          },
          "text": {
            "beginOffset": 165,
            "content": "You are allowed to reprint them for educational and commercial purposes, with credit to learningenglish.voanews.com."
          }
        },
        {
          "sentiment": {
            "magnitude": 0.1,
            "score": 0.1
          },
          "text": {
            "beginOffset": 282,
            "content": "VOA photos are also in the public domain."
          }
        },
        {
          "sentiment": {
            "magnitude": 0.4,
            "score": -0.4
          },
          "text": {
            "beginOffset": 324,
            "content": "However, photos and video images from news agencies such as AP and Reuters are copyrighted, so you are not allowed to republish them."
          }
        },
        {
          "sentiment": {
            "magnitude": 0.7,
            "score": 0.7
          },
          "text": {
            "beginOffset": 459,
            "content": "If you are requesting one-time use of VOA Learning English content, please fill out the information in this form and we will respond to you as soon as possible."
          }
        },
        {
          "sentiment": {
            "magnitude": 0.2,
            "score": -0.2
          },
          "text": {
            "beginOffset": 620,
            "content": "For repeat use, please see the Content Usage FAQs on the page."
          }
        },
        {
          "sentiment": {
            "magnitude": 0.3,
            "score": 0.3
          },
          "text": {
            "beginOffset": 684,
            "content": "High-resolution audio and video files can be downloaded for free through USAGM Direct an online service providing original multimedia content from Voice of America for publication across all platforms: online, mobile, print and broadcast."
          }
        },
        {
          "sentiment": {
            "magnitude": 0.3,

    各種項目は今までに説明したものがメイン。大きな特徴はcontentが単語ではなく、文(センテンス)になっているということ。センテンス単位でmagnitudeや、scoreが算出されている。

    そのため、文を通して感情を数値として読み取る事ができる。

    コンテンツ分類

    ドキュメントを分析し、ドキュメント内で見つかったテキストに適用されるコンテンツカテゴリのリストを返す事ができる

    実施コマンド

    gcloud ml language classify-text --content-file=/tmp/voa.original > /tmp/voa.classify-text

    結果

    # head -n100 /tmp/voa.classify-text
    {
      "categories": [
        {
          "confidence": 0.81,
          "name": "/Reference/Language Resources/Foreign Language Resources"
        }
      ]
    }

    “リファレンス/言語リソース/外国語リソース”

    外国語コンテンツのリファレンスということが、なんとなくわかる。

    構文解析

    指定されたテキストを一連の文とトークン(通常は単語)に分解して、それらのトークンに関する言語情報を提供する

    実行コマンド

    gcloud ml language analyze-syntax --content-file=/tmp/voa.original > /tmp/voa.analyze-syntax
    

    結果

    # head -n200 /tmp/voa.analyze-syntax
    {
      "language": "en",
      "sentences": [
        {
          "text": {
            "beginOffset": 0,
            "content": "https://learningenglish.voanews.com/p/6861.html"
          }
        },
        {
          "text": {
            "beginOffset": 49,
            "content": "Requesting usage of VOA Learning English content"
          }
        },
        {
          "text": {
            "beginOffset": 99,
            "content": "Learning English texts, MP3s and videos are in the public domain."
          }
        },
        {
          "text": {
            "beginOffset": 165,
            "content": "You are allowed to reprint them for educational and commercial purposes, with credit to learningenglish.voanews.com."
          }
        },
        {
          "text": {
            "beginOffset": 282,
            "content": "VOA photos are also in the public domain."
          }
        },
        {
          "text": {
            "beginOffset": 324,
            "content": "However, photos and video images from news agencies such as AP and Reuters are copyrighted, so you are not allowed to republish them."
          }
        },
        {
          "text": {
            "beginOffset": 459,
            "content": "If you are requesting one-time use of VOA Learning English content, please fill out the information in this form and we will respond to you as soon as possible."
          }
        },
        {
          "text": {
            "beginOffset": 620,
            "content": "For repeat use, please see the Content Usage FAQs on the page."
          }
        },
        {
          "text": {
            "beginOffset": 684,
            "content": "High-resolution audio and video files can be downloaded for free through USAGM Direct an online service providing original multimedia content from Voice of America for publication across all platforms: online, mobile, print and broadcast."
          }
        },
        {
          "text": {
            "beginOffset": 923,
            "content": "Access to USAGM Direct requires user registration."
          }
        },
        {
          "text": {
            "beginOffset": 974,
            "content": "If you have any questions about our policies, or to let us know that you plan to use our materials, write to learningenglish@voanews.com."
          }
        }
      ],
      "tokens": [
        {
          "dependencyEdge": {
            "headTokenIndex": 0,
            "label": "ROOT"
          },
          "lemma": "https://learningenglish.voanews.com/p/6861.html",
          "partOfSpeech": {
            "aspect": "ASPECT_UNKNOWN",
            "case": "CASE_UNKNOWN",
            "form": "FORM_UNKNOWN",
            "gender": "GENDER_UNKNOWN",
            "mood": "MOOD_UNKNOWN",
            "number": "NUMBER_UNKNOWN",
            "person": "PERSON_UNKNOWN",
            "proper": "PROPER_UNKNOWN",
            "reciprocity": "RECIPROCITY_UNKNOWN",
            "tag": "X",
            "tense": "TENSE_UNKNOWN",
            "voice": "VOICE_UNKNOWN"
          },
          "text": {
            "beginOffset": 0,
            "content": "https://learningenglish.voanews.com/p/6861.html"
          }
        },
        {
          "dependencyEdge": {
            "headTokenIndex": 2,
            "label": "AMOD"
          },
          "lemma": "request",
          "partOfSpeech": {
            "aspect": "ASPECT_UNKNOWN",
            "case": "CASE_UNKNOWN",
            "form": "FORM_UNKNOWN",
            "gender": "GENDER_UNKNOWN",
            "mood": "MOOD_UNKNOWN",
            "number": "NUMBER_UNKNOWN",
            "person": "PERSON_UNKNOWN",
            "proper": "PROPER_UNKNOWN",
            "reciprocity": "RECIPROCITY_UNKNOWN",
            "tag": "VERB",
            "tense": "TENSE_UNKNOWN",
            "voice": "VOICE_UNKNOWN"
          },
          "text": {
            "beginOffset": 49,
            "content": "Requesting"
          }
        },
        {
          "dependencyEdge": {
            "headTokenIndex": 2,
            "label": "ROOT"
          },
          "lemma": "usage",
          "partOfSpeech": {
            "aspect": "ASPECT_UNKNOWN",
            "case": "CASE_UNKNOWN",
            "form": "FORM_UNKNOWN",
            "gender": "GENDER_UNKNOWN",
            "mood": "MOOD_UNKNOWN",
            "number": "SINGULAR",
            "person": "PERSON_UNKNOWN",
            "proper": "PROPER_UNKNOWN",
            "reciprocity": "RECIPROCITY_UNKNOWN",
            "tag": "NOUN",
            "tense": "TENSE_UNKNOWN",
            "voice": "VOICE_UNKNOWN"
          },
          "text": {
            "beginOffset": 60,
            "content": "usage"
          }
        },
        {
          "dependencyEdge": {
            "headTokenIndex": 2,
            "label": "PREP"
          },
          "lemma": "of",
          "partOfSpeech": {
            "aspect": "ASPECT_UNKNOWN",
            "case": "CASE_UNKNOWN",
            "form": "FORM_UNKNOWN",
            "gender": "GENDER_UNKNOWN",
            "mood": "MOOD_UNKNOWN",
            "number": "NUMBER_UNKNOWN",
            "person": "PERSON_UNKNOWN",
            "proper": "PROPER_UNKNOWN",
            "reciprocity": "RECIPROCITY_UNKNOWN",
            "tag": "ADP",
            "tense": "TENSE_UNKNOWN",
            "voice": "VOICE_UNKNOWN"
          },
          "text": {
            "beginOffset": 66,
            "content": "of"
          }
        },
        {
          "dependencyEdge": {
            "headTokenIndex": 6,
            "label": "NN"
          },
          "lemma": "VOA",
          "partOfSpeech": {
            "aspect": "ASPECT_UNKNOWN",
            "case": "CASE_UNKNOWN",
            "form": "FORM_UNKNOWN",
            "gender": "GENDER_UNKNOWN",
            "mood": "MOOD_UNKNOWN",
            "number": "SINGULAR",
            "person": "PERSON_UNKNOWN",
            "proper": "PROPER",
            "reciprocity": "RECIPROCITY_UNKNOWN",
            "tag": "NOUN",
            "tense": "TENSE_UNKNOWN",
            "voice": "VOICE_UNKNOWN"
          },
          "text": {
            "beginOffset": 69,
            "content": "VOA"
          }
        },
        {
          "dependencyEdge": {
            "headTokenIndex": 6,
            "label": "NN"

    文とトークンが抽出され、それらの文(sentences)と中盤以降にトークン(tokens)を含むレスポンスが返される。

    tagはNOUN(名詞)、VERB(動詞)、ADJ(形容詞)などがわかる。

    まとめ

    GCPが使えるようになっていれば非常に簡単にCloud Natural Language API を試す事ができ、使い方によっては非常に有益な解析ができそうだ。

  • zsh: no matches found:  zshでscpコマンドで失敗

    zsh: no matches found: zshでscpコマンドで失敗

    はじめに

    zshでscpコマンドで失敗した。

    % scp -r root@tkd002:/tmp/hoge* .
    zsh: no matches found: root@tkd002:/tmp/hoge*

    原因とその対策についてまとめる

    原因

    zshの補完でひっかかってしまうようだ。

    setopt nonomatch

    .zshrcへ一行追加すればよいが、その場限りの場合は上記コマンドを単純に実行するだけでもよい。

    % setopt nonomatch
    % scp -r root@tkd002:/tmp/hoge* .
    hoge.analyze-sentiment       100% 3865   884.6KB/s   00:00    
    % 

     

     

     

     

  • dockerをECSで動かした時のdebug方法について

    dockerをECSで動かした時のdebug方法について

    はじめに

    dockerコンテナを動かしていると、何らかの原因で起動シェルが止まってしまうことがある。今回はその調査方法の一つ、ECS用にAWSからオフィシャルのログ収集ツール ECSログコレクター が提供されているので、今回はそれを紹介。

    インストール方法

    AWS公式サイトにある通り、ツールを取得する

    $ curl -O https://raw.githubusercontent.com/awslabs/ecs-logs-collector/master/ecs-logs-collector.sh

    その後、sudoをつけてシェルを実行する

    $ sudo bash ./ecs-logs-collector.sh

    しばらくすると、諸々ログが取れる

    $ sudo bash ./ecs-logs-collector.sh
    Trying to check if the script is running as root ... ok
    Trying to resolve instance-id ... ok
    Trying to collect system information ... ok
    Trying to check disk space usage ... ok
    Trying to collect common operating system logs ... ok
    Trying to collect kernel logs ... ok
    Trying to get mount points and volume information ... ok
    Trying to check SELinux status ... ok
    Trying to get iptables list ... ok
    Trying to detect installed packages ... ok
    Trying to detect active system services list ... ok
    Trying to gather Docker daemon information ... ok
    Trying to inspect all Docker containers ... ok
    Trying to collect Docker daemon logs ... ok
    Trying to collect Amazon ECS Container Agent logs ... ok
    Trying to collect Amazon ECS Container Agent state and config ... ok
    Trying to collect Amazon ECS Container Agent engine data ... ok
    Trying to archive gathered log information ... ok

    無事完了すると、correctというディレクトリが作られ、ログが集約される。

    $ ls -ltr
    合計 236
    -rw-rw-r-- 1 ec2-user ec2-user  14181  7月 19 14:25 ecs-logs-collector.sh
    drwxr-xr-x 3 root     root       4096  7月 19 14:25 collect
    -rw-r--r-- 1 root     root     219653  7月 19 14:26 collect-i-0051ca9951de8e82a.tgz

    dockerの状態を確認するには、correct、インスタンスid、配下のdockerディレクトリにログとして出力される。

    /collect/i-xxxxxxxxx/docker

    ざっと見てみると、

    [
        {
            "Id": "xxxxxxx",
            "Created": "2019-07-19T12:36:48.665852002Z",
            "Path": "sh",
            "Args": [
                "/root/bin/execute.sh"
            ],
            "State": {
                "Status": "exited",
                "Running": false,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": true,
                "Dead": false,
                "Pid": 0,
                "ExitCode": 137,
                "Error": "",
                "StartedAt": "2019-07-19T12:36:49.47823385Z",
                "FinishedAt": "2019-07-19T13:12:07.097787961Z"
            },

    終了したときの状態がわかる。

    OOMKilledがtrueになっており、このコンテナはメモリを使い果たし、OOMKillerが発動、コンテナがkillされたのではと推測が立つ。

    linuxのimageをbaseに使っていると、当然メモリーを使い切った際にはOOM killerなどは発生する。その際の原因調査をする上では、サーバレスのfargateよりは、まだホストにログインできるECSの方が原因調査はやりやすい。

    それ以外にも非常に細かい状態を確認することができる。

                "DnsOptions": null,
                "DnsSearch": null,
                "ExtraHosts": null,
                "GroupAdd": null,
                "IpcMode": "shareable",
                "Cgroup": "",
                "Links": null,
                "OomScoreAdj": 0,
                "PidMode": "",
                "Privileged": false,
                "PublishAllPorts": false,
                "ReadonlyRootfs": false,
                "SecurityOpt": null,
                "UTSMode": "",
                "UsernsMode": "",
                "ShmSize": 67108864,
                "Runtime": "runc",
                "ConsoleSize": [
                    0,
                    0
                ],
                "Isolation": "",
                "CpuShares": 2,
                "Memory": 2147483648,
                "NanoCpus": 0,
                "CgroupParent": "/ecs/ad627055-dc5b-4903-96ed-be9e0f25cf33",
                "BlkioWeight": 0,
                "BlkioWeightDevice": null,
                "BlkioDeviceReadBps": null,
                "BlkioDeviceWriteBps": null,
                "BlkioDeviceReadIOps": null,
                "BlkioDeviceWriteIOps": null,
                "CpuPeriod": 0,
                "CpuQuota": 0,
                "CpuRealtimePeriod": 0,
                "CpuRealtimeRuntime": 0,
                "CpusetCpus": "",
                "CpusetMems": "",
                "Devices": null,
                "DeviceCgroupRules": null,
                "DiskQuota": 0,
                "KernelMemory": 0,
                "MemoryReservation": 1073741824,
                "MemorySwap": 4294967296,
                "MemorySwappiness": null,
                "OomKillDisable": false,
                "PidsLimit": 0,
                "Ulimits": [
                    {
                        "Name": "cpu",
                        "Hard": 0,
                        "Soft": 0
                    },
                    {
                        "Name": "nofile",
                        "Hard": 4096,
                        "Soft": 1024
                    }
                ],
                "CpuCount": 0,
                "CpuPercent": 0,
                "IOMaximumIOps": 0,
                "IOMaximumBandwidth": 0,
                "MaskedPaths": [
                    "/proc/acpi",
                    "/proc/kcore",
                    "/proc/keys",
                    "/proc/latency_stats",
                    "/proc/timer_list",
                    "/proc/timer_stats",
                    "/proc/sched_debug",
                    "/proc/scsi",
                    "/sys/firmware"
                ],

    詳細はこちら。

    https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/ecs-logs-collector.html

    では、今日はこの辺で。

     

     

  • 作業ブランチを別リポジトリのブランチへ引っ越し

    作業ブランチを別リポジトリのブランチへ引っ越し

    はじめに

    アプリケーションを作っていると、当初作ろうとしていたものと、今作っているものが徐々にズレてくることがある。

    その時に考えることの一つが、「リポジトリどうしよう」ってやつだ。

    新しいリポジトリとはいえ、いきなりmasterにpushするのもなんだか忍びない。

    今回はリポジトリのブランチを別のリポジトリのブランチとしてコピーする方法を紹介。

    やりたいこと

    いたって簡単。

    これだ。これをやりたいのだ。

    コマンド

    # 新しいリポジトリをclone
    git clone git@git.sumito.com:hoge/AFTER-REPO.git
    cd AFTER-REPO
    git checkout -b AFTER-BRANCH
    
    # remote登録
    git remote add tmpbranch git@git.sumito.com:hoge/BEFORE-REPO.git
    git pull tmpbranch AFTER-BRANCH --allow-unrelated-histories
    
    # 新BRANCHへpush
    git add .
    git commit -m 'copy'
    git remote rm tmpbranch
    git push origin AFTER-BRANCH

    あとはpushした先で期待通りになっているか確認するだけでよい。