Ipsator
Ipsator journal

NLP

How Text Became Numbers: From Simple Tricks to Intelligent Language Models

A journey through the evolution of text representation techniques, from simple encodings to powerful transformer-based models, showing how words became numbers that machines can truly understand.

How Text Became Numbers: From Simple Tricks to Intelligent Language Models

Have you ever wondered how computers manage to understand language? For us, words and sentences feel natural, but for computers, it’s a challenge because they only work with numbers. To help machines make sense of text, we need to turn words into numbers that they can process. Over the years, researchers have developed many ways to do this, starting with basic methods and moving toward more advanced techniques, like the language models we see today such as ChatGPT and BERT. In this blog, we’ll explore the key steps in this journey and see how each new idea has helped computers get better at understanding human language.

1. Label Encoding

The easiest way to turn words into numbers is to simply assign each word a number. For example, “apple” might become 1, “banana” becomes 2, and so on. This method is called label encoding. It’s basic and uses little memory. Label Encoding can be used to convert predictable categories of data into integers so that the predictive model can work properly.

Applications:

  • Label encoding translates categorical values into numbers, making non-numeric data usable for machine learning models.
  • Each unique category or label is assigned a distinct integer, for example: “Apple → 0, Banana → 1, Orange → 2”.
  • Works well with ordinal data (categories with an inherent order), such as education levels or customer ratings.

Limitations:

  • Does not actually understand the textual data. Imposes artificial ordinal relationships on categorical text data.
  • Cannot handle unseen (out-of-vocabulary) tokens during prediction.
  • Not suitable for algorithms relying on distance metrics, introducing misleading “closeness”

Code Example:

from sklearn.preprocessing import LabelEncoder
words = ['apple', 'banana', 'carrot']
encoder = LabelEncoder()
labels = encoder.fit_transform(words)
print(labels)    # Output: [0 1 2]

2. One-Hot Encoding

One-hot encoding prevents the “ordering” mistake from label encoding. Each word is unique, but there’s no information about similarity or meaning between words. If you have a vocabulary of 10,000 words, you end up with 10,000-dimensional vectors—usually mostly zero! It’s quick for small vocabularies, but unmanageable for big ones.

Applications:

  • Widely used for handling categorical features in linear models (e.g., logistic regression, support vector machines) and neural networks.
  • Useful for text and NLP tasks where words, tags, or entities must be converted to numeric arrays for modeling.
  • Applied in preprocessing for KNN, tree-based, ensemble, and deep learning models to fully leverage categorical feature information

Limitations

  • Does not capture any semantic relationships or similarity between words or categories, treating each as completely independent.
  • Can cause curse of dimensionality problems, adversely affecting model performance with large vocabularies.
  • May lead to overfitting, especially with limited data and many categories.
  • Does not handle out-of-vocabulary or unseen tokens naturally; adding new categories requires retraining or re-encoding.

Code Example:

from sklearn.preprocessing import OneHotEncoder
import numpy as np
words = np.array(['apple', 'banana', 'carrot']).reshape(-1, 1)
encoder = OneHotEncoder(sparse=False)
one_hot = encoder.fit_transform(words)
print(one_hot)

3. N-Gram (Bi-gram/Trigram) Representation

Instead of looking at single words (“unigrams”), n-gram encoding breaks texts into contiguous sequences of n items – for instance “new york” (bigram) or “machine learning is” (trigram). N-grams capture local context, which is vital for basic text classification, sentiment analysis, or bag-of-words models. However, as n increases, the unique combinations skyrocket, making your feature matrix bigger and sparser.

Applications:

  • Used widely in language modeling to predict the next word or sequence of words based on previous words, improving text generation and speech recognition.
  • Supports information retrieval and document clustering by representing documents more comprehensively through word combinations.

Limitations:

  • Capture only local context, failing to model long-range dependencies in language.
  • Can overfit on training data, especially with large n, reducing generalization capability.

Code Example:

from sklearn.feature_extraction.text import CountVectorizer
texts = ["machine learning is fun", "learning machines is exciting"]
vectorizer = CountVectorizer(ngram_range=(2,2)) # Bigram
features = vectorizer.fit_transform(texts)
print(vectorizer.get_feature_names_out())
# ['is exciting' 'is fun' 'learning is' 'learning machines' 'machine learning' 'machines is']

