text:
layout: model
title: Chinese BertForTokenClassification Cased model (from Yip)
author: John Snow Labs
name: bert_ner_bert_finetuned_ner_chinese
date: 2022-08-02
tags: [bert, ner, open_source, zh]
task: Named Entity Recognition
language: zh
edition: Spark NLP 4.1.0
spark_version: 3.0
supported: true
annotator: BertForTokenClassification
article_header:
  type: cover
use_language_switcher: "Python-Scala-Java"
---

## Description

Pretrained BertForTokenClassification model, adapted from Hugging Face and curated to provide scalability and production-readiness using Spark NLP. `bert-finetuned-ner-chinese` is a Chinese model originally trained by `Yip`.

## Predicted Entities

`company`, `name`, `position`, `movie`, `organization`, `scene`, `game`, `address`, `0`, `book`, `government`

{:.btn-box}
<button class="button button-orange" disabled>Live Demo</button>
<button class="button button-orange" disabled>Open in Colab</button>
[Download](https://s3.amazonaws.com/auxdata.johnsnowlabs.com/public/models/bert_ner_bert_finetuned_ner_chinese_zh_4.1.0_3.0_1659423087160.zip){:.button.button-orange.button-orange-trans.arr.button-icon}

## How to use

<div class="tabs-box" markdown="1">
{% include programmingLanguageSelectScalaPythonNLU.html %}
```python
documentAssembler = DocumentAssembler() \
        .setInputCol("text") \
        .setOutputCol("document")

sentenceDetector = SentenceDetectorDLModel.pretrained("sentence_detector_dl", "xx")\
       .setInputCols(["document"])\
       .setOutputCol("sentence")

tokenizer = Tokenizer() \
    .setInputCols("sentence") \
    .setOutputCol("token")

tokenClassifier = BertForTokenClassification.pretrained("bert_ner_bert_finetuned_ner_chinese","zh") \
    .setInputCols(["sentence", "token"]) \
    .setOutputCol("ner")

pipeline = Pipeline(stages=[documentAssembler, sentenceDetector, tokenizer, tokenClassifier])

data = spark.createDataFrame([["PUT YOUR STRING HERE"]]).toDF("text")

result = pipeline.fit(data).transform(data)
```
```scala
val documentAssembler = new DocumentAssembler()
          .setInputCol("text")
          .setOutputCol("document")

val sentenceDetector = SentenceDetectorDLModel.pretrained("sentence_detector_dl", "xx")
       .setInputCols(Array("document"))
       .setOutputCol("sentence")

val tokenizer = new Tokenizer()
    .setInputCols(Array("sentence"))
    .setOutputCol("token")
+
val tokenClassifier = BertForTokenClassification.pretrained("bert_ner_bert_finetuned_ner_chinese","zh")
    .setInputCols(Array("sentence", "token"))
    .setOutputCol("ner")

val pipeline = new Pipeline().setStages(Array(documentAssembler,sentenceDetector, tokenizer, tokenClassifier))

val data = Seq("PUT YOUR STRING HERE").toDF("text")

val result = pipeline.fit(data).transform(data)
```
</div>

{:.model-param}
## Model Information

{:.table-model}
|---|---|
|Model Name:|bert_ner_bert_finetuned_ner_chinese|
|Compatibility:|Spark NLP 4.1.0+|
|License:|Open Source|
|Edition:|Official|
|Input Labels:|[document, token]|
|Output Labels:|[ner]|
|Language:|zh|
|Size:|381.6 MB|
|Case sensitive:|true|
|Max sentence length:|128|

## References

- https://huggingface.co/Yip/bert-finetuned-ner-chinese


text:
/* line 15, ../../../app/stylesheets/base.sass */
* {
  margin: 0;
  padding: 0; }

/* line 19, ../../../app/stylesheets/base.sass */
body {
  font-size: 12px;
  font-family: sans-serif; }
  /* line 23, ../../../app/stylesheets/base.sass */
  body .clear {
    clear: both;
    height: 0; }
  /* line 27, ../../../app/stylesheets/base.sass */
  body h1 {
    margin: 15px 0;
    font-size: 22px;
    font-weight: normal; }
  /* line 32, ../../../app/stylesheets/base.sass */
  body h2 {
    font-size: 22px;
    margin: 15px 0;
    font-weight: normal; }
  /* line 37, ../../../app/stylesheets/base.sass */
  body h3 {
    font-size: 18px;
    margin: 10px 0;
    font-weight: normal; }
  /* line 42, ../../../app/stylesheets/base.sass */
  body h4 {
    font-size: 16px;
    margin: 10px 0;
    font-weight: normal; }
  /* line 47, ../../../app/stylesheets/base.sass */
  body hr {
    height: 1px;
    border: 0; }
  /* line 51, ../../../app/stylesheets/base.sass */
  body p {
    margin: 15px 0; }
  /* line 54, ../../../app/stylesheets/base.sass */
  body a img {
    border: none; }
  /* line 57, ../../../app/stylesheets/base.sass */
  body #container {
    min-width: 960px; }
    /* line 60, ../../../app/stylesheets/base.sass */
    body #container #header {
      padding: 0 20px;
      position: relative;
      padding-top: 1px; }
      /* line 65, ../../../app/stylesheets/base.sass */
      body #container #header h1 {
        margin: 0;
        padding: 10px 0;
        font-size: 30px; }
        /* line 71, ../../../app/stylesheets/base.sass */
        body #container #header h1 a:link, body #container #header h1 a:active, body #container #header h1 a:hover, body #container #header h1 a:visited {
          text-decoration: none; }
      /* line 74, ../../../app/stylesheets/base.sass */
      body #container #header #user-navigation {
        position: absolute;
        top: 0px;
        right: 20px; }
        /* line 79, ../../../app/stylesheets/base.sass */
        body #container #header #user-navigation ul {
          margin: 0;
          padding: 0;
          list-style-type: none; }
          /* line 84, ../../../app/stylesheets/base.sass */
          body #container #header #user-navigation ul li {
            float: left;
            padding: 5px 10px; }
            /* line 89, ../../../app/stylesheets/base.sass */
            body #container #header #user-navigation ul li a:link, body #container #header #user-navigation ul li a:visited, body #container #header #user-navigation ul li a:hover, body #container #header #user-navigation ul li a:active {
              text-decoration: none; }
      /* line 92, ../../../app/stylesheets/base.sass */
      body #container #header #main-navigation {
        width: 100%; }
        /* line 95, ../../../app/stylesheets/base.sass */
        body #container #header #main-navigation ul {
          margin: 0;
          padding: 0;
          list-style-type: none; }
          /* line 100, ../../../app/stylesheets/base.sass */
          body #container #header #main-navigation ul li {
            float: left;
            margin-right: 5px; }
            /* line 104, ../../../app/stylesheets/base.sass */
            body #container #header #main-navigation ul li a {
              font-size: 15px;
              display: block;
              padding: 8px 15px; }
              /* line 109, ../../../app/stylesheets/base.sass */
              body #container #header #main-navigation ul li a:link, body #container #header #main-navigation ul li a:visited, body #container #header #main-navigation ul li a:hover, body #container #header #main-navigation ul li a:active {
                text-decoration: none; }
    /* line 113, ../../../app/stylesheets/base.sass */
    body #container #wrapper {
      padding: 0 20px;
      padding-top: 20px; }
      /* line 117, ../../../app/stylesheets/base.sass */
      body #container #wrapper #main {
        width: 70%;
        float: left; }
        /* line 121, ../../../app/stylesheets/base.sass */
        body #container #wrapper #main .block {
          margin-bottom: 20px;
          padding-top: 1px; }
          /* line 125, ../../../app/stylesheets/base.sass */
          body #container #wrapper #main .block .content .inner {
            padding: 0 15px 15px; }
        /* line 128, ../../../app/stylesheets/base.sass */
        body #container #wrapper #main .main p.first {
          margin-top: 0; }
        /* line 131, ../../../app/stylesheets/base.sass */
        body #container #wrapper #main #footer {
          padding-bottom: 20px; }
      /* line 134, ../../../app/stylesheets/base.sass */
      body #container #wrapper #sidebar {
        width: 25%;
        float: right; }
        /* line 138, ../../../app/stylesheets/base.sass */
        body #container #wrapper #sidebar h3 {
          padding: 10px 15px;
          margin: 0;
          font-size: 13px; }
        /* line 143, ../../../app/stylesheets/base.sass */
        body #container #wrapper #sidebar .block {
          margin-bottom: 20px;
          padding-bottom: 10px; }
          /* line 147, ../../../app/stylesheets/base.sass */
          body #container #wrapper #sidebar .block .content {
            padding: 0 15px; }
          /* line 150, ../../../app/stylesheets/base.sass */
          body #container #wrapper #sidebar .block .sidebar-block {
            padding: 10px; }
        /* line 153, ../../../app/stylesheets/base.sass */
        body #container #wrapper #sidebar ul.navigation {
          margin: 0;
          padding: 0;
          list-style-type: none; }
          /* line 159, ../../../app/stylesheets/base.sass */
          body #container #wrapper #sidebar ul.navigation li a:link, body #container #wrapper #sidebar ul.navigation li a:visited {
            display: block;
            padding: 10px 15px; }
        /* line 163, ../../../app/stylesheets/base.sass */
        body #container #wrapper #sidebar .notice {
          padding: 10px; }
    /* line 166, ../../../app/stylesheets/base.sass */
    body #container #box {
      width: 500px;
      margin: 50px auto; }
      /* line 170, ../../../app/stylesheets/base.sass */
      body #container #box .block {
        margin-bottom: 20px; }
        /* line 173, ../../../app/stylesheets/base.sass */
        body #container #box .block h2 {
          padding: 10px 15px;
          margin: 0; }
        /* line 177, ../../../app/stylesheets/base.sass */
        body #container #box .block .content {
          padding: 10px 20px; }
  /* line 180, ../../../app/stylesheets/base.sass */
  body .secondary-navigation {
    font-size: 13px;
    border-bottom-width: 10px;
    border-bottom-style: solid; }
    /* line 185, ../../../app/stylesheets/base.sass */
    body .secondary-navigation ul {
      margin: 0;
      padding: 0;
      list-style-type: none; }
      /* line 190, ../../../app/stylesheets/base.sass */
      body .secondary-navigation ul li {
        float: left; }
        /* line 193, ../../../app/stylesheets/base.sass */
        body .secondary-navigation ul li a {
          display: block;
          padding: 10px 15px; }
          /* line 197, ../../../app/stylesheets/base.sass */
          body .secondary-navigation ul li a:link, body .secondary-navigation ul li a:visited, body .secondary-navigation ul li a:hover, body .secondary-navigation ul li a:active {
            text-decoration: none; }
  /* line 201, ../../../app/stylesheets/base.sass */
  body .pagination a {
    padding: 2px 5px;
    margin-right: 5px;
    display: block;
    float: left;
    border-style: solid;
    border-width: 1px;
    text-decoration: none; }
  /* line 210, ../../../app/stylesheets/base.sass */
  body .pagination span {
    padding: 2px 5px;
    margin-right: 5px;
    display: block;
    float: left;
    border-style: solid;
    border-width: 1px; }
    /* line 218, ../../../app/stylesheets/base.sass */
    body .pagination span.current {
      font-weight: bold; }
  /* line 222, ../../../app/stylesheets/base.sass */
  body .table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 15px; }
    /* line 227, ../../../app/stylesheets/base.sass */
    body .table th {
      padding: 10px;
      font-weight: bold;
      text-align: left; }
      /* line 232, ../../../app/stylesheets/base.sass */
      body .table th.first {
        width: 30px; }
      /* line 235, ../../../app/stylesheets/base.sass */
      body .table th.last {
        width: 200px; }
    /* line 238, ../../../app/stylesheets/base.sass */
    body .table .checkbox {
      margin-left: 10px; }
    /* line 241, ../../../app/stylesheets/base.sass */
    body .table td {
      padding: 10px; }
      /* line 244, ../../../app/stylesheets/base.sass */
      body .table td.last {
        text-align: right; }
  /* line 248, ../../../app/stylesheets/base.sass */
  body input.checkbox {
    margin: 0;
    padding: 0; }
  /* line 254, ../../../app/stylesheets/base.sass */
  body .form .group {
    margin-bottom: 15px; }
  /* line 258, ../../../app/stylesheets/base.sass */
  body .form div.left {
    width: 20%;
    float: left; }
  /* line 262, ../../../app/stylesheets/base.sass */
  body .form div.right {
    width: 75%;
    float: right; }
  /* line 266, ../../../app/stylesheets/base.sass */
  body .form div.fieldWithErrors label.label {
    display: inline; }
  /* line 270, ../../../app/stylesheets/base.sass */
  body .form .columns .column {
    width: 48%; }
  /* line 273, ../../../app/stylesheets/base.sass */
  body .form .columns .left {
    float: left; }
  /* line 276, ../../../app/stylesheets/base.sass */
  body .form .columns .right {
    float: right; }
  /* line 280, ../../../app/stylesheets/base.sass */
  body .form label.label {
    font-size: 1.2em;
    padding: 1px 0;
    margin: 0;
    display: block;
    padding-bottom: 2px;
    font-weight: bold; }
  /* line 288, ../../../app/stylesheets/base.sass */
  body .form label.right {
    text-align: right; }
  /* line 291, ../../../app/stylesheets/base.sass */
  body .form label.checkbox, body .form label.radio {
    line-height: 1.5em; }
  /* line 295, ../../../app/stylesheets/base.sass */
  body .form input.text_field {
    font-size: 1.2em;
    padding: 1px 0;
    margin: 0;
    width: 100%;
    border-width: 1px;
    border-style: solid; }
  /* line 303, ../../../app/stylesheets/base.sass */
  body .form input.checkbox, body .form input.radio {
    margin-right: 5px; }
  /* line 306, ../../../app/stylesheets/base.sass */
  body .form textarea.text_area {
    font-size: 1.2em;
    padding: 1px 0;
    margin: 0;
    width: 100%;
    border-width: 1px;
    border-style: solid; }
  /* line 314, ../../../app/stylesheets/base.sass */
  body .form .fieldWithErrors .error {
    color: red; }
  /* line 318, ../../../app/stylesheets/base.sass */
  body ul.list {
    margin: 0;
    padding: 0;
    list-style-type: none; }
    /* line 323, ../../../app/stylesheets/base.sass */
    body ul.list li {
      clear: left;
      padding-bottom: 5px; }
      /* line 327, ../../../app/stylesheets/base.sass */
      body ul.list li .left {
        float: left; }
        /* line 330, ../../../app/stylesheets/base.sass */
        body ul.list li .left .avatar {
          width: 50px;
          height: 50px; }
      /* line 334, ../../../app/stylesheets/base.sass */
      body ul.list li .item {
        margin-left: 80px; }
        /* line 337, ../../../app/stylesheets/base.sass */
        body ul.list li .item .avatar {
          float: left;
          margin: 0 5px 5px 0;
          width: 30px;
          height: 30px; }
  /* line 343, ../../../app/stylesheets/base.sass */
  body .actions-bar {
    padding: 10px 1px; }
    /* line 346, ../../../app/stylesheets/base.sass */
    body .actions-bar .actions {
      float: left; }
    /* line 349, ../../../app/stylesheets/base.sass */
    body .actions-bar .pagination {
      float: right;
      padding: 1px 0; }

