What's new

Closed Closed

Status
Not open for further replies.

Ruyi

𝙳𝚞𝚔𝚎 𝚘𝚏 𝙿𝙷𝙲
Elite
Joined
Dec 7, 2015
Posts
2,132
Solutions
14
Reaction
10,197
Points
1,916
Closed
 
Last edited:
Ang mga code na iyong ibinahagi ay pareho lamang. Hindi mo na kailangan pang i-combine ang mga ito dahil pareho lamang sila. Ang mga code na ito ay naglalaman ng dalawang bahagi:

1. Bahagi ng pagbilang ng mga salita at pangungusap sa isang text area. Naglalaman ito ng mga sumusunod na script:
  • Pagkuha ng mga element ng text area at count
  • Pagdagdag ng event listener sa text area
  • Pagkuha ng teksto mula sa text area
  • Pagbibilang ng mga salita at pangungusap sa pamamagitan ng pagsplit ng teksto
  • Pag-update ng mga count element

2. Bahagi ng pag-OCR ng isang imahe at paglagay ng nakikitang teksto sa text area. Naglalaman ito ng mga sumusunod na script:
  • Pagkuha ng mga element ng text area at count
  • Pagdagdag ng event listener sa text area
  • Pagkuha ng teksto mula sa text area
  • Pag-OCR ng imahe gamit ang Tesseract.js
  • Pagkuha ng recognized text mula sa result
  • Pag-set ng recognized text bilang input sa text area
  • Pag-trigger ng input event para ma-update ang mga count
  • Pagtapos ng worker

Kung nais mong i-combine ang mga code na ito, maaring gawin mo ito ngunit dapat mo isaalang-alang na maaaring magkaroon ng conflict sa mga variable at event listener. Upang maiwasan ito, maaari mong baguhin ang mga pangalan ng mga variable at id ng mga element. Halimbawa, maaari mong baguhin ang textInput sa una at ikalawang code ng script 1 upang maging textInput1 at textInput2. Gayundin, maaari mong baguhin ang wordCountElement sa una at ikalawang code ng script 1 upang maging wordCountElement1 at wordCountElement2. Ganito rin ang gagawin mo sa iba pang mga variable at id ng mga element.

Matapos gawin ang mga nabanggit na pagbabago, maaari mo nang i-combine ang mga code sa pamamagitan ng paglalagay ng script 1 bago ang script 2. Ito ang magiging resulta:

JavaScript:
// Get the text area and count elements
var textInput1 = document.getElementById('text-input1');
var wordCountElement1 = document.getElementById('word-count1');
var sentenceCountElement1 = document.getElementById('sentence-count1');

// Add an event listener to the text area
textInput1.addEventListener('input', function() {
  // Get the text from the text area
  var text = textInput1.value;

  // Count the words by splitting the text on any whitespace character and filtering out empty strings
  var words = text.split(/\s+/).filter(function(word) {
    return word !== '';
  });
  var wordCount = words.length;

  // Count the sentences by splitting the text on periods, exclamation marks, and question marks
  var sentences = text.split(/[.!?]/).filter(function(sentence) {
    return sentence !== '';
  });
  var sentenceCount = sentences.length;

  // Update the word and sentence count elements
  wordCountElement1.textContent = 'Word Count: ' + wordCount;
  sentenceCountElement1.textContent = 'Sentence Count: ' + sentenceCount;
});

// Add below your existing event listener
document.getElementById('start-ocr').addEventListener('click', function() {
  var fileSelector = document.getElementById('file-selector');
  if (fileSelector.files.length > 0) {
    var file = fileSelector.files[0];
    Tesseract.createWorker().then(worker => {
      worker.recognize(file).then(function(result) {
        // Get the recognized text from the result
        var text = result.data.text;
        // Set the recognized text as input
        textInput1.value = text;
        // Trigger the input event to update counts
        textInput1.dispatchEvent(new Event('input'));
        // Terminate the worker
        worker.terminate();
      }).catch(function(error) {
        // Handle any errors that might occur
        console.error(error);
        alert('An error occurred while performing OCR.');
      });
    })
  }
});


// Get the text area and count elements
var textInput2 = document.getElementById('text-input2');
var wordCountElement2 = document.getElementById('word-count2');
var sentenceCountElement2 = document.getElementById('sentence-count2');

// Add an event listener to the text area
textInput2.addEventListener('input', function() {
  // Get the text from the text area
  var text = textInput2.value;

  // Count the words by splitting the text on any whitespace character and filtering out empty strings
  var words = text.split(/\s+/).filter(function(word) {
    return word !== '';
  });
  var wordCount = words.length;

  // Count the sentences by splitting the text on periods, exclamation marks, and question marks
  var sentences = text.split(/[.!?]/).filter(function(sentence) {
    return sentence !== '';
  });
  var sentenceCount = sentences.length;

  // Update the word and sentence count elements
  wordCountElement2.textContent = 'Word Count: ' + wordCount;
  sentenceCountElement2.textContent = 'Sentence Count: ' + sentenceCount;
});

// Add below your existing event listener
document.getElementById('start-ocr').addEventListener('click', function() {
  var fileSelector = document.getElementById('file-selector');
  if (fileSelector.files.length > 0) {
    var file = fileSelector.files[0];
    Tesseract.createWorker().then(worker => {
      worker.recognize(file).then(function(result) {
        // Get the recognized text from the result
        var text = result.data.text;
        // Set the recognized text as input
        textInput2.value = text;
        // Trigger the input event to update counts
        textInput2.dispatchEvent(new Event('input'));
        // Terminate the worker
        worker.terminate();
      }).catch(function(error) {
        // Handle any errors that might occur
        console.error(error);
        alert('An error occurred while performing OCR.');
      });
    })
  }
});
 
Status
Not open for further replies.

Similar threads

Back
Top