Giter Club home page Giter Club logo

detect-file-encoding-and-language's Introduction

Detect-File-Encoding-And-Language

npm npm npm bundle size

NPM stats

Functionality

Determine the encoding and language of text files!

  • Detects 40 languages as well as the appropriate encoding
  • Available as CLI, in Node.js and in the browser
  • Supports .txt, .srt, .sub, .html, .csv, .tsv
  • Works best with large inputs
  • Completely free, no API key required

For reliable encoding and language detection, use files containing at least 500 words of coherent text. Smaller inputs can work as well but the results might be less accurate and in some cases incorrect.

Live Demo

Feel free to test the functionality of this NPM package here. Upload your own files and see if the encoding and language are detected correctly!

Installation

npm install detect-file-encoding-and-language

Usage

Via CDN

// index.html
<body>
  <input type="file" id="my-input-field" />
  <script src="https://unpkg.com/detect-file-encoding-and-language/umd/language-encoding.min.js"></script>
  <script src="app.js"></script>
</body>

// app.js
document.getElementById("my-input-field").addEventListener("change", (e) => {
  const file = e.target.files[0];
  languageEncoding(file).then((fileInfo) => console.log(fileInfo));
  // Possible result: { language: english, encoding: UTF-8, confidence: { encoding: 1, language: 1 } }
});

If you don't want to use a CDN feel free to download the source code!

In React

// App.js
import languageEncoding from "detect-file-encoding-and-language";
export default function App() {
  function inputHandler(e) {
    const file = e.target.files[0];
    languageEncoding(file).then((fileInfo) => console.log(fileInfo));
    // Possible result: { language: english, encoding: UTF-8, confidence: { encoding: 1, language: 1 } }
  }
  return <input type="file" onChange={inputHandler} />;
}

In Node

File

// server.js
const languageEncoding = require("detect-file-encoding-and-language");
const pathToFile = "/home/username/documents/my-text-file.txt";
languageEncoding(pathToFile).then((fileInfo) => console.log(fileInfo));
// Possible result: { language: japanese, encoding: Shift-JIS, confidence: { encoding: 0.94, language: 0.94 } }

Buffer

// server.js
const languageEncoding = require("detect-file-encoding-and-language");
const content = Buffer.from("file content");
languageEncoding(content).then((fileInfo) => console.log(fileInfo));
// Possible result: { language: japanese, encoding: Shift-JIS, confidence: { encoding: 0.94, language: 0.94 } }

Via CLI

# Installation
npm install -g detect-file-encoding-and-language

# Usage
dfeal "/home/username/Documents/subtitle file.srt"
# Possible result: { language: french, encoding: CP1252, confidence: { encoding: 0.99, language: 0.99 } }

Using a buffer (browser)

Check out this issue page! @davuses posted a very simple code snippet there that converts your buffer into a blob which you can then pass into the function instead of a file!

Supported Languages

  • Polish
  • Czech
  • Hungarian
  • Romanian
  • Slovak
  • Slovenian
  • Albanian
  • Russian
  • Ukrainian
  • Bulgarian
  • English
  • French
  • Portuguese
  • Spanish
  • German
  • Italian
  • Danish
  • Norwegian
  • Swedish
  • Dutch
  • Finnish
  • Serbo-Croatian
  • Estonian
  • Icelandic
  • Malay-Indonesian
  • Greek
  • Turkish
  • Hebrew
  • Arabic
  • Farsi-Persian
  • Lithuanian
  • Chinese-Simplified
  • Chinese-Traditional
  • Japanese
  • Korean
  • Thai
  • Bengali
  • Hindi
  • Urdu
  • Vietnamese

Used Encodings

  • UTF-8
  • UTF-16LE
  • UTF-16BE
  • UTF-32LE
  • UTF-32BE
  • UTF-7
  • UTF-1
  • UTF-EBCDIC
  • SCSU
  • BOCU-1
  • CP1250
  • CP1251
  • CP1252
  • CP1253
  • CP1254
  • CP1255
  • CP1256
  • CP1257
  • GB18030
  • BIG5
  • Shift-JIS
  • EUC-KR
  • TIS-620

