Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- Copyright 2023 The MediaPipe Authors.

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. -->

<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>

<h1>Identifying language with the MediaPipe Language Detection Task</h1>
<p>This demo detects the language of the input text. The result shows the most likely language using the <a href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO_639-1</a> language code.</p>

<h2>How to use</h2>
<p>Add text to the input field, and then press <b>Detect Language</b>.</p>
<p>You can <button class="mdc-button" id="populate-text">
    <span class="mdc-button__ripple"></span>
    <span class="mdc-button__label">POPULATE TEXT</span>
  </button> with a default input, or add your own text.</p>
<p><b>Input:
  </b>
</p>
<label class="mdc-text-field mdc-text-field--filled mdc-text-field--textarea mdc-text-field--no-label">
  <span class="mdc-text-field__ripple"></span>
  <span class="mdc-text-field__resizer">

    <textarea id="input" class="mdc-text-field__input" rows="8" cols="40" aria-label="Text Input"></textarea>
  </span>
  <span class="mdc-line-ripple"></span>
</label></br>
<button id="submit" class="mdc-button mdc-button--raised">
  <span class="mdc-button__label">DETECT LANGUAGE</span>
</button></br>

<p><b>Result:</b></p>
<p id="output"></p>
              
            
!

CSS

              
                /* Copyright 2023 The MediaPipe Authors.

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. */

@use "@material";
body {
  font-family: roboto;
  margin: 2em;
  color: #3d3d3d;
  --mdc-theme-primary: #007f8b;
  --mdc-theme-on-primary: #f1f3f4;
}

h1 {
  color: #007f8b;
}

h2 {
  clear: both;
}

button {
  min-width: 200px;
  height: 50px;
}

section {
  opacity: 1;
  transition: opacity 500ms ease-in-out;
}

#input {
  height: 100px;
  --mdc-theme-primary: #12b5cb;
  --mdc-theme-on-primary: #f1f3f4;
}

#output {
  min-height: 100px;
  min-width: 500px;
  font-weight: bold;
}

.invisible {
  opacity: 0.2;
}

#submit {
  margin-top: 20px;
}

              
            
!

JS

              
                /* Copyright 2023 The MediaPipe Authors.

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. */

import { MDCTextField } from "https://cdn.skypack.dev/@material/textfield";
import { MDCRipple } from "https://cdn.skypack.dev/@material/ripple";
import {
  LanguageDetector,
  FilesetResolver
} from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-text@0.10.0";

// Create the LanguageDetector object upon page load
let languageDetector: LanguageDetector;
const createLanguageDetector = async () => {
  const text = await FilesetResolver.forTextTasks(
    "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-text@0.10.0/wasm"
  );
  languageDetector = await LanguageDetector.createFromOptions(text, {
    baseOptions: {
      modelAssetPath: `https://storage.googleapis.com/mediapipe-models/language_detector/language_detector/float32/1/language_detector.tflite`
    },
    maxResults: 5
  });
};
createLanguageDetector();

const textField = new MDCTextField(document.querySelector(".mdc-text-field"));

const defaultText: string =
  "日本語は、日本国内や、かつての日本領だった国、そして国外移民や移住者を含む日本人同士の間で使用されている言語。日本は法令によって公用語を規定していないが、法令その他の公用文は全て日本語で記述され、各種法令において日本語を用いることが規定され、学校教育においては「国語」の教科として学習を行うなど、事実上日本国内において唯一の公用語となっている。";

// Get the required elements
const input = document.getElementById("input") as HTMLInputElement;
const output = document.getElementById("output") as HTMLElement;
const submit = document.getElementById("submit") as HTMLButtonElement;
const defaultTextButton = document.getElementById(
  "populate-text"
) as HTMLButtonElement;

// Add a button click listener to add the default text
defaultTextButton.addEventListener("click", () => {
  input.value = defaultText;
});

// Add a button click listener that classifies text on click
submit.addEventListener("click", async () => {
  if (input.value === "") {
    alert("Please write some text, or click 'Populate text' to add text");
    return;
  }

  output.innerText = "Detecting language...";

  await sleep(5);
  const result: LanguageDetectorResult = await languageDetector.detect(
    input.value
  );
  displayDetectionResult(result);
});

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

// Iterate through the detected languages in the LanguageDetectorResult object, then display them in #output
function displayDetectionResult(result: LanguageDetectorResult) {
  if (result.languages.length > 0) {
    output.innerText = "";
  } else {
    output.innerText = "Result is empty";
  }

  for (const language of result.languages) {
    const categoryDiv = document.createElement("div");
    categoryDiv.innerText = `${
      language.languageCode
    }: ${language.probability.toFixed(2)}`;
    output.appendChild(categoryDiv);
  }
}

              
            
!
999px

Console