4. TF-IDF (Term Frequency-Inverse Document Frequency)

Measures how important a word is in a document relative to a whole collection. Frequent words in one document get higher score, but if they appear everywhere, their score is reduced. TF-IDF helps identify discriminative words for document retrieval, spam detection, and baseline text classification. It does not understand synonyms or semantics—“dog” and “canine” are totally different.

Applications:

  • Used in information retrieval to rank documents based on the relevance of query terms, improving search engine accuracy.
  • Helps in text classification by identifying important terms that differentiate categories like spam detection or sentiment analysis.
  • Supports document similarity and clustering by converting text into numerical vectors for grouping related content.
  • Applied in text summarization by selecting key sentences that capture the essence of documents through important term weights.

Limitations:

  • Lacks contextual awareness; cannot distinguish different meanings of the same word depending on context.
  • Ignores semantic relationships and complex linguistic connections between words.
  • Highly sensitive to fluctuations in document frequency; term importance scores can vary with corpus changes, causing instability.
  • Does not capture the sequence of terms, ignoring word order entirely.

Code Example:

from sklearn.feature_extraction.text import TfidfVectorizer

docs = ["cats chase mice", "dogs love cats"]
tfidf = TfidfVectorizer()
X = tfidf.fit_transform(docs)
print(tfidf.get_feature_names_out())
# Output: ['cats', 'chase', 'dogs', 'love', 'mice']
print(X.toarray())
# [[0.44943642 0.6316672  0.         0.         0.6316672 ]
  [0.44943642 0.         0.6316672  0.6316672  0.        ]]

5. Word2Vec

Uses neural networks to learn a vector representation for each word by predicting neighboring words (Skip-gram) or by predicting a word from its neighbors (CBOW). Word2Vec allows arithmetic like king - man + woman ≈ queen, showing true semantic learning. Vectors are dense and capture similarity. Downside: Each word gets one vector, so it can’t distinguish “bank” in “river bank” from “bank” in “money bank.”

Applications:

  • Enhances semantic understanding by capturing word meanings and relationships in a continuous vector space, improving NLP tasks.
  • Used in search engines to improve query relevance by understanding context and synonyms.
  • Supports language translation by mapping words across languages in vector space, enabling better cross-lingual tasks.
  • Aids in recommendation systems by analyzing user behavior and item relations through embedding sequences.
  • Enables improved text summarization and question answering by capturing relationships between terms.

Limitations:

  • Has difficulty handling out-of-vocabulary (OOV) words; cannot generate embeddings for unseen words, often assigning random vectors.
  • It cannot inherently capture polysemy (multiple meanings of a word) since it generates a single vector per word.
  • Treats antonyms as similar, failing to distinguish opposites or nuanced semantic differences.
  • Context window size is fixed and may limit capturing broader contextual information in language.

Code Example:

from gensim.models import Word2Vec

sentences = [["dog", "barks"], ["cat", "meows"]]
model = Word2Vec(sentences, vector_size=10, min_count=1)
print(model.wv.similarity('dog', 'cat'))
# Output: Numeric similarity score between 'dog' and 'cat'

6. Transformers & BERT

BERT (Bidirectional Encoder Representations from Transformers) is a deep learning model built on the Transformer architecture, focused on understanding natural language context. It processes text in both directions at once (bidirectional), giving it a deep understanding of word meaning in context. BERT generates contextual embeddings for all words in a sentence or paragraph. It was pre-trained using tasks like masked language modeling (predict hidden words given surrounding context), enabling self-supervised learning. BERT’s output vectors can be adapted for tasks like text classification, question answering, and named entity recognition by adding a small neural layer and fine-tuning. It uses self-attention to weigh the importance of each word in relation to others, making parallel training, scalability, and context handling possible.

These days, we employ embedding models provided sentence-transformers, bge, instructor, nomic-ai, etc. in our RAG pipelines to find most similar text chunks to generate a chatbot response upon data similar to user prompt.

