Answers.Body: <p>On a very basic level, you are absolutely correct about the encoding of the attached sentence. But, practically, when you have a set n number of documents to be encoded, things happen differently.</p>
  <ol>
  <li><p>Let's say we have <code>n=1000</code> independent sentences.</p>
  </li>
  <li><p>Now, doing it by hand is a little difficult, but let's say we have to do it. What you will start with is making a list of all the unique words there are and assigning each of them a number, let's call this our <code>vocabulary</code>. It might look like this</p>
  </li>
  </ol>
  <pre><code>{
      'the': 1,
      'menace': 2, 
      'shirt': 3, 
       ...
  }
  </code></pre>
  <ol start="3">
  <li><p>Now each possible word has a separate id assigned, you will go back to each of the sentences and encode them based on the number you assigned to each word in the last step. Note that in this method you need to be completely neutral about misspelled words assigning them different numbers as you find them. Of course, you can choose to correct the spelling if you wish, but you already are analyzing 1000 lines, why add one more task? Each line will now look something like what you mentioned in your question.</p>
  </li>
  <li><p>You can use this encoded information in algorithms.</p>
  </li>
  </ol>
  <p>But, to have better results, as mentioned you will need to correct the misspelled words, right? What if instead of <code>n=1000</code> it was <code>n=1,000,000</code> or like some trained models out there, with <code>n=4 Billion documents</code>, you can't possibly think to correct the spellings by hand (manually) right?</p>
  <p>So, researchers found more standardized methods. Essentially, we define a set of tokens, that might be a word like <code>the</code>, <code>hell</code> etc., or tokens (a sequence of letters) <code>1</code>, <code>a</code>, <code>b</code>, <code>c</code>, <code>blad</code>, <code>ght</code>, etc. These tokens are designed based on multiple statistics the most important of which are length, and occurrence. Basically, if a word is too large it is taking space in the set, and if it is too rarely used, it can be broken into smaller tokens that might be a part of other words as well. The idea is to have tokens with similar frequency of occurrence when analyzed over a large corpus of data, but also include smaller tokens like <code>a</code>, <code>b</code>, etc. which can be used to account for new and previously unseen words. As you very correctly put it, computers understand numbers, and numbers can be anything as long as they make sense to computers.</p>
  <p>These sets contain a vocabulary of tokens that can be used to encode any word. Here are some examples to help consolidate my points-</p>
  <ol>
  <li><p>Revolution (correct spelling)
  tokenized - <code>['rev', 'ol', 'u', 'tion']</code></p>
  </li>
  <li><p>Revoluton (incorrect spelling)
  tokenized - <code>['rev', 'ol', 'u', 't', 'on']</code></p>
  </li>
  <li><p>Hi
  tokenized - <code>['Hi']</code></p>
  </li>
  </ol>
  <p>and so on.</p>
  <p>NOTE - The aforementioned tokens are only for illustration and might (probably will) differ from tokenizer to tokenizer.</p>
  <p>So, now instead of defining our own tokenizers which need to be designed with multiple aspects and considerations, you can choose to use a standardized one already designed and available online. Some examples would be, NLTK's tokenizer, Spacy's tokenizer, Gensim, WordPeice (used for BERT) for your case.</p>
  <p>Additionally, not having tokenizers has its own shortcomings, some of which are-</p>
  <ol>
  <li><p>If you add independent words to the vocabulary, then the vocabulary quickly gets out of hand (consider that the English language has around 1 million words, not to mention other languages if you plan on training a translator). Great tokenizers have achieved good results with a token vocabulary of as small as 30,000 tokens.</p>
  </li>
  <li><p>As noted, the same words but wrong spellings hold space of their own and make the vocabulary unnecessarily longer. With standard tokens, you don't have to care about these misspelled words as they can be accounted for easily.</p>
  </li>
  <li><p>Vocabulary based on words will fail in case of new words, and so every time new information comes up, you will have to add a new element to the vocabulary. As was with misspelled words, new words can be accounted for with standard token-based vocabularies as well.</p>
  </li>
  </ol>
  <p>Hope this helps.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-01T04:57:14.607Z
Answers.LastActivityDate: 2023-07-04T04:27:06.137Z
Answers.LastEditDate: 2023-07-04T04:27:06.137Z
Answers.UserDisplayName: Chinmay
Body: <p>I'm trying to understand NLP, how sentences can be used as input output in neural network architecture. As we know ANN is only compatible with number data. That's mean the sentences must be convert to number, right?</p>
  <p>Suppose I have this sentence</p>
  <pre><code>Hello world, the world is saying hello.
  </code></pre>
  <p>How it looks like when it converted to number?
  I think it will looks like this...</p>
  <pre><code>1 2 3 2 4 5 1
  </code></pre>
  <p>Where:</p>
  <pre><code>hello = 1
  world = 2
  ...
  </code></pre>
  <p>But what if there's typo? For example <code>hello worlf</code></p>
  
