Answers.Body: <p>Text encoding depends very much on the purpose of your application. Here are some examples:</p>
  <p><strong>Text-to-speech:</strong> You would start with the word form itself, and probably look it up in a table that gives you a mapping to the phonological structure. Or you work through it (eg with a finite state transducer) and look at combinations of letters and check for the most common realistion of these letters (depending on the language and the writing system you are using). In the latter approach you can also deal with unknown words.</p>
  <p><strong>Part-of-speech tagging:</strong> You would look up the string in a dictionary, returning you a list of all the possible tags for the word (possibly augmented by frequency information). From then on you would operate on a sequence of tags, until you have them disambiguated, and you end up with a list that maps each word in the sentence (token) with a particular tag. For unknown words you can either assign them all possible tags (and let the disambiguation sort them out), or choose only the open class ones (you won't find many unknown prepositions or determiners).</p>
  <p><strong>Information retrieval/storage:</strong> This is one situation where you would want to replace word by numbers. You'd have a look-up table that maps word to integer values, and these can be very efficiently stored. For example, in the <em>Bank of English</em> one of the biggest early language corpora (several 100s of millions of tokens in the early 1990s), each token was encoded as a 4 byte integer. It was then very fast to go from the index positions of a word (from an inveretd index) to the actual position in the file, as each token is 4 bytes long, so the token at index <span class="math-container">$n$</span> is at file position <span class="math-container">$4n$</span>. It also somewhat compresses the text if you assume the average word length is greater than 4 (and that you don't encode the spaces between the tokens). Unknown words: they are by definition not in your text, so you can just return &quot;not found&quot;.</p>
  <p><strong>Machine learning:</strong> ML approaches don't really work on strings, so you need to encode word numerically. Ideally this should not be a random mapping (such as alphabetical order), so one approach that is used is to represent each word by a vector that encodes which other words occur in its neighbourhood. It is a well-known principle in corpus linguistics (since the 1930s) that words are not randomly distributed in a text, but that some words co-occur with regularity. For example, <em>rasher</em> and <em>bacon</em> usually go together. So the vector which describes <em>rasher</em> will have a positive number in the slot which  relates to <em>does it occur with 'bacon'?</em>, whereas the vector describing <em>bookshelf</em> will not. With a large enough vector, you can approximate similarities in meaning between words (based on their lexcial environments). Unknown words: you could use a random or average vector -- I don't know what the standard way is to deal with that.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-22T10:44:57.337Z
Answers.LastActivityDate: 2021-11-22T10:44:57.337Z
Answers.UserDisplayName: Oliver Mason
Body: <p>What are the approaches of encoding text data? I would be glad to hear some summarization from experienced persons.</p>
  <p>And are there any solutions accepting words outside the vocabulary and including them to the results (online machine learning)?</p>
  <p><strong>Data input</strong></p>
  <p>So my basic understanding is that if we want predict some value (linear regression) or say what is the probability of occuring some event (logistic regression) we have to gather some features as our input and encode them as number. But this is not necessarily true when working with continuous data like sentences.</p>
  <p>The most naive aproach, which comes to my mind is just to assign some natural numbers to each word in the vocabulary. But this number does not contain any meaningful data about the word itself. On the other hand what seems to be important in NLP is just the order of the words. This is where I think about n-grams so we feed network with more than just one word. Or attention like in the Transformer.</p>
  <p>Another idea, which cames to my mind is to vectorize the word using one of the Word Embedding technique. Here we have some context about the word so the input is not just a dumb number. But does it have any value when we want to predict the next word? Can Word Embedding be used in that way or it's purpose is completely different.</p>
  <p>Last thing I was reading of was to encode characters rather than words but it feels pointless in such basic example as next word prediction. I would think about it more for sub-word tasks like inflection generating.</p>
  <p><strong>Labelling</strong></p>
  <p>Again based on my knowledge when we want to solve yes/no problem we're using sigmoid function. If we have more classes we can use one-hot encoding. But sometimes the output of the network might give us ambiguous meaning so we're using the softmax function so all output sum to 1.</p>
  <p>How this looks in NLP area? When having a vocabulary consisting of 600k words do we really need 600k softmaxed outputs? I'm also thinking there about Word Embedding solutions where we can reduce the number of outputs to let's say 300 numbers and then find the closest word matching the output without using softmax.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-11-22T09:38:31.263Z
Comments.Text: What do you want to do? NLP is a vast area, and there are lots of options to deal with it, but this depends on what you want to achieve. Without knowing that it's impossible to answer.
Comments.UserDisplayName: Oliver Mason
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-11-22T10:13:19.257Z
Comments.Text: I wasn't thinking about any specific use case. I thought it will be possible to get some general understanding of that topic. But if I had to make something then I have two examples:
  1. Conversion of text to phonetic representation in a language having some general rules but also many exceptions
  2. Part-of-speech tagging