Confidence Score

The confidence score ranges from 0 to 1. It's an object that contains two different confidence scores. The language confidence score and the encoding confidence score. Both confidence scores will be the same if the detected encoding is Unicode. Otherwise the confidence score for the language and the encoding is calculated seperately. It is based on the amount of matches that were found for a particular language and the frequency of those matches. If you want to learn more about how it all works, check out the Wiki entry!

License

This project is licensed under the MIT License

License

detect-file-encoding-and-language's People

Contributors

erkstruwe avatar gignupg avatar martindeveloper avatar ninad-bhangui avatar stellarpower avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

detect-file-encoding-and-language's Issues

Email files.

Will it work with email (EML) files? I am interested in detecting what encoding was used to write an email file.

Is it possible to get Mac Roman detection?

Would it be possible to get more encodings?

I see a lot of Mac Roman (macroman or macintsoh) encoded files that are detected as Windows 1252 (windows-1252 or cp1252) but that leaves these files with a lot of unknown character symbols and incorrect characters.

Incorrectly specify the encoding

Hi, everyone!
I have a problem with the library. I am trying to determine the encoding of text documents. And I get the wrong result. My documents .doc and .docx with UTF-8 encoding and language Russian return the result: encoding = BIG5, language = Chinese.
Has anyone come across this? How to fix it?

I will be glad to good advice

Incorrect character encoding

Character encoding detection is wholly based on the language detected (if not already detected as UTF-8), and in many cases this defaults to CP1252 character encoding.

The Google homepage served in Germany is unable to be detected:

dfeal google-de.html
{
    "encoding": null,
    "language": null,
    "confidence": {
        "encoding": null,
        "language": null
    }
}

The content is actually encoded in the ISO-8859-1 character set (the meta charset value is UTF-8 but that's a different issue):

file google-de.html
google-de.html: HTML document text, ISO-8859 text, with very long lines (2439)

Adding " das " (https://github.com/gignupg/Detect-File-Encoding-And-Language/blob/063340d/src/config/languageObject.js#L13) to the file allows the language to be detected as German but the character encoding comes from https://github.com/gignupg/Detect-File-Encoding-And-Language/blob/063340d/src/config/languageObject.js#L198 which is incorrect:

echo " das " | cat - google-de.html > google-de-with-das.html
dfeal google-de-with-das.html
{
    "encoding": "CP1252",
    "language": "german",
    "confidence": {
        "encoding": 0.03,
        "language": 0.03
    }
}

The Google homepage served in the US is encoded in ASCII but is detected as UTF-8:

file google-us.html
google-us.html: HTML document text, ASCII text, with very long lines (2442)
dfeal google-us.html
{
    "encoding": "UTF-8",
    "language": "english",
    "confidence": {
        "encoding": 1,
        "language": 0.03
    }
}

The character encoding should be detected independently of the language, maybe an npm package could be used? Based on https://github.com/file/file/blob/master/src/encoding.c, detecting the character encoding is not trivial.

google-us.html.zip
google-de.html.zip

Is it possible to use the result externally?

I'd like to do a series of many actions based on the encoding and I don't want to run all these actions inside then.

So is it possible to return a value to an external variable like the_encoding? If so, could you update the Readme accordingly?

I know it involves a promise, but still thought it's worth a shot to ask.

I've tried the following which returns undefined:

document.getElementById("my-input-field").addEventListener("change", (e) => {
  var the_encoding;
  const file = e.target.files[0];
  languageEncoding(file).then((fileInfo) => the_encoding = fileInfo.encoding); // also tried this.the_encoding...
  console.log(the_encoding); // undefined...
  // Then run various actions involving the_encoding
});

TypeScript

It would be great if this package had types for using with TypeScript.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.