Tags: <natural-language-processing><data-preprocessing>
Title: How are sentences numerically encoded before passing them to neural networks?
UserDisplayName: Muhammad Ikhwan Perwira
ThreadText: # How are sentences numerically encoded before passing them to neural networks?
  
  (Asked by: username_0 on 2023-07-01)
  
  I'm trying to understand NLP, how sentences can be used as input output in neural network architecture. As we know ANN is only compatible with number data. That's mean the sentences must be convert to number, right?
  
  Suppose I have this sentence
  
  ```
  Hello world, the world is saying hello.
  
  ```
  
  How it looks like when it converted to number?
  I think it will looks like this...
  
  ```
  1 2 3 2 4 5 1
  
  ```
  
  Where:
  
  ```
  hello = 1
  world = 2
  ...
  
  ```
  
  But what if there's typo? For example `hello worlf`
  
  **Tags:** natural-language-processing, data-preprocessing
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-07-01. Score: 8 | Accepted answer
  
  On a very basic level, you are absolutely correct about the encoding of the attached sentence. But, practically, when you have a set n number of documents to be encoded, things happen differently.
  
  1. Let's say we have `n=1000` independent sentences.
  2. Now, doing it by hand is a little difficult, but let's say we have to do it. What you will start with is making a list of all the unique words there are and assigning each of them a number, let's call this our `vocabulary`. It might look like this
  
  ```
  {
      'the': 1,
      'menace': 2, 
      'shirt': 3, 
       ...
  }
  
  ```
  
  3. Now each possible word has a separate id assigned, you will go back to each of the sentences and encode them based on the number you assigned to each word in the last step. Note that in this method you need to be completely neutral about misspelled words assigning them different numbers as you find them. Of course, you can choose to correct the spelling if you wish, but you already are analyzing 1000 lines, why add one more task? Each line will now look something like what you mentioned in your question.
  4. You can use this encoded information in algorithms.
  
  But, to have better results, as mentioned you will need to correct the misspelled words, right? What if instead of `n=1000` it was `n=1,000,000` or like some trained models out there, with `n=4 Billion documents`, you can't possibly think to correct the spellings by hand (manually) right?
  
  So, researchers found more standardized methods. Essentially, we define a set of tokens, that might be a word like `the`, `hell` etc., or tokens (a sequence of letters) `1`, `a`, `b`, `c`, `blad`, `ght`, etc. These tokens are designed based on multiple statistics the most important of which are length, and occurrence. Basically, if a word is too large it is taking space in the set, and if it is too rarely used, it can be broken into smaller tokens that might be a part of other words as well. The idea is to have tokens with similar frequency of occurrence when analyzed over a large corpus of data, but also include smaller tokens like `a`, `b`, etc. which can be used to account for new and previously unseen words. As you very correctly put it, computers understand numbers, and numbers can be anything as long as they make sense to computers.
  
  These sets contain a vocabulary of tokens that can be used to encode any word. Here are some examples to help consolidate my points-
  
  1. Revolution (correct spelling)
     tokenized - `['rev', 'ol', 'u', 'tion']`
  2. Revoluton (incorrect spelling)
     tokenized - `['rev', 'ol', 'u', 't', 'on']`
  3. Hi
     tokenized - `['Hi']`
  
  and so on.
  
  NOTE - The aforementioned tokens are only for illustration and might (probably will) differ from tokenizer to tokenizer.
  
  So, now instead of defining our own tokenizers which need to be designed with multiple aspects and considerations, you can choose to use a standardized one already designed and available online. Some examples would be, NLTK's tokenizer, Spacy's tokenizer, Gensim, WordPeice (used for BERT) for your case.
  
  Additionally, not having tokenizers has its own shortcomings, some of which are-
  
  1. If you add independent words to the vocabulary, then the vocabulary quickly gets out of hand (consider that the English language has around 1 million words, not to mention other languages if you plan on training a translator). Great tokenizers have achieved good results with a token vocabulary of as small as 30,000 tokens.
  2. As noted, the same words but wrong spellings hold space of their own and make the vocabulary unnecessarily longer. With standard tokens, you don't have to care about these misspelled words as they can be accounted for easily.
  3. Vocabulary based on words will fail in case of new words, and so every time new information comes up, you will have to add a new element to the vocabulary. As was with misspelled words, new words can be accounted for with standard token-based vocabularies as well.
  
  Hope this helps.
  
  
  ---
  
  