Comments.UserDisplayName: Harry
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-11-22T11:46:03.150Z
Comments.Text: Hello. Welcome to Artificial Intelligence Stack Exchange. After having quickly read several parts of this post, it seems to me that you're asking many distinct (although related) questions. Please, [**ask only one question per post**](https://meta.stackexchange.com/a/39224). If you have more than one question/issue/problem, ask each question in a separate post. If you think this question was wrongly closed and you're actually asking the same question but in different ways, let me know.
Comments.UserDisplayName: nbro
Tags: <neural-networks><natural-language-processing><data-preprocessing><word-embedding><data-labelling>
Title: General approaches in text encoding and labelling for NLP
UserDisplayName: Harry
ThreadText: # General approaches in text encoding and labelling for NLP
  
  (Asked by: username_0 on 2021-11-21)
  
  What are the approaches of encoding text data? I would be glad to hear some summarization from experienced persons.
  
  And are there any solutions accepting words outside the vocabulary and including them to the results (online machine learning)?
  
  **Data input**
  
  So my basic understanding is that if we want predict some value (linear regression) or say what is the probability of occuring some event (logistic regression) we have to gather some features as our input and encode them as number. But this is not necessarily true when working with continuous data like sentences.
  
  The most naive aproach, which comes to my mind is just to assign some natural numbers to each word in the vocabulary. But this number does not contain any meaningful data about the word itself. On the other hand what seems to be important in NLP is just the order of the words. This is where I think about n-grams so we feed network with more than just one word. Or attention like in the Transformer.
  
  Another idea, which cames to my mind is to vectorize the word using one of the Word Embedding technique. Here we have some context about the word so the input is not just a dumb number. But does it have any value when we want to predict the next word? Can Word Embedding be used in that way or it's purpose is completely different.
  
  Last thing I was reading of was to encode characters rather than words but it feels pointless in such basic example as next word prediction. I would think about it more for sub-word tasks like inflection generating.
  
  **Labelling**
  
  Again based on my knowledge when we want to solve yes/no problem we're using sigmoid function. If we have more classes we can use one-hot encoding. But sometimes the output of the network might give us ambiguous meaning so we're using the softmax function so all output sum to 1.
  
  How this looks in NLP area? When having a vocabulary consisting of 600k words do we really need 600k softmaxed outputs? I'm also thinking there about Word Embedding solutions where we can reduce the number of outputs to let's say 300 numbers and then find the closest word matching the output without using softmax.
  
  **Tags:** neural-networks, natural-language-processing, data-preprocessing, word-embedding, data-labelling
  > **Comments:**
  > **username_1**: What do you want to do? NLP is a vast area, and there are lots of options to deal with it, but this depends on what you want to achieve. Without knowing that it's impossible to answer.  
  > **username_0**: I wasn't thinking about any specific use case. I thought it will be possible to get some general understanding of that topic. But if I had to make something then I have two examples: 1. Conversion of text to phonetic representation in a language having some general rules but also many exceptions 2. Part-of-speech tagging  
  > **username_2**: Hello. Welcome to Artificial Intelligence Stack Exchange. After having quickly read several parts of this post, it seems to me that you're asking many distinct (although related) questions. Please, [**ask only one question per post**](https://meta.stackexchange.com/a/39224). If you have more than one question/issue/problem, ask each question in a separate post. If you think this question was wrongly closed and you're actually asking the same question but in different ways, let me know.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-11-22. Score: 0
  
  Text encoding depends very much on the purpose of your application. Here are some examples:
  
  **Text-to-speech:** You would start with the word form itself, and probably look it up in a table that gives you a mapping to the phonological structure. Or you work through it (eg with a finite state transducer) and look at combinations of letters and check for the most common realistion of these letters (depending on the language and the writing system you are using). In the latter approach you can also deal with unknown words.
  
  **Part-of-speech tagging:** You would look up the string in a dictionary, returning you a list of all the possible tags for the word (possibly augmented by frequency information). From then on you would operate on a sequence of tags, until you have them disambiguated, and you end up with a list that maps each word in the sentence (token) with a particular tag. For unknown words you can either assign them all possible tags (and let the disambiguation sort them out), or choose only the open class ones (you won't find many unknown prepositions or determiners).
  
  **Information retrieval/storage:** This is one situation where you would want to replace word by numbers. You'd have a look-up table that maps word to integer values, and these can be very efficiently stored. For example, in the *Bank of English* one of the biggest early language corpora (several 100s of millions of tokens in the early 1990s), each token was encoded as a 4 byte integer. It was then very fast to go from the index positions of a word (from an inveretd index) to the actual position in the file, as each token is 4 bytes long, so the token at index $n$ is at file position $4n$. It also somewhat compresses the text if you assume the average word length is greater than 4 (and that you don't encode the spaces between the tokens). Unknown words: they are by definition not in your text, so you can just return "not found".
  
  **Machine learning:** ML approaches don't really work on strings, so you need to encode word numerically. Ideally this should not be a random mapping (such as alphabetical order), so one approach that is used is to represent each word by a vector that encodes which other words occur in its neighbourhood. It is a well-known principle in corpus linguistics (since the 1930s) that words are not randomly distributed in a text, but that some words co-occur with regularity. For example, *rasher* and *bacon* usually go together. So the vector which describes *rasher* will have a positive number in the slot which relates to *does it occur with 'bacon'?*, whereas the vector describing *bookshelf* will not. With a large enough vector, you can approximate similarities in meaning between words (based on their lexcial environments). Unknown words: you could use a random or average vector -- I don't know what the standard way is to deal with that.
  
  
  ---
  
  