Applications:

  • Enhances question answering systems by understanding context and relationships in text.
  • Applied in text summarization to extract key points and generate concise summaries.
  • Excels in sentiment analysis, helping to detect emotions and opinions from text.
  • Effective in cross-lingual NLP tasks, including zero-shot learning and transfer learning across multiple languages.

Limitations:

  • Primarily designed for understanding tasks and lacks native support for natural language generation, making it less suitable for text generation tasks.
  • Interpretability of the model’s decisions can be challenging due to the black-box nature of deep transformer architectures.
  • Fine-tuning requires task-specific labeled data, which may not always be plentiful or accessible.
  • Require extremely high computational resources for training and inference, leading to increased energy use and carbon footprint.

Code Example:

from transformers import pipeline

classifier = pipeline('sentiment-analysis')
print(classifier("I can't stand this movie"))
# Output: [{'label': 'NEGATIVE', 'score': 0.99}]

7. GPT

GPT (Generative Pre-trained Transformer) is a deep learning model based on the Transformer architecture that excels at text generation and understanding. reads and processes text in a left-to-right fashion, predicting the next word given previous words (autoregressive). Uses only the decoder part of the transformer. It can be fine-tuned on specific tasks like summarization, translation, or conversation. Produces contextual embeddings and can create coherent sentences, paragraphs, or entire articles from a prompt. GPT is ideal for text generation, chatbots, creative writing, code synthesis, and even answering questions in conversational style. You give it a starting sentence (prompt), and it continues writing based on what it has learned.

We incorporate LLMs like ChatGPT, Gemini, Mistral, LlaMA, Grok, Qwen, etc. in our chatbot and AI Agentic pipelines to generate accurate structured and prompt based textual outputs to make our lives easier.

Applications:

  • Text generation: Produces coherent, contextually relevant sentences and paragraphs for tasks like content creation and basic writing prompts.
  • Question answering: Responds to factual or contextual questions by leveraging pretrained linguistic knowledge.
  • Text summarization: Generates concise summaries for longer documents, supporting faster information gathering.
  • Transfer learning: Serves as a general-purpose foundation that can be fine-tuned for new language tasks with minimal architectural changes, making it flexible for various NLP problems.

Limitations:

  • Smaller context window size, limiting handling of long-range dependencies and complex context.
  • Requires relatively large amounts of labeled data for fine-tuning to perform well on specific downstream tasks.
  • Struggles with complex reasoning and logical inference, as it mainly captures statistical patterns.

Code Example:

from transformers import pipeline

generator = pipeline('text-generation', model='gpt2')
print(generator("In the future, AI will", max_length=15))
# Output: Generated sentence, e.g., "In the future, AI will change the way we work..."

Conclusion

The journey from simple label encoding to powerful models like BERT and GPT highlights how far natural language processing has come. Early techniques gave computers a basic way to handle language as numbers, but struggled with meaning and context. With the arrival of word embeddings and especially transformer-based models, machines can now understand not just individual words, but also the relationships and subtleties within sentences and conversations.

Whether you’re building a basic classification model or experimenting with advanced generative AI, choosing the right encoding or embedding technique is a crucial first step. So, it’s very important that engineers know about these concepts before choosing the tools in NLP projects.

Now that we’ve seen how text becomes numbers, the next step is: how do we measure similarity between these numbers? That’s where cosine similarity, dot product, and other distance metrics come in. We’ll cover that in the next post. Thanks for reading!

Keep exploring

More from the journey

Newer article

Shielding High-Demand Systems from Fraud | Anti-Bot Strategies & Rate Limiting

A deep dive into anti-bot and anti-fraud strategies for high-demand systems like ticket sales or limited-edition launches. This guide covers how to combat new account fraud, implement effective rate limiting, use device fingerprinting, and build a layered defense to make your system an unprofitable target for malicious actors.

Read article
Earlier article

How 'Best Seller' and 'Must Try' Tags Power Smarter Food Choices on Food Delivery Platforms

How data-driven 'Best Seller' and 'Must Try' tags on food delivery platforms enhance user experience, boost restaurant visibility, and streamline menu discovery through intelligent, scalable backend systems

Read article