redpajama_set_name: RedPajamaGithub


text:
import type { CSSProperties, ReactNode } from "react";
import { forwardRef } from "react";
import cn from "classnames";
import { UnstyledButton } from "@react-md/button";
import type { TextIconSpacingProps } from "@react-md/icon";
import { IconRotator, TextIconSpacing } from "@react-md/icon";
import { bem } from "@react-md/utils";

import type { TableCellHorizontalAlignment } from "./config";

export type SortOrder = "ascending" | "descending" | "none" | "other";

export interface TableCellContentProps extends TextIconSpacingProps {
  /**
   * An optional id for the sort order button.
   */
  id?: string;

  /**
   * An optional style for the sort order button.
   */
  style?: CSSProperties;

  /**
   * The current sort order for this cell. Setting this to `null` will prevent
   * the button from being rendered.
   */
  sortOrder?: SortOrder;

  /**
   * Boolean if the icon should be rotated.
   */
  rotated?: boolean;

  /**
   * @remarks \@since 4.0.3
   * @see {@link TableCellHorizontalAlignment}
   */
  hAlign?: TableCellHorizontalAlignment;
}

const block = bem("rmd-table-cell");

/**
 * This is mostly an internal component since it is automatically used within
 * the `TableCell` component but this will conditionally wrap the `children`
 * within an `UnstyledButton` to make a clickable cell. This is really to help
 * with sort behavior within headers.
 */
export const TableCellContent = forwardRef<
  HTMLButtonElement,
  TableCellContentProps
>(function TableCellContent(
  {
    id,
    icon: propIcon,
    style,
    className,
    sortOrder,
    children,
    rotated: propRotated,
    hAlign = "left",
    ...props
  },
  ref
) {
  if (!sortOrder || propIcon === null) {
    return <>{children}</>;
  }

  let icon: ReactNode = null;
  if (sortOrder !== "none") {
    const rotated = propRotated ?? sortOrder === "descending";

    icon = <IconRotator rotated={rotated}>{propIcon}</IconRotator>;
  }

  return (
    <UnstyledButton
      id={id}
      ref={ref}
      style={style}
      className={cn(
        block("child", {
          [hAlign]: hAlign !== "left",
        }),
        className
      )}
    >
      <TextIconSpacing {...props} icon={icon}>
        {children}
      </TextIconSpacing>
    </UnstyledButton>
  );
});


text:
<?php

/**
 * @license MIT
 */

namespace Birke\Rememberme\Token;

use RandomLib\Factory;
use RandomLib\Generator;

/**
 * A token class that uses ircmaxell/random-lib to generate secure random tokens
 */
class RandomLibToken extends AbstractToken
{
    /**
     * @var Generator
     */
    protected $generator;

    protected $formatMap;

    /**
     * @param int            $tokenBytes
     * @param string         $tokenFormat
     * @param Generator|null $generator
     */
    public function __construct($tokenBytes = 32, $tokenFormat = self::FORMAT_HEX, Generator $generator = null)
    {
        parent::__construct($tokenBytes, $tokenFormat);
        if (is_null($generator)) {
            $factory = new Factory();
            $this->generator = $factory->getMediumStrengthGenerator();
        } else {
            $this->generator = $generator;
        }
        $this->formatMap = [
            self::FORMAT_HEX => Generator::CHAR_LOWER_HEX,
            self::FORMAT_PLAIN => Generator::CHAR_BASE64,
            self::FORMAT_BASE64 => Generator::CHAR_BASE64,
        ];
    }

    /**
     * Generate a random, 32-byte Token
     * @return string
     */
    public function createToken()
    {
        return $this->generator->generateString($this->tokenBytes, $this->formatMap[$this->tokenFormat]);
    }
}


text:
import json
import shutil
import os

from .logger import Logger
from .wrapper import Wrapper

from .modifiers.ManifestVersionModifier import ManifestVersionModifier
from .modifiers.ServiceWorkerModifier import ServiceWorkerModifier
from .modifiers.ManifestActionModifier import ManifestActionModifier
from .modifiers.JSActionModifier import JSActionModifier
from .modifiers.ExecuteScriptModifier import ExecuteScriptModifier
from .modifiers.InsertCssModifier import InsertCssModifier
from .modifiers.WebAccessibleResourcesModifier import WebAccessibleResourcesModifier
from .modifiers.ContentSecurityPolicyModifier import ContentSecurityPolicyModifier
from .modifiers.HostPermissionsModifier import HostPermissionsModifier

class Worker:
  wrapper = None

  def work(self, source):
    Logger().log(source)

    # TODO: Can destination be a command line argument instead?
    destination = source + "_delete"
    if os.path.exists(destination):
      Logger().log("Overwriting already existing destination.", 1)
      # TODO: Should deletion be done by default?
      shutil.rmtree(destination)

    shutil.copytree(source, destination)
    manifest_path = destination + "/manifest.json"
    manifest = {}
    if not os.path.exists(manifest_path):
      Logger().log("Missing manifest", 0)
    else:
      if os.path.exists(manifest_path):
        with open(manifest_path) as json_file:
          manifest = json.load(json_file)
    self.wrapper = Wrapper(destination, manifest)

    [modifier.run() for modifier in [
      ManifestVersionModifier(self.wrapper),
      ServiceWorkerModifier(self.wrapper),
      ManifestActionModifier(self.wrapper),
      JSActionModifier(self.wrapper),
      ExecuteScriptModifier(self.wrapper),
      InsertCssModifier(self.wrapper),
      WebAccessibleResourcesModifier(self.wrapper),
      ContentSecurityPolicyModifier(self.wrapper),
      HostPermissionsModifier(self.wrapper),
    ]]


text: require 'radar/app'


text:
package org.deeplearning4j.nn.conf.graph;

import lombok.Data;
import org.deeplearning4j.nn.conf.inputs.InputType;
import org.deeplearning4j.nn.conf.inputs.InvalidInputTypeException;
import org.deeplearning4j.nn.conf.memory.LayerMemoryReport;
import org.deeplearning4j.nn.conf.memory.MemoryReport;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.shade.jackson.annotation.JsonProperty;

/** An ElementWiseVertex is used to combine the activations of two or more layer in an element-wise manner<br>
 * For example, the activations may be combined by addition, subtraction or multiplication or by selecting the maximum.
 * Addition, Average, Max and Product may use an arbitrary number of input arrays. Note that in the case of subtraction, only two inputs may be used.
 * @author Alex Black
 */
@Data
public class ElementWiseVertex extends GraphVertex {

    public ElementWiseVertex(@JsonProperty("op") Op op) {
        this.op = op;
    }

    public enum Op {
        Add, Subtract, Product, Average, Max
    }

    protected Op op;

