Skip to content
/ NLP Public
forked from Imranyy/javascript

Classifies text as insults or non-insults

Notifications You must be signed in to change notification settings

imrany/NLP

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Insult checker

Checks input as insult or not using brain.js

Using Natural

Certainly! Here's an example that uses the natural library in Node.js, which provides a range of natural language processing tools including a Naive Bayes classifier for text classification:

// Import natural library
const natural = require('natural');

// Create and configure the NaiveBayes classifier
const classifier = new natural.BayesClassifier();

// Define the training data
const trainingData = [
  { text: "You are stupid", label: "insult" },
  { text: "I like your shirt", label: "non-insult" },
  // Add more labeled examples to your training data
];

// Train the NaiveBayes classifier
trainingData.forEach(data => classifier.addDocument(data.text, data.label));
classifier.train();

// Test the NaiveBayes classifier with some input data
const input = "You are so annoying";
const output = classifier.classify(input);

// Output the prediction
if (output === "insult") {
  console.log("Input is an insult");
} else {
  console.log("Input is not an insult");
}

In this example, we create an instance of the natural.BayesClassifier class, which represents a Naive Bayes classifier. The training data is defined with labeled examples of input text samples, where the text property contains the input text and the label property contains the class label (insult or non-insult). The classifier.addDocument() method is used to add each training example to the classifier, and the classifier.train() method is called to train the classifier.

You can then use the classifier.classify() method to make predictions on new input text samples. The predicted class label (insult or non-insult) is returned, and you can use it to output the prediction accordingly.

Note that the Naive Bayes classifier is a simple probabilistic model that assumes independence between features, and may not always capture complex patterns in the input text data. It's important to experiment with different classifiers and fine-tune the model to achieve the best possible results for your specific use case. Additionally, as with any machine learning model, proper text preprocessing techniques may be necessary to achieve better classification accuracy, depending on the specific characteristics of your dataset.

Using Brain.js

// Import brain.js library
const brain = require('brain.js');

// Create the LSTM neural network
const lstm = new brain.recurrent.LSTM();

// Define the training data
const trainingData = [
  { input: "You are stupid", output: "insult" },
  { input: "I like your shirt", output: "non-insult" },
  // Add more labeled examples to your training data
];

// Train the LSTM neural network
lstm.train(trainingData, { iterations: 1000 });

// Test the LSTM neural network with some input data
const input = "You are so annoying";
const output = lstm.run(input);

// Output the prediction
if (output === "insult") {
  console.log("Input is an insult");
} else {
  console.log("Input is not an insult");
}

In this example, we create an instance of the brain.recurrent.LSTM class, which represents a Long Short-Term Memory (LSTM) recurrent neural network. The training data is defined with labeled examples of input text samples, where the input property contains the input text and the output property contains the class label (insult or non-insult). The lstm.train() method is called to train the LSTM neural network with the training data, and we specify the number of iterations for training (in this case, 1000 iterations).

You can then use the lstm.run() method to make predictions on new input text samples. The predicted class label (insult or non-insult) is returned, and you can use it to output the prediction accordingly.

Note that this is a basic example and may not be suitable for all text classification tasks. Fine-tuning of hyperparameters, adjusting the architecture of the LSTM network, and proper text preprocessing techniques may be necessary to achieve better classification accuracy, depending on the specific characteristics of your dataset. It's important to experiment and optimize the model based on your specific requirements to achieve the best possible results.

Using Tensorflowjs

// Import TensorFlow.js library
import * as tf from '@tensorflow/tfjs';

// Define the training data
const trainingData = [
  { input: "You are stupid", output: [1] },
  { input: "I like your shirt", output: [0] },
  // Add more labeled examples to your training data
];

// Convert training data to TensorFlow tensors
const xs = trainingData.map(data => tf.tensor2d(data.input, [1, data.input.length]));
const ys = trainingData.map(data => tf.tensor2d(data.output, [1, data.output.length]));

// Create the text classification model
const model = tf.sequential();
model.add(tf.layers.dense({ inputShape: [xs[0].shape[1]], units: 8, activation: 'relu' }));
model.add(tf.layers.dense({ units: 1, activation: 'sigmoid' }));

// Compile the model
model.compile({ optimizer: 'adam', loss: 'binaryCrossentropy', metrics: ['accuracy'] });

// Train the model
model.fit(xs, ys, { epochs: 100 })
.then(() => {
    console.log('Training completed!');
})
.catch((error:any) => {
    console.error('Training error:', error);
});

// Test the model with some input data
const input = "You are so annoying";
const inputTensor = tf.tensor2d([input], [1, input.length]);
const prediction = model.predict(inputTensor);
const insultProbability = prediction.dataSync()[0];
console.log("Insult probability:", insultProbability);

Note: This is a simplified example and may not be suitable for production-grade text classification tasks. It's important to understand the principles of machine learning, preprocess your data appropriately, and experiment with different model architectures and hyperparameters to optimize the model for your specific use case. Additionally, you may need to consider model evaluation, performance optimization, and other considerations for real-world applications.

Using Tensorflowjs toxicity model

// Import TensorFlow.js library
const tf = require('@tensorflow/tfjs');

// Load the toxicity classifier model
const toxicity = require('@tensorflow-models/toxicity');

// Load the toxicity classifier model
toxicity.load()
  .then(model => {
    // Test the model with some input data
    const input = "You are so stupid";
    model.classify([input])
      .then(predictions => {
        // Output the prediction
        console.log("Toxicity predictions:", predictions);
      })
      .catch(error => {
        console.error("Prediction error:", error);
      });
  })
  .catch(error => {
    console.error("Model loading error:", error);
  });

Note: The toxicity model used in this example is a pre-trained model provided by TensorFlow.js that can detect toxic content in text, including insults. However, it's important to understand that the accuracy of the model may vary depending on your specific use case and data. It's recommended to evaluate the model's performance on your own data and consider fine-tuning or training your own model for better accuracy, if needed. Additionally, you may need to consider other factors such as data preprocessing, model optimization, and performance evaluation to ensure accurate and reliable text classification results in a real-world application.

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 86.4%
  • HTML 13.6%