    @Override
    public ElementWiseVertex clone() {
        return new ElementWiseVertex(op);
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof ElementWiseVertex))
            return false;
        return ((ElementWiseVertex) o).op == op;
    }

    @Override
    public int hashCode() {
        return op.hashCode();
    }

    @Override
    public int numParams(boolean backprop) {
        return 0;
    }

    @Override
    public int minVertexInputs() {
        return 2;
    }

    @Override
    public int maxVertexInputs() {
        switch (op) {
            case Add:
            case Average:
            case Product:
            case Max:
                //No upper bound
                return Integer.MAX_VALUE;
            case Subtract:
                return 2;
            default:
                throw new UnsupportedOperationException("Unknown op: " + op);
        }
    }

    @Override
    public org.deeplearning4j.nn.graph.vertex.GraphVertex instantiate(ComputationGraph graph, String name, int idx,
                    INDArray paramsView, boolean initializeParams) {
        org.deeplearning4j.nn.graph.vertex.impl.ElementWiseVertex.Op op;
        switch (this.op) {
            case Add:
                op = org.deeplearning4j.nn.graph.vertex.impl.ElementWiseVertex.Op.Add;
                break;
            case Average:
                op = org.deeplearning4j.nn.graph.vertex.impl.ElementWiseVertex.Op.Average;
                break;
            case Subtract:
                op = org.deeplearning4j.nn.graph.vertex.impl.ElementWiseVertex.Op.Subtract;
                break;
            case Product:
                op = org.deeplearning4j.nn.graph.vertex.impl.ElementWiseVertex.Op.Product;
                break;
            case Max:
                op = org.deeplearning4j.nn.graph.vertex.impl.ElementWiseVertex.Op.Max;
                break;
            default:
                throw new RuntimeException();
        }
        return new org.deeplearning4j.nn.graph.vertex.impl.ElementWiseVertex(graph, name, idx, op);
    }

    @Override
    public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException {
        if (vertexInputs.length == 1)
            return vertexInputs[0];
        InputType first = vertexInputs[0];
        if (first.getType() != InputType.Type.CNN) {
            //FF, RNN or flat CNN data inputs
            for (int i = 1; i < vertexInputs.length; i++) {
                if (vertexInputs[i].getType() != first.getType()) {
                    throw new InvalidInputTypeException(
                                    "Invalid input: ElementWise vertex cannot process activations of different types:"
                                                    + " first type = " + first.getType() + ", input type " + (i + 1)
                                                    + " = " + vertexInputs[i].getType());
                }
            }
        } else {
            //CNN inputs... also check that the depth, width and heights match:
            InputType.InputTypeConvolutional firstConv = (InputType.InputTypeConvolutional) first;
            int fd = firstConv.getDepth();
            int fw = firstConv.getWidth();
            int fh = firstConv.getHeight();

            for (int i = 1; i < vertexInputs.length; i++) {
                if (vertexInputs[i].getType() != InputType.Type.CNN) {
                    throw new InvalidInputTypeException(
                                    "Invalid input: ElementWise vertex cannot process activations of different types:"
                                                    + " first type = " + InputType.Type.CNN + ", input type " + (i + 1)
                                                    + " = " + vertexInputs[i].getType());
                }

                InputType.InputTypeConvolutional otherConv = (InputType.InputTypeConvolutional) vertexInputs[i];

                int od = otherConv.getDepth();
                int ow = otherConv.getWidth();
                int oh = otherConv.getHeight();

                if (fd != od || fw != ow || fh != oh) {
                    throw new InvalidInputTypeException(
                                    "Invalid input: ElementWise vertex cannot process CNN activations of different sizes:"
                                                    + "first [depth,width,height] = [" + fd + "," + fw + "," + fh
                                                    + "], input " + i + " = [" + od + "," + ow + "," + oh + "]");
                }
            }
        }
        return first; //Same output shape/size as
    }

    @Override
    public MemoryReport getMemoryReport(InputType... inputTypes) {
        //No working memory in addition to output activations
        return new LayerMemoryReport.Builder(null, ElementWiseVertex.class, inputTypes[0], inputTypes[0])
                        .standardMemory(0, 0).workingMemory(0, 0, 0, 0).cacheMemory(0, 0).build();
    }
}


text:
from setuptools import setup
import os
import sys

if sys.argv[-1] == 'publish':
    os.system('python setup.py sdist upload')
    os.system('python setup.py upload_docs')
    sys.exit()

here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.rst')).read()

version = open("VERSION").readline().strip()

install_requires = [
    # List your project dependencies here.
    # For more details, see:
    # http://packages.python.org/distribute/setuptools.html#declaring-dependencies
    'mando',
    'python-dateutil >= 2.1',
    'pandas >= 0.9.0',
    'tstoolbox >= 0.8',
]

setup(name='swmmtoolbox',
      version=version,
      description="The swmmtoolbox extracts data from the Storm Water Management Model 5 binary output file.",
      long_description=README,
      classifiers=[
          # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
          'Development Status :: 5 - Production/Stable',
          'Environment :: Console',
          'Intended Audience :: End Users/Desktop',
          'Intended Audience :: Developers',
          'License :: OSI Approved :: BSD License',
          'Natural Language :: English',
          'Operating System :: OS Independent',
          'Programming Language :: Python :: 2',
          'Programming Language :: Python :: 3',
          'Topic :: Scientific/Engineering',
          'Topic :: Software Development :: Libraries :: Python Modules',
      ],
      keywords='stormwater model water hydrology hydraulics',
      author='Tim Cera, P.E.',
      author_email='tim@cerazone.net',
      url='',
      packages=['swmmtoolbox'],
      package_dir={'': '.'},
      include_package_data=True,
      zip_safe=False,
      install_requires=install_requires,
      entry_points={
          'console_scripts':
              ['swmmtoolbox=swmmtoolbox.swmmtoolbox:main']
      }
      )


text:
package com.web.program;

import java.util.ArrayList;
import java.util.List;

import com.web.program.Program.ProgramType;
import com.web.tools.FormatingTools;

public class AnalysisConfigStore implements Writable, ConfigStore{

	private String aAction;
	private Header aHeader;
	private String aName;

	public AnalysisConfigStore(String pAction)  {
		aAction = FormatingTools.parseByCamelCase(pAction);
		aHeader = new Header(ProgramType.FRONT);
		aHeader.addModule(new FrontEndModule("Dispatcher","../Dispatcher"));
		aHeader.addModule(new FrontEndModule("AT","../constants/AT"));
		aHeader.addModule(new FrontEndModule("{Store}", "flux/utils"));

		aName = pAction + "ConfigStore";
	}

	/* (non-Javadoc)
	 * @see com.web.program.ConfigStore#getName()
	 */
	@Override
	public String getName() {
		return aName;
	}

	@Override
	public String[] toFile() {
		List<String> aLines = new ArrayList<String>();

		for (String s : aHeader.toFile()) {
			aLines.add(s);
		}

		aLines.add("class " + aName + " extends Store {");
		aLines.add("\tconstructor(dispatcher) {");
		aLines.add("\t\tsuper(dispatcher);");
		aLines.add("\t\tthis._argumentList = immutable.List();");
		aLines.add("\t}");

		aLines.add("\t__onDispatch(payload) {");
		aLines.add("\t\tswitch (payload.action) {");
		aLines.add("\t\t\tcase AT." + aAction + "PANEL.ADD_ARGUMENT:");
		aLines.add("\t\t\t\tthis._argumentList =  this._argumentList.push(payload.data['arg']);");
		aLines.add("\t\t\t\tthis.__emitChange();");
		aLines.add("\t\t\t\tbreak;");
		aLines.add("\t\t\tcase AT." + aAction + "PANEL.EDIT_ARGUMENT:");
		aLines.add("\t\t\t\tthis._argumentList =  this._argumentList.set(");
		aLines.add("\t\t\t\t\tpayload.data.argIndex,");
		aLines.add("\t\t\t\t\tpayload.data.arg");
		aLines.add("\t\t\t\t);");
		aLines.add("\t\t\t\tthis.__emitChange();");
		aLines.add("\t\t\t\tbreak;");
		aLines.add("\t\t}");
		aLines.add("\t}");
		aLines.add("\tgetArgumentList() {");
		aLines.add("\t\treturn this._argumentList;");
		aLines.add("\t}");
		aLines.add("}");

		aLines.add("module.exports = new " + aName + "(Dispatcher);");

		return aLines.toArray(new String[aLines.size()]);
	}

	@Override
	public String[] getPartial(PartialParts pPart) {
		// TODO Auto-generated method stub
		return null;
	}
}


text:
package com.koch.ambeth.security.server.privilege;

import com.koch.ambeth.util.collections.IList;

public interface IEntityTypePermissionRuleProvider {
	IList<IEntityTypePermissionRule> getEntityTypePermissionRules(Class<?> entityType);
}


text:
package com.twitter.finagle.netty4.framer

import com.twitter.finagle.Failure
import com.twitter.finagle.framer.Framer
import com.twitter.io.Buf
import io.netty.channel.embedded.EmbeddedChannel
import io.netty.channel.{ChannelHandlerContext, ChannelInboundHandlerAdapter}
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar
import scala.collection.mutable.ArrayBuffer

@RunWith(classOf[JUnitRunner])
class FrameHandlerTest extends FunSuite with MockitoSugar {

  object FixedFramer extends TestFramer(4)

  def toBuf(s: String): Buf = Buf.Utf8(s)
  def toStr(b: Buf): String = Buf.Utf8.unapply(b).get

  test("FrameHandler handles frames") {
    val messagesSeen = new ArrayBuffer[String]
    var nonStringsMessageCount = 0
    val readSnooper = new ChannelInboundHandlerAdapter {
      override def channelRead(ctx: ChannelHandlerContext, msg: scala.Any): Unit = {
        msg match {
          case b: Buf =>
            messagesSeen.append(toStr(b))
          case _ =>
            nonStringsMessageCount += 1
        }
        super.channelRead(ctx, msg)
      }
    }
    val ch = new EmbeddedChannel(new FrameHandler(FixedFramer), readSnooper)
    ch.pipeline.fireChannelActive

    ch.writeInbound(toBuf("hi"))
    assert(nonStringsMessageCount == 0)
    assert(messagesSeen.isEmpty)

    ch.writeInbound(toBuf("2u"))
    assert(nonStringsMessageCount == 0)
    assert(messagesSeen.head == "hi2u")

    ch.writeInbound(toBuf("a big string"))
    assert(nonStringsMessageCount == 0)
    assert(messagesSeen.drop(1) == "a big string".grouped(4).toList)
  }

  test("FrameHandler doesn't swallow exceptions thrown by decoder") {
    val exnThrown = new Exception("boom")
    val failingFramer = new Framer {
      def apply(buf: Buf): IndexedSeq[Buf] = throw exnThrown
    }

    val ch = new EmbeddedChannel(new FrameHandler(failingFramer))
    ch.pipeline.fireChannelActive
    val exnSeen = intercept[Exception] { ch.writeInbound(Buf.Empty) }
    assert(exnThrown == exnSeen)
  }

  test("FrameHandler throws exceptions when handling non-Buf messages") {
    val notABuf = "I'm definitely not a buf"
    val noopFramer = new Framer {
      def apply(b: Buf) = { IndexedSeq.empty[Buf] }
    }

    val ch = new EmbeddedChannel(new FrameHandler(noopFramer))
    ch.pipeline.fireChannelActive
    intercept[Failure] { ch.writeInbound("Not a buf") }
  }
}


text:
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'wkh#(3wt62^^xwnxknc=rhjxy(k=^tuk5scmd=8i3dmw!q4o_#'

# When testing PayPal notification message handling the address generated by
# ngrok has to be added to ALLOWED_HOSTS
# ALLOWED_HOSTS = ['127.0.0.1', '<NGROK-HERE>.ngrok.io']
ALLOWED_HOSTS = []

# our site id to use with flatpages
# (normally 2, im some rare cases higher, dependant on num of sites in table)
SITE_ID = 5

INTERNAL_IPS = ('127.0.0.1',)

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.flatpages',
    'django_forms_bootstrap',
    'paypal.standard.ipn',
    'rest_framework',
    'tinymce',
    'emoticons',
    'disqus',
    'reusable_blog',
    'hello',
    'accounts',
    'paypal_store',
    'products',
    'magazines',
    'threads',
    'polls',
]

# tell Django that we want to use this class as our User class
AUTH_USER_MODEL = 'accounts.User'

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'accounts.backends.EmailAuth',
)

LOGIN_URL = '/login/'

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
]

ROOT_URLCONF = 'we_are_social.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'we_are_social.wsgi.application'

# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME':
        'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME':
        'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME':
        'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME':
        'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

# register our static directory
STATIC_URL = '/static/'
STATIC_ROOT = ''

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

# register our media directory
MEDIA_URL = '/media/'
MEDIA_ROOT = (
    os.path.join(BASE_DIR, 'media')
)

# tinymce settings
TINYMCE_JS_ROOT = (
    os.path.join(BASE_DIR, "static", 'js', 'tinymce', 'tinymce.min.js')
)


text:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__, instance_relative_config=True)
app.config.from_object('wikiapi.config')
app.config.from_pyfile('application.cfg', silent=True)
db = SQLAlchemy(app)

from wikiapi import views, models

db.create_all()


text:
layout: page
title: "About Us"
header:
   image_fullwidth: "people.png"
permalink: "/about/"
---

    The IoT Initiative is a joint effort by a number of organisations from within the University of Edinburgh, including Information Services, the School of Informatics, and Corporate Services. It is supported by City of Edinburgh Council and several private sector organisations.

IoT Programme Board

            The Programme Board is responsible for overseeing the University's IoT Programme and for giving consideration and guidance regarding programme implementation activities and projects,
            ensuring these deliver against the IoT Programme Strategy.

IoT Action Groups

        	The IoT Initiative is multi-disciplinary and is being delivered with expertise and oversight drawn from across the University through a number of focused Action Groups:

Security and Privacy

Governance and Ethics

Marketing and Engagement

Teaching and Learning

Coordination Group

           The Coordination Group (CoG) undertakes informal knowledge-sharing and monitoring of the Action Groups.

Informatics IoT Research

                 The School of Informatics has established an internal IoT Research Programme to coordinate relevant activities. The Programme is led by Paul Patras and Rik Sarkar.


text:
package tour;

import com.mongodb.client.model.BulkWriteOptions;
import com.mongodb.client.model.DeleteOneModel;
import com.mongodb.client.model.InsertOneModel;
import com.mongodb.client.model.ReplaceOneModel;
import com.mongodb.client.model.UpdateOneModel;
import com.mongodb.client.model.WriteModel;
import com.mongodb.rx.client.MongoClient;
import com.mongodb.rx.client.MongoClients;
import com.mongodb.rx.client.MongoCollection;
import com.mongodb.rx.client.MongoDatabase;
import com.mongodb.rx.client.Success;
import org.bson.Document;
import rx.observers.TestSubscriber;

import java.util.ArrayList;
import java.util.List;

import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Filters.exists;
import static com.mongodb.client.model.Filters.gt;
import static com.mongodb.client.model.Filters.gte;
import static com.mongodb.client.model.Filters.lt;
import static com.mongodb.client.model.Filters.lte;
import static com.mongodb.client.model.Projections.excludeId;
import static com.mongodb.client.model.Sorts.descending;
import static java.util.concurrent.TimeUnit.SECONDS;
import static tour.SubscriberHelpers.printDocumentSubscriber;
import static tour.SubscriberHelpers.printSubscriber;

/**
 * The QuickTour code example see: https://mongodb.github.io/mongo-java-driver-reactivestreams/1.0/getting-started
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public final class QuickTour {

    /**
     * Run this main method to see the output of this quick example.
     *
     * @param args takes an optional single argument for the connection string
     * @throws Throwable if an operation fails
     */
    public static void main(final String[] args) throws Throwable {
        MongoClient mongoClient;

        if (args.length == 0) {
            // connect to the local database server
            mongoClient = MongoClients.create();
        } else {
            mongoClient = MongoClients.create(args[0]);
        }

        // get handle to "mydb" database
        MongoDatabase database = mongoClient.getDatabase("mydb");

        // get a handle to the "test" collection
        MongoCollection<Document> collection = database.getCollection("test");

        // drop all the data in it
        TestSubscriber subscriber = new TestSubscriber();
        collection.drop().subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        // make a document and insert it
        Document doc = new Document("name", "MongoDB")
                .append("type", "database")
                .append("count", 1)
                .append("info", new Document("x", 203).append("y", 102));

        collection.insertOne(doc).timeout(10, SECONDS).toBlocking().single();

        // get it (since it's the only one in there since we dropped the rest earlier on)
        collection.find().first().subscribe(printDocumentSubscriber());

        // now, lets add lots of little documents to the collection so we can explore queries and cursors
        List<Document> documents = new ArrayList<Document>();
        for (int i = 0; i < 100; i++) {
            documents.add(new Document("i", i));
        }

        collection.insertMany(documents).timeout(10, SECONDS).toBlocking().single();

        subscriber = printSubscriber("total # of documents after inserting 100 small ones (should be 101): ");
        collection.count().subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        subscriber = printDocumentSubscriber();
        collection.find().first().subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        subscriber = printDocumentSubscriber();
        collection.find().subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        // Query Filters
        // now use a query to get 1 document out

        collection.find(eq("i", 71)).first().subscribe(printDocumentSubscriber());
        // now use a range query to get a larger subset
        collection.find(gt("i", 50)).subscribe(printDocumentSubscriber());

        // range query with multiple constraints
        collection.find(and(gt("i", 50), lte("i", 100))).subscribe(printDocumentSubscriber());

        // Sorting
        collection.find(exists("i")).sort(descending("i")).first().subscribe(printDocumentSubscriber());

        // Projection
        collection.find().projection(excludeId()).first().subscribe(printDocumentSubscriber());

        // Update One
        subscriber = printSubscriber("Update Result: ");
        collection.updateOne(eq("i", 10), new Document("$set", new Document("i", 110))).subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        // Update Many
        subscriber = printSubscriber("Update Result: ");
        collection.updateMany(lt("i", 100), new Document("$inc", new Document("i", 100))).subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        // Delete One
        subscriber = printSubscriber("Delete Result: ");
        collection.deleteOne(eq("i", 110)).subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        // Delete Many
        subscriber = printSubscriber("Delete Result: ");
        collection.deleteMany(gte("i", 100)).subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        subscriber = new TestSubscriber();
        collection.drop().subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        // ordered bulk writes
        List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
        writes.add(new InsertOneModel<Document>(new Document("_id", 4)));
        writes.add(new InsertOneModel<Document>(new Document("_id", 5)));
        writes.add(new InsertOneModel<Document>(new Document("_id", 6)));
        writes.add(new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2))));
        writes.add(new DeleteOneModel<Document>(new Document("_id", 2)));
        writes.add(new ReplaceOneModel<Document>(new Document("_id", 3), new Document("_id", 3).append("x", 4)));

        subscriber = printSubscriber("Bulk write results: ");
        collection.bulkWrite(writes).subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        subscriber = new TestSubscriber();
        collection.drop().subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        subscriber = printSubscriber("Bulk write results: ");
        collection.bulkWrite(writes, new BulkWriteOptions().ordered(false)).subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        subscriber = printDocumentSubscriber();
        collection.find().subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        // Clean up
        subscriber = new TestSubscriber<Success>();
        collection.drop().subscribe(subscriber);
        subscriber.awaitTerminalEvent();

        // release resources
        mongoClient.close();
    }

    private QuickTour() {
    }
}


text:
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package types

import (
	"bytes"
	"encoding/base64"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"math"
	"reflect"
	"strconv"
	"strings"
	"time"
	"unicode/utf8"

	"github.com/pingcap/errors"
	"github.com/pingcap/tidb/parser/mysql"
	"github.com/pingcap/tidb/parser/terror"
	"github.com/pingcap/tidb/util/hack"
	"golang.org/x/exp/slices"
)

/*
   The binary JSON format from MySQL 5.7 is as follows:

   JSON doc ::= type value
   type ::=
       0x01 |       // large JSON object
       0x03 |       // large JSON array
       0x04 |       // literal (true/false/null)
       0x05 |       // int16
       0x06 |       // uint16
       0x07 |       // int32
       0x08 |       // uint32
       0x09 |       // int64
       0x0a |       // uint64
       0x0b |       // double
       0x0c |       // utf8mb4 string
       0x0d |       // opaque value
       0x0e |       // date
       0x0f |       // datetime
       0x10 |       // timestamp
       0x11 |       // time

   value ::=
       object  |
       array   |
       literal |
       number  |
       string  |
       opaque  |
       time    |
       duration |

   object ::= element-count size key-entry* value-entry* key* value*

   array ::= element-count size value-entry* value*

   // number of members in object or number of elements in array
   element-count ::= uint32

   // number of bytes in the binary representation of the object or array
   size ::= uint32

   key-entry ::= key-offset key-length

   key-offset ::= uint32

   key-length ::= uint16    // key length must be less than 64KB

   value-entry ::= type offset-or-inlined-value

   // This field holds either the offset to where the value is stored,
   // or the value itself if it is small enough to be inlined (that is,
   // if it is a JSON literal or a small enough [u]int).
   offset-or-inlined-value ::= uint32

   key ::= utf8mb4-data

   literal ::=
       0x00 |   // JSON null literal
       0x01 |   // JSON true literal
       0x02 |   // JSON false literal

   number ::=  ....    // little-endian format for [u]int(16|32|64), whereas
                       // double is stored in a platform-independent, eight-byte
                       // format using float8store()

   string ::= data-length utf8mb4-data

   data-length ::= uint8*    // If the high bit of a byte is 1, the length
                             // field is continued in the next byte,
                             // otherwise it is the last byte of the length
                             // field. So we need 1 byte to represent
                             // lengths up to 127, 2 bytes to represent
                             // lengths up to 16383, and so on...

   opaque ::= typeId data-length byte*

   time ::= uint64

   duration ::= uint64 uint32

   typeId ::= byte
*/

var jsonZero = CreateBinaryJSON(uint64(0))

const maxJSONDepth = 100

// BinaryJSON represents a binary encoded JSON object.
// It can be randomly accessed without deserialization.
type BinaryJSON struct {
	TypeCode JSONTypeCode
	Value    []byte
}

// String implements fmt.Stringer interface.
func (bj BinaryJSON) String() string {
	out, err := bj.MarshalJSON()
	terror.Log(err)
	return string(out)
}

// Copy makes a copy of the BinaryJSON
func (bj BinaryJSON) Copy() BinaryJSON {
	buf := make([]byte, len(bj.Value))
	copy(buf, bj.Value)
	return BinaryJSON{TypeCode: bj.TypeCode, Value: buf}
}

// MarshalJSON implements the json.Marshaler interface.
func (bj BinaryJSON) MarshalJSON() ([]byte, error) {
	buf := make([]byte, 0, len(bj.Value)*3/2)
	return bj.marshalTo(buf)
}

func (bj BinaryJSON) marshalTo(buf []byte) ([]byte, error) {
	switch bj.TypeCode {
	case JSONTypeCodeOpaque:
		return jsonMarshalOpaqueTo(buf, bj.GetOpaque()), nil
	case JSONTypeCodeString:
		return jsonMarshalStringTo(buf, bj.GetString()), nil
	case JSONTypeCodeLiteral:
		return jsonMarshalLiteralTo(buf, bj.Value[0]), nil
	case JSONTypeCodeInt64:
		return strconv.AppendInt(buf, bj.GetInt64(), 10), nil
	case JSONTypeCodeUint64:
		return strconv.AppendUint(buf, bj.GetUint64(), 10), nil
	case JSONTypeCodeFloat64:
		return bj.marshalFloat64To(buf)
	case JSONTypeCodeArray:
		return bj.marshalArrayTo(buf)
	case JSONTypeCodeObject:
		return bj.marshalObjTo(buf)
	case JSONTypeCodeDate, JSONTypeCodeDatetime, JSONTypeCodeTimestamp:
		return jsonMarshalTimeTo(buf, bj.GetTime()), nil
	case JSONTypeCodeDuration:
		return jsonMarshalDurationTo(buf, bj.GetDuration()), nil
	}
	return buf, nil
}

// IsZero return a boolean indicate whether BinaryJSON is Zero
func (bj BinaryJSON) IsZero() bool {
	// This behavior is different on MySQL 5.7 and 8.0
	//
	// In MySQL 5.7, most of these non-integer values are 0, and return a warning:
	// "Invalid JSON value for CAST to INTEGER from column j"
	//
	// In MySQL 8, most of these non-integer values are not zero, with a warning:
	// > "Evaluating a JSON value in SQL boolean context does an implicit comparison
	// > against JSON integer 0; if this is not what you want, consider converting
	// > JSON to a SQL numeric type with JSON_VALUE RETURNING"
	//
	// TODO: return a warning as MySQL 8 does

	return CompareBinaryJSON(bj, jsonZero) == 0
}

// GetInt64 gets the int64 value.
func (bj BinaryJSON) GetInt64() int64 {
	return int64(jsonEndian.Uint64(bj.Value))
}

// GetUint64 gets the uint64 value.
func (bj BinaryJSON) GetUint64() uint64 {
	return jsonEndian.Uint64(bj.Value)
}

// GetFloat64 gets the float64 value.
func (bj BinaryJSON) GetFloat64() float64 {
	return math.Float64frombits(bj.GetUint64())
}

// GetString gets the string value.
func (bj BinaryJSON) GetString() []byte {
	strLen, lenLen := binary.Uvarint(bj.Value)
	return bj.Value[lenLen : lenLen+int(strLen)]
}

// Opaque represents a raw binary type
type Opaque struct {
	// TypeCode is the same with database type code
	TypeCode byte
	// Buf is the underlying bytes of the data
	Buf []byte
}

// GetOpaque gets the opaque value
func (bj BinaryJSON) GetOpaque() Opaque {
	typ := bj.Value[0]

	strLen, lenLen := binary.Uvarint(bj.Value[1:])
	bufStart := lenLen + 1
	return Opaque{
		TypeCode: typ,
		Buf:      bj.Value[bufStart : bufStart+int(strLen)],
	}
}

// GetTime gets the time value
func (bj BinaryJSON) GetTime() Time {
	coreTime := CoreTime(bj.GetUint64())

	tp := mysql.TypeDate
	if bj.TypeCode == JSONTypeCodeDatetime {
		tp = mysql.TypeDatetime
	} else if bj.TypeCode == JSONTypeCodeTimestamp {
		tp = mysql.TypeTimestamp
	}

	return NewTime(coreTime, tp, DefaultFsp)
}

// GetDuration gets the duration value
func (bj BinaryJSON) GetDuration() Duration {
	return Duration{
		time.Duration(bj.GetInt64()),
		int(jsonEndian.Uint32(bj.Value[8:])),
	}
}

// GetOpaqueFieldType returns the type of opaque value
func (bj BinaryJSON) GetOpaqueFieldType() byte {
	return bj.Value[0]
}

// GetKeys gets the keys of the object
func (bj BinaryJSON) GetKeys() BinaryJSON {
	count := bj.GetElemCount()
	ret := make([]BinaryJSON, 0, count)
	for i := 0; i < count; i++ {
		ret = append(ret, CreateBinaryJSON(string(bj.objectGetKey(i))))
	}
	return buildBinaryJSONArray(ret)
}

// GetElemCount gets the count of Object or Array.
func (bj BinaryJSON) GetElemCount() int {
	return int(jsonEndian.Uint32(bj.Value))
}

func (bj BinaryJSON) arrayGetElem(idx int) BinaryJSON {
	return bj.valEntryGet(headerSize + idx*valEntrySize)
}

func (bj BinaryJSON) objectGetKey(i int) []byte {
	keyOff := int(jsonEndian.Uint32(bj.Value[headerSize+i*keyEntrySize:]))
	keyLen := int(jsonEndian.Uint16(bj.Value[headerSize+i*keyEntrySize+keyLenOff:]))
	return bj.Value[keyOff : keyOff+keyLen]
}

func (bj BinaryJSON) objectGetVal(i int) BinaryJSON {
	elemCount := bj.GetElemCount()
	return bj.valEntryGet(headerSize + elemCount*keyEntrySize + i*valEntrySize)
}

func (bj BinaryJSON) valEntryGet(valEntryOff int) BinaryJSON {
	tpCode := bj.Value[valEntryOff]
	valOff := jsonEndian.Uint32(bj.Value[valEntryOff+valTypeSize:])
	switch tpCode {
	case JSONTypeCodeLiteral:
		return BinaryJSON{TypeCode: JSONTypeCodeLiteral, Value: bj.Value[valEntryOff+valTypeSize : valEntryOff+valTypeSize+1]}
	case JSONTypeCodeUint64, JSONTypeCodeInt64, JSONTypeCodeFloat64:
		return BinaryJSON{TypeCode: tpCode, Value: bj.Value[valOff : valOff+8]}
	case JSONTypeCodeString:
		strLen, lenLen := binary.Uvarint(bj.Value[valOff:])
		totalLen := uint32(lenLen) + uint32(strLen)
		return BinaryJSON{TypeCode: tpCode, Value: bj.Value[valOff : valOff+totalLen]}
	case JSONTypeCodeOpaque:
		strLen, lenLen := binary.Uvarint(bj.Value[valOff+1:])
		totalLen := 1 + uint32(lenLen) + uint32(strLen)
		return BinaryJSON{TypeCode: tpCode, Value: bj.Value[valOff : valOff+totalLen]}
	case JSONTypeCodeDate, JSONTypeCodeDatetime, JSONTypeCodeTimestamp:
		return BinaryJSON{TypeCode: tpCode, Value: bj.Value[valOff : valOff+8]}
	case JSONTypeCodeDuration:
		return BinaryJSON{TypeCode: tpCode, Value: bj.Value[valOff : valOff+12]}
	}
	dataSize := jsonEndian.Uint32(bj.Value[valOff+dataSizeOff:])
	return BinaryJSON{TypeCode: tpCode, Value: bj.Value[valOff : valOff+dataSize]}
}

func (bj BinaryJSON) marshalFloat64To(buf []byte) ([]byte, error) {
	// NOTE: copied from Go standard library.
	f := bj.GetFloat64()
	if math.IsInf(f, 0) || math.IsNaN(f) {
		return buf, &json.UnsupportedValueError{Str: strconv.FormatFloat(f, 'g', -1, 64)}
	}

	// Convert as if by ES6 number to string conversion.
	// This matches most other JSON generators.
	// See golang.org/issue/6384 and golang.org/issue/14135.
	// Like fmt %g, but the exponent cutoffs are different
	// and exponents themselves are not padded to two digits.
	abs := math.Abs(f)
	ffmt := byte('f')
	// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
	if abs != 0 {
		if abs < 1e-6 || abs >= 1e21 {
			ffmt = 'e'
		}
	}
	buf = strconv.AppendFloat(buf, f, ffmt, -1, 64)
	if ffmt == 'e' {
		// clean up e-09 to e-9
		n := len(buf)
		if n >= 4 && buf[n-4] == 'e' && buf[n-3] == '-' && buf[n-2] == '0' {
			buf[n-2] = buf[n-1]
			buf = buf[:n-1]
		}
	}
	return buf, nil
}

func (bj BinaryJSON) marshalArrayTo(buf []byte) ([]byte, error) {
	elemCount := int(jsonEndian.Uint32(bj.Value))
	buf = append(buf, '[')
	for i := 0; i < elemCount; i++ {
		if i != 0 {
			buf = append(buf, ", "...)
		}
		var err error
		buf, err = bj.arrayGetElem(i).marshalTo(buf)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	return append(buf, ']'), nil
}

func (bj BinaryJSON) marshalObjTo(buf []byte) ([]byte, error) {
	elemCount := int(jsonEndian.Uint32(bj.Value))
	buf = append(buf, '{')
	for i := 0; i < elemCount; i++ {
		if i != 0 {
			buf = append(buf, ", "...)
		}
		buf = jsonMarshalStringTo(buf, bj.objectGetKey(i))
		buf = append(buf, ": "...)
		var err error
		buf, err = bj.objectGetVal(i).marshalTo(buf)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	return append(buf, '}'), nil
}

func jsonMarshalStringTo(buf, s []byte) []byte {
	// NOTE: copied from Go standard library.
	// NOTE: keep in sync with string above.
	buf = append(buf, '"')
	start := 0
	for i := 0; i < len(s); {
		if b := s[i]; b < utf8.RuneSelf {
			if jsonSafeSet[b] {
				i++
				continue
			}
			if start < i {
				buf = append(buf, s[start:i]...)
			}
			switch b {
			case '\\', '"':
				buf = append(buf, '\\', b)
			case '\n':
				buf = append(buf, '\\', 'n')
			case '\r':
				buf = append(buf, '\\', 'r')
			case '\t':
				buf = append(buf, '\\', 't')
			default:
				// This encodes bytes < 0x20 except for \t, \n and \r.
				// If escapeHTML is set, it also escapes <, >, and &
				// because they can lead to security holes when
				// user-controlled strings are rendered into JSON
				// and served to some browsers.
				buf = append(buf, `\u00`...)
				buf = append(buf, jsonHexChars[b>>4], jsonHexChars[b&0xF])
			}
			i++
			start = i
			continue
		}
		c, size := utf8.DecodeRune(s[i:])
		if c == utf8.RuneError && size == 1 {
			if start < i {
				buf = append(buf, s[start:i]...)
			}
			buf = append(buf, `\ufffd`...)
			i += size
			start = i
			continue
		}
		// U+2028 is LINE SEPARATOR.
		// U+2029 is PARAGRAPH SEPARATOR.
		// They are both technically valid characters in JSON strings,
		// but don't work in JSONP, which has to be evaluated as JavaScript,
		// and can lead to security holes there. It is valid JSON to
		// escape them, so we do so unconditionally.
		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
		if c == '\u2028' || c == '\u2029' {
			if start < i {
				buf = append(buf, s[start:i]...)
			}
			buf = append(buf, `\u202`...)
			buf = append(buf, jsonHexChars[c&0xF])
			i += size
			start = i
			continue
		}
		i += size
	}
	if start < len(s) {
		buf = append(buf, s[start:]...)
	}
	buf = append(buf, '"')
	return buf
}

// opaque value will yield "base64:typeXX:<base64 encoded string>"
func jsonMarshalOpaqueTo(buf []byte, opaque Opaque) []byte {
	b64 := base64.StdEncoding.EncodeToString(opaque.Buf)
	output := fmt.Sprintf(`"base64:type%d:%s"`, opaque.TypeCode, b64)

	// as the base64 string is simple and predictable, it could be appended
	// to the buf directly.
	buf = append(buf, output...)
	return buf
}

func jsonMarshalLiteralTo(b []byte, litType byte) []byte {
	switch litType {
	case JSONLiteralFalse:
		return append(b, "false"...)
	case JSONLiteralTrue:
		return append(b, "true"...)
	case JSONLiteralNil:
		return append(b, "null"...)
	}
	return b
}

func jsonMarshalTimeTo(buf []byte, time Time) []byte {
	// printing json datetime/duration will always keep 6 fsp
	time.SetFsp(6)
	buf = append(buf, []byte(quoteJSONString(time.String()))...)
	return buf
}

func jsonMarshalDurationTo(buf []byte, duration Duration) []byte {
	// printing json datetime/duration will always keep 6 fsp
	duration.Fsp = 6
	buf = append(buf, []byte(quoteJSONString(duration.String()))...)
	return buf
}

// ParseBinaryJSONFromString parses a json from string.
func ParseBinaryJSONFromString(s string) (bj BinaryJSON, err error) {
	if len(s) == 0 {
		err = ErrInvalidJSONText.GenWithStackByArgs("The document is empty")
		return
	}
	data := hack.Slice(s)
	if !json.Valid(data) {
		err = ErrInvalidJSONText.GenWithStackByArgs("The document root must not be followed by other values.")
		return
	}
	if err = bj.UnmarshalJSON(data); err != nil && !ErrJSONObjectKeyTooLong.Equal(err) {
		err = ErrInvalidJSONText.GenWithStackByArgs(err)
	}
	return
}

// UnmarshalJSON implements the json.Unmarshaler interface.
func (bj *BinaryJSON) UnmarshalJSON(data []byte) error {
	var decoder = json.NewDecoder(bytes.NewReader(data))
	decoder.UseNumber()
	var in interface{}
	err := decoder.Decode(&in)
	if err != nil {
		return errors.Trace(err)
	}
	newBj, err := CreateBinaryJSONWithCheck(in)
	if err != nil {
		return errors.Trace(err)
	}
	bj.TypeCode = newBj.TypeCode
	bj.Value = newBj.Value
	return nil
}

// HashValue converts certain JSON values for aggregate comparisons.
// For example int64(3) == float64(3.0)
// Other than the numeric condition, this function has to construct a bidirectional map between hash value
// and the original representation
func (bj BinaryJSON) HashValue(buf []byte) []byte {
	switch bj.TypeCode {
	case JSONTypeCodeInt64:
		// Convert to a FLOAT if no precision is lost.
		// In the future, it will be better to convert to a DECIMAL value instead
		// See: https://github.com/pingcap/tidb/issues/9988
		if bj.GetInt64() == int64(float64(bj.GetInt64())) {
			buf = append(buf, JSONTypeCodeFloat64)
			buf = appendBinaryFloat64(buf, float64(bj.GetInt64()))
		} else {
			buf = append(buf, bj.TypeCode)
			buf = append(buf, bj.Value...)
		}
	case JSONTypeCodeUint64:
		if bj.GetUint64() == uint64(float64(bj.GetUint64())) {
			buf = append(buf, JSONTypeCodeFloat64)
			buf = appendBinaryFloat64(buf, float64(bj.GetUint64()))
		} else {
			buf = append(buf, bj.TypeCode)
			buf = append(buf, bj.Value...)
		}
	case JSONTypeCodeArray:
		// this hash value is bidirectional, because you can get the element one-by-one
		// and you know the end of it, as the elemCount is also appended here
		buf = append(buf, bj.TypeCode)
		elemCount := int(jsonEndian.Uint32(bj.Value))
		buf = append(buf, bj.Value[0:dataSizeOff]...)
		for i := 0; i < elemCount; i++ {
			buf = bj.arrayGetElem(i).HashValue(buf)
		}
	case JSONTypeCodeObject:
		// this hash value is bidirectional, because you can get the key using the json
		// string format, and get the value accordingly.
		buf = append(buf, bj.TypeCode)
		elemCount := int(jsonEndian.Uint32(bj.Value))
		buf = append(buf, bj.Value[0:dataSizeOff]...)
		for i := 0; i < elemCount; i++ {
			keyJSON := CreateBinaryJSON(string(bj.objectGetKey(i)))
			buf = append(buf, keyJSON.Value...)
			buf = bj.objectGetVal(i).HashValue(buf)
		}
	default:
		buf = append(buf, bj.TypeCode)
		buf = append(buf, bj.Value...)
	}
	return buf
}

// CreateBinaryJSON creates a BinaryJSON from interface.
func CreateBinaryJSON(in interface{}) BinaryJSON {
	bj, err := CreateBinaryJSONWithCheck(in)
	if err != nil {
		panic(err)
	}
	return bj
}

// CreateBinaryJSONWithCheck creates a BinaryJSON from interface with error check.
func CreateBinaryJSONWithCheck(in interface{}) (BinaryJSON, error) {
	typeCode, buf, err := appendBinaryJSON(nil, in)
	if err != nil {
		return BinaryJSON{}, err
	}
	bj := BinaryJSON{TypeCode: typeCode, Value: buf}
	// GetElemDepth always returns +1.
	if bj.GetElemDepth()-1 > maxJSONDepth {
		return BinaryJSON{}, ErrJSONDocumentTooDeep
	}
	return bj, nil
}

func appendBinaryJSON(buf []byte, in interface{}) (JSONTypeCode, []byte, error) {
	var typeCode byte
	var err error
	switch x := in.(type) {
	case nil:
		typeCode = JSONTypeCodeLiteral
		buf = append(buf, JSONLiteralNil)
	case bool:
		typeCode = JSONTypeCodeLiteral
		if x {
			buf = append(buf, JSONLiteralTrue)
		} else {
			buf = append(buf, JSONLiteralFalse)
		}
	case int64:
		typeCode = JSONTypeCodeInt64
		buf = appendBinaryUint64(buf, uint64(x))
	case uint64:
		typeCode = JSONTypeCodeUint64
		buf = appendBinaryUint64(buf, x)
	case float64:
		typeCode = JSONTypeCodeFloat64
		buf = appendBinaryFloat64(buf, x)
	case json.Number:
		typeCode, buf, err = appendBinaryNumber(buf, x)
		if err != nil {
			return typeCode, nil, errors.Trace(err)
		}
	case string:
		typeCode = JSONTypeCodeString
		buf = appendBinaryString(buf, x)
	case BinaryJSON:
		typeCode = x.TypeCode
		buf = append(buf, x.Value...)
	case []interface{}:
		typeCode = JSONTypeCodeArray
		buf, err = appendBinaryArray(buf, x)
		if err != nil {
			return typeCode, nil, errors.Trace(err)
		}
	case map[string]interface{}:
		typeCode = JSONTypeCodeObject
		buf, err = appendBinaryObject(buf, x)
		if err != nil {
			return typeCode, nil, errors.Trace(err)
		}
	case Opaque:
		typeCode = JSONTypeCodeOpaque
		buf = appendBinaryOpaque(buf, x)
	case Time:
		typeCode = JSONTypeCodeDate
		if x.Type() == mysql.TypeDatetime {
			typeCode = JSONTypeCodeDatetime
		} else if x.Type() == mysql.TypeTimestamp {
			typeCode = JSONTypeCodeTimestamp
		}
		buf = appendBinaryUint64(buf, uint64(x.CoreTime()))
	case Duration:
		typeCode = JSONTypeCodeDuration
		buf = appendBinaryUint64(buf, uint64(x.Duration))
		buf = appendBinaryUint32(buf, uint32(x.Fsp))
	default:
		msg := fmt.Sprintf(unknownTypeErrorMsg, reflect.TypeOf(in))
		err = errors.New(msg)
	}
	return typeCode, buf, err
}

func appendZero(buf []byte, length int) []byte {
	var tmp [8]byte
	rem := length % 8
	loop := length / 8
	for i := 0; i < loop; i++ {
		buf = append(buf, tmp[:]...)
	}
	for i := 0; i < rem; i++ {
		buf = append(buf, 0)
	}
	return buf
}

func appendUint32(buf []byte, v uint32) []byte {
	var tmp [4]byte
	jsonEndian.PutUint32(tmp[:], v)
	return append(buf, tmp[:]...)
}

func appendBinaryNumber(buf []byte, x json.Number) (JSONTypeCode, []byte, error) {
	// The type interpretation process is as follows:
	// - Attempt float64 if it contains Ee.
	// - Next attempt int64
	// - Then uint64 (valid in MySQL JSON, not in JSON decode library)
	// - Then float64
	// - Return an error
	if strings.ContainsAny(string(x), "Ee.") {
		f64, err := x.Float64()
		if err != nil {
			return JSONTypeCodeFloat64, nil, errors.Trace(err)
		}
		return JSONTypeCodeFloat64, appendBinaryFloat64(buf, f64), nil
	} else if val, err := x.Int64(); err == nil {
		return JSONTypeCodeInt64, appendBinaryUint64(buf, uint64(val)), nil
	} else if val, err := strconv.ParseUint(string(x), 10, 64); err == nil {
		return JSONTypeCodeUint64, appendBinaryUint64(buf, val), nil
	}
	val, err := x.Float64()
	if err == nil {
		return JSONTypeCodeFloat64, appendBinaryFloat64(buf, val), nil
	}
	var typeCode JSONTypeCode
	return typeCode, nil, errors.Trace(err)
}

func appendBinaryString(buf []byte, v string) []byte {
	begin := len(buf)
	buf = appendZero(buf, binary.MaxVarintLen64)
	lenLen := binary.PutUvarint(buf[begin:], uint64(len(v)))
	buf = buf[:len(buf)-binary.MaxVarintLen64+lenLen]
	buf = append(buf, v...)
	return buf
}

func appendBinaryOpaque(buf []byte, v Opaque) []byte {
	buf = append(buf, v.TypeCode)

	lenBegin := len(buf)
	buf = appendZero(buf, binary.MaxVarintLen64)
	lenLen := binary.PutUvarint(buf[lenBegin:], uint64(len(v.Buf)))

	buf = buf[:len(buf)-binary.MaxVarintLen64+lenLen]
	buf = append(buf, v.Buf...)
	return buf
}

func appendBinaryFloat64(buf []byte, v float64) []byte {
	off := len(buf)
	buf = appendZero(buf, 8)
	jsonEndian.PutUint64(buf[off:], math.Float64bits(v))
	return buf
}

func appendBinaryUint64(buf []byte, v uint64) []byte {
	off := len(buf)
	buf = appendZero(buf, 8)
	jsonEndian.PutUint64(buf[off:], v)
	return buf
}

func appendBinaryUint32(buf []byte, v uint32) []byte {
	off := len(buf)
	buf = appendZero(buf, 4)
	jsonEndian.PutUint32(buf[off:], v)
	return buf
}

func appendBinaryArray(buf []byte, array []interface{}) ([]byte, error) {
	docOff := len(buf)
	buf = appendUint32(buf, uint32(len(array)))
	buf = appendZero(buf, dataSizeOff)
	valEntryBegin := len(buf)
	buf = appendZero(buf, len(array)*valEntrySize)
	for i, val := range array {
		var err error
		buf, err = appendBinaryValElem(buf, docOff, valEntryBegin+i*valEntrySize, val)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	docSize := len(buf) - docOff
	jsonEndian.PutUint32(buf[docOff+dataSizeOff:], uint32(docSize))
	return buf, nil
}

func appendBinaryValElem(buf []byte, docOff, valEntryOff int, val interface{}) ([]byte, error) {
	var typeCode JSONTypeCode
	var err error
	elemDocOff := len(buf)
	typeCode, buf, err = appendBinaryJSON(buf, val)
	if err != nil {
		return nil, errors.Trace(err)
	}
	if typeCode == JSONTypeCodeLiteral {
		litCode := buf[elemDocOff]
		buf = buf[:elemDocOff]
		buf[valEntryOff] = JSONTypeCodeLiteral
		buf[valEntryOff+1] = litCode
		return buf, nil
	}
	buf[valEntryOff] = typeCode
	valOff := elemDocOff - docOff
	jsonEndian.PutUint32(buf[valEntryOff+1:], uint32(valOff))
	return buf, nil
}

type field struct {
	key string
	val interface{}
}

func appendBinaryObject(buf []byte, x map[string]interface{}) ([]byte, error) {
	docOff := len(buf)
	buf = appendUint32(buf, uint32(len(x)))
	buf = appendZero(buf, dataSizeOff)
	keyEntryBegin := len(buf)
	buf = appendZero(buf, len(x)*keyEntrySize)
	valEntryBegin := len(buf)
	buf = appendZero(buf, len(x)*valEntrySize)

	fields := make([]field, 0, len(x))
	for key, val := range x {
		fields = append(fields, field{key: key, val: val})
	}
	slices.SortFunc(fields, func(i, j field) bool {
		return i.key < j.key
	})
	for i, field := range fields {
		keyEntryOff := keyEntryBegin + i*keyEntrySize
		keyOff := len(buf) - docOff
		keyLen := uint32(len(field.key))
		if keyLen > math.MaxUint16 {
			return nil, ErrJSONObjectKeyTooLong
		}
		jsonEndian.PutUint32(buf[keyEntryOff:], uint32(keyOff))
		jsonEndian.PutUint16(buf[keyEntryOff+keyLenOff:], uint16(keyLen))
		buf = append(buf, field.key...)
	}
	for i, field := range fields {
		var err error
		buf, err = appendBinaryValElem(buf, docOff, valEntryBegin+i*valEntrySize, field.val)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	docSize := len(buf) - docOff
	jsonEndian.PutUint32(buf[docOff+dataSizeOff:], uint32(docSize))
	return buf, nil
}


text:
<?php

namespace Phalcon\Validation;

/**
 * Phalcon\Validation\Validator
 *
 * This is a base class for validators
 */
abstract class Validator implements \Phalcon\Validation\ValidatorInterface
{

    protected $_options;

    /**
     * Phalcon\Validation\Validator constructor
     *
     * @param array $options
     */
    public function __construct(array $options = null) {}

    /**
     * Checks if an option has been defined
     *
     * @deprecated since 2.1.0
     * @see \Phalcon\Validation\Validator::hasOption()
     * @param string $key
     * @deprecated
     * @return bool
     */
    public function isSetOption($key) {}

    /**
     * Checks if an option is defined
     *
     * @param string $key
     * @return bool
     */
    public function hasOption($key) {}

    /**
     * Returns an option in the validator's options
     * Returns null if the option hasn't set
     *
     * @param string $key
     * @param mixed $defaultValue
     * @return mixed
     */
    public function getOption($key, $defaultValue = null) {}

    /**
     * Sets an option in the validator
     *
     * @param string $key
     * @param mixed $value
     */
    public function setOption($key, $value) {}

    /**
     * Executes the validation
     *
     * @param \Phalcon\Validation $validation
     * @param string $attribute
     * @return bool
     */
    abstract public function validate(\Phalcon\Validation $validation, $attribute);

}


text:
SYNONYM

#### According to
The Catalogue of Life, 3rd January 2011

#### Published in
Fl. peruv. 1:54. 1798

#### Original name
null

### Remarks
null


text:
<?php
/**
 * This file is part of the Ray.WebFormModule package.
 *
 * @license http://opensource.org/licenses/MIT MIT
 */
namespace Ray\WebFormModule;

class AbstractAuraFormTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var AbstractForm
     */
    private $form;

    public function setUp()
    {
        parent::setUp();
        $this->form = (new FormFactory)->newInstance(FakeForm::class);
    }

    public function testForm()
    {
        $formHtml = $this->form->form();
        $this->assertSame('<form method="post" enctype="multipart/form-data">', $formHtml);
    }

    public function testAntiCsrfForm()
    {
        $this->form->setAntiCsrf(new FakeAntiCsrf);
        $this->form->postConstruct();
        $formHtml = $this->form->form();
        $this->assertSame('<form method="post" enctype="multipart/form-data"><input type="hidden" name="__csrf_token" value="goodvalue" />' . PHP_EOL, $formHtml);
    }

    public function testInput()
    {
        $name = $this->form->input('name');
        $this->assertSame('<input id="name" type="text" name="name" />' . PHP_EOL, (string) $name);
    }

    public function testError()
    {
        $this->form->fill([]);
        $data = ['name' => '@invalid@'];
        $isValid = $this->form->apply($data);
        $this->assertFalse($isValid);
        $error = $this->form->error('name');
        $this->assertSame('Name must be alphabetic only.', $error);
        $html = (string) $this->form;

        return $html;
    }

    /**
     * @depends testError
     */
    public function tesetInputDataReamainedOnValidationFailure($html)
    {
        $expected = '<input id="name" type="text" name="name" value="@invalid@" />';
        $this->assertContains($expected, $html);
    }
}


text:
.thumb img {
	-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)"
}

.thumb img:hover {
	-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"
}

ul.social-icons li {
	width:24px;
	height:24px;
}


text:
@class Person;

@interface Firm : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *persons;
@end

@interface Firm (CoreDataGeneratedAccessors)

- (void)addPersonsObject:(Person *)value;
- (void)removePersonsObject:(Person *)value;
- (void)addPersons:(NSSet *)values;
- (void)removePersons:(NSSet *)values;

@end


text:
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        app('view')->composer('layouts.master', function ($view) {
            $action = app('request')->route()->getAction();

            $controller = class_basename($action['controller']);

            list($controller, $action) = explode('@', $controller);

            $view->with(compact('controller', 'action'));
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}


text:
layout: post
date: 2016-05-15
title: "Sarah Houston Drew 2013"
category: Sarah Houston
tags: [Sarah Houston,2013]
---
### Sarah Houston Drew
Just **$289.99**
### 2013

BRANDS	Sarah Houston

Years	2013

Buy it: [https://www.readybrides.com/en/sarah-houston/47316-sarah-houston-drew.html](https://www.readybrides.com/en/sarah-houston/47316-sarah-houston-drew.html)


text:
import numpy as np
from keras.models import Sequential
from keras import initializers
from keras.layers import *
from keras.optimizers import RMSprop, SGD, Adagrad, Nadam
from keras.utils import np_utils
from keras import backend as K
import time

def relu_init(shape, name=None):
    return initializers.glorot_normal(shape)

def getModel(input_dim = 256, hidden_units = 100, hidden_layers=1, nb_classes = 256,
        lr = 0.005, out_activation='softmax', optimizer = 'Adagrad', dropout=0.0, nCaption=1):
    '''
    Create the RNN model.
    * nb_classes {integer} - The number of classes at the output layer.
    * lr {float} - The learning rate. Default is 0.01
    '''
    model = Sequential()
    if(nCaption == 1):
        model.add(Masking(mask_value=0., input_shape=(20, input_dim)))

    #Create RNN
    model.add(LSTM(output_dim = hidden_units,
                        kernel_initializer='glorot_uniform',
                        activation = 'tanh',
                        input_dim=input_dim,
                        return_sequences = True, #return_sequences: Boolean. Whether to return the last output in the output sequence, or the full sequence. Not sure about this one
                        dropout_W=dropout,
                        dropout_U=dropout
                        ))

    if hidden_layers == 2:
        model.add(LSTM(output_dim = 128,
                            kernel_initializer='glorot_uniform',
                            activation = 'tanh',
                            input_dim= hidden_units,
                            return_sequences = True,
                            dropout_W=dropout,
                            dropout_U=dropout
                            ))
    # model.add(Dropout(dropout))
    model.add(TimeDistributed(Dense(nb_classes,activation=out_activation, kernel_initializer='glorot_uniform')))
    # If statements to select optimizer
    if optimizer == 'Adagrad':
        o = Adagrad(lr=0.01, epsilon=1e-08, decay=0.0)
    elif optimizer == 'RMSprop':
        o = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
    elif optimizer == 'SDG':
        o = SGD(lr=lr, momentum = 0.9, nesterov = True)
    elif optimizer == 'Nadam':
        o = Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004)

    model.compile(loss = 'categorical_crossentropy',
                  optimizer = o,
                  metrics = ['accuracy'])

    # Store the optimizer used
    model.optimizer.__setattr__('optimizer', optimizer)

    return model


text:
package zchang.me.uilibrary;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

/**
 * Created by Administrator on 2016/3/28.
 */
public class CircleBackgroundDrawable extends Drawable {
    private RectF circleBounds;
    private Rect bounds;

    Paint paint;

    public CircleBackgroundDrawable(int color) {
        super();
        paint = new Paint();
        paint.setColor(color);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        this.bounds = bounds;
        this.circleBounds = new RectF(bounds);
        this.circleBounds.set(this.circleBounds.left * 0.75f, this.circleBounds.top * 0.75f, this.circleBounds.right * 0.75f, this.circleBounds.bottom * 0.75f);
        super.onBoundsChange(bounds);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.save();

        canvas.translate((bounds.bottom - bounds.top) * 0.125f, (bounds.right - bounds.left) * 0.125f);
        canvas.drawOval(circleBounds, paint);

        canvas.restore();
    }

    @Override
    public void setAlpha(int alpha) {

    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return 0xcd;
    }
}


text:
@implementation AnimColumnSeries

@synthesize enableGradient;

- (void) drawColumnHandler : (UIView *)view
{
    isVerticalGradient = NO;
    enableGradient = YES;
    animations = [[NSMutableArray alloc] init];
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0];
    [view setUserInteractionEnabled:NO];
    __block NSMutableArray *layersToRemove = nil;
    [CATransaction setCompletionBlock:
     ^{
        [layersToRemove enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
        {
            //[obj removeFromSuperlayer];
        }];
        [layersToRemove removeAllObjects];
        [view setUserInteractionEnabled:YES];
    }];
    [self getLayerByIndex];
    diff = dataSize - [localLayers count];
    layersToRemove = [NSMutableArray arrayWithArray:localLayers];
    CGFloat areaRectWidth = view.frame.size.width - paddingLeft - paddingRight;
    NSMutableArray * axisLables = categoryAxis.axisLabels;
    int size = axisLables.count;
    CGFloat stopX, stopY;
    AxisLabel * axisLabel = categoryAxis.baseZero ? [axisLables objectAtIndex:1]: [axisLables objectAtIndex:0];
    CGFloat position = axisLabel.position;
    CGFloat perUnitWidth = position * areaRectWidth;
    CGFloat bottom = view.frame.size.height - paddingBottom;
    CGFloat areaRectHeight = bottom - paddingTop;
    CGFloat totalSpaceWidth = 2 * barSpaceWidth;
    CGFloat perColumnWidth = (perUnitWidth - totalSpaceWidth) / seriesSize;
    CGFloat left, top, right;
    for (int index = 0; index < size; index++)
    {
        float value = [[propertyValues objectAtIndex:index] floatValue];
        AxisLabel * axisLabel = [axisLables objectAtIndex:index];
        stopX = axisLabel.position * areaRectWidth + paddingLeft;
        stopY = bottom -   value * (areaRectHeight/linearAxis.maxAxisValue);
        left = barSpaceWidth * (2 * index + 1) + (seriesIndex + seriesSize * index) * perColumnWidth + paddingLeft;// +
        right = left + perColumnWidth;
        top = stopY;
        float  height = bottom - top;
        id<ICALayer>  layer = [self createLayer:index value:value layersToRemove:layersToRemove];
        if (enableGradient)
        {
            [layer setFrameHandler:CGRectMake(left, bottom, perColumnWidth, 0)];
        }
        [layer setPropertyHandler:left height:height rectWidth:perColumnWidth bottom:bottom];
        [layer setValueHandler:value];
        [layer createAnimationForKey:@"startValue"
                              fromValue:[NSNumber numberWithDouble:bottom]
                                toValue:[NSNumber numberWithDouble:top]
                               delegate:self];
        [self addClickPoint:index rect:CGRectMake(left, top, perColumnWidth, bottom - top)];
    }
   [CATransaction commit];
}

- (id<ICALayer>)createLayer : (int) index value : (CGFloat) value  layersToRemove : (NSMutableArray *)layersToRemove
{
    CALayer * parentLayer = [chartView layer];
    id<ICALayer> layer;
    if( index >= [localLayers count] )
    {
        layer = enableGradient? [[SCGradientLayer alloc] init : stroke.strokeColor] : [RectLayer createRectLayer : stroke.strokeColor];
        if (enableGradient && !isVerticalGradient)
        {
            CAGradientLayer * gradientLayer = (SCGradientLayer *)layer;
            gradientLayer.startPoint = CGPointMake(0, 0.5);
            gradientLayer.endPoint = CGPointMake(1.0, 0.5);
        }
        //[parentLayer insertSublayer:(CALayer *)layer atIndex:0];
        [parentLayer addSublayer:(CALayer *)layer];
        [layer setSeriesIndex:seriesIndex];
        diff--;
    }
    else
    {
        id<ICALayer> onelayer = [localLayers objectAtIndex:index];
        if (diff > 0)
        {
            layer = onelayer;
            [parentLayer insertSublayer:(CALayer *)onelayer atIndex:index];
            diff--;
        }
        else if(diff < 0)
        {
            while(diff < 0)
            {
                [(CALayer *)onelayer removeFromSuperlayer];
                [parentLayer addSublayer:(CALayer *)onelayer];
                diff++;
                onelayer = [localLayers objectAtIndex:index];
                if([onelayer getValueHandler] == value || diff == 0)
                {
                    layer = onelayer;
                    [layersToRemove removeObject:layer];
                    break;
                }
            }
        }
    }
    return layer;
}

- (void) updateShapeHandler : (NSTimer *)timer
{
    NSLog(@"updateColumnHandler------%@",@"updateColumnHandler");

    if (enableGradient)
    {
        [self updateGradientRectHandler];
        return;
    }
    [self updateNoGradientRectHandler];
}

- (void) updateNoGradientRectHandler
{
    [self getLayerByIndex];
    [localLayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
     {
         CGFloat startValue = [[[obj presentationLayer] valueForKey:@"startValue"] floatValue];
         RectLayer * rectLayer = (RectLayer *)obj;
         CGRect rect = CGRectMake(rectLayer.left, startValue, rectLayer.rectWidth ,rectLayer.bottom-startValue);
         [rectLayer drawPath:rect];
         [obj sendSublayerToBack];
     }];
}

- (void) updateGradientRectHandler
{
    [self getLayerByIndex];
    [localLayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
     {
         SCGradientLayer * gradientLayer = (SCGradientLayer *)obj;
         CGFloat startValue = [[[gradientLayer presentationLayer] valueForKey:@"startValue"] floatValue];
         CGRect rect = CGRectMake(gradientLayer.left, startValue, gradientLayer.rectWidth ,gradientLayer.bottom - startValue);
         gradientLayer.frame = rect;
         if (idx == 0)
         {
             NSLog(@"updateGradientRectHandler++++%f", startValue);
         }
         [obj sendSublayerToBack];
     }];
}

- (void) getLayerByIndex
{
    localLayers = [[NSMutableArray alloc] init];
    CALayer * parentLayer = chartView.layer;
    NSArray * childrens = [parentLayer sublayers];
    for (id<ICALayer> layer in childrens)
    {
        if (([layer isKindOfClass:[SCGradientLayer class]] || [layer isKindOfClass:[RectLayer class]]) && [layer getSeriesIndex] == seriesIndex)
        {
            [localLayers addObject:layer];
        }
    }
}

@end


text:
<?php
declare(strict_types=1);

namespace TileLand\Direction;

interface Direction
{
    public function getName(): string;

    public function getReverse(): Direction;
}


text:
namespace Discord
{
    /// <summary>
    ///     A Unicode emoji.
    /// </summary>
    public class Emoji : IEmote
    {
        // TODO: need to constrain this to Unicode-only emojis somehow

        /// <inheritdoc />
        public string Name { get; }
        /// <summary>
        ///     Gets the Unicode representation of this emote.
        /// </summary>
        /// <returns>
        ///     A string that resolves to <see cref="Emoji.Name"/>.
        /// </returns>
        public override string ToString() => Name;

        /// <summary>
        ///     Initializes a new <see cref="Emoji"/> class with the provided Unicode.
        /// </summary>
        /// <param name="unicode">The pure UTF-8 encoding of an emoji.</param>
        public Emoji(string unicode)
        {
            Name = unicode;
        }

        /// <summary>
        ///     Determines whether the specified emoji is equal to the current one.
        /// </summary>
        /// <param name="other">The object to compare with the current object.</param>
        public override bool Equals(object other)
        {
            if (other == null) return false;
            if (other == this) return true;

            var otherEmoji = other as Emoji;
            if (otherEmoji == null) return false;

            return string.Equals(Name, otherEmoji.Name);
        }

        /// <inheritdoc />
        public override int GetHashCode() => Name.GetHashCode();
    }
}


text:
package org.opengroup.osdu.core.common.model.search;

import com.google.common.base.Strings;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.web.context.annotation.RequestScope;

import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RequestScope
public class RecordChangedMessages {

    private String messageId;
    private String publishTime;
    private String data;
    private Map<String, String> attributes;

    public String getDataPartitionId() {
        String output = attributes.get(DpsHeaders.DATA_PARTITION_ID);
        if(Strings.isNullOrEmpty(output))
            output =  attributes.get(DpsHeaders.ACCOUNT_ID);
        return output;
    }

    public boolean missingAccountId() {
        return this.attributes == null || Strings.isNullOrEmpty(this.getDataPartitionId());
    }

    public String getCorrelationId() {
        return attributes.get(DpsHeaders.CORRELATION_ID.toLowerCase());
    }

    public boolean hasCorrelationId() {
        return this.attributes != null && !Strings.isNullOrEmpty(this.getCorrelationId());
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/title_rout"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/teal"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/title_gank_back_ibtn"
            android:layout_width="56dp"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:background="@drawable/item_teal_selector_no_bord"
            android:src="@drawable/ic_navigate_before_white_24dp"/>

        <Button
            android:id="@+id/title_gank_class_tv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"
            android:background="@drawable/item_teal_selector_no_bord"
            android:padding="8dp"
            android:text="干货分类"
            android:textColor="@color/white"
            android:textSize="16sp"/>
    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/find_gank_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/title_rout"
        android:background="@color/black_trans10"/>
</RelativeLayout>


text:
'use strict';

/**
 * Module dependencies.
 */
var passport = require('passport'),
	url = require('url'),
	//FacebookStrategy = require('passport-facebook').Strategy,
	SpotifyStrategy = require('passport-spotify').Strategy,
	config = require('../config'),
	users = require('../../app/controllers/users.server.controller');

module.exports = function() {

	// Use facebook strategy
	passport.use(new SpotifyStrategy({
			clientID: config.spotify.clientID,
			clientSecret: config.spotify.clientSecret,
			callbackURL: config.spotify.callbackURL,
			passReqToCallback: true
		},
		function(req, accessToken, refreshToken, profile, done) {
			// Set the provider data and include tokens

			var providerData = profile._json;
			providerData.accessToken = accessToken;
			providerData.refreshToken = refreshToken;

			// Create the user OAuth profile
			var providerUserProfile = {
				firstName: profile.displayName,
				//lastName: profile.name.familyName,
				displayName: profile.displayName,
				email: profile.emails[0].value,
				username: profile.username,
				provider: 'spotify',
				providerIdentifierField: 'id',
				providerData: providerData,
				code:req.query.code
			};

			// Save the user OAuth profile
			users.saveOAuthUserProfile(req, providerUserProfile, done);

		}
	));
};