concept pattern in category nlp

appears as: patterns, pattern, The patterns, patterns, pattern, The pattern
Real-World Natural Language Processing

This is an excerpt from Manning's book Real-World Natural Language Processing.

The main paradigm for MT that remained dominant both in academia and industry before the advent of neural machine translation (NMT) is called statistical machine translation (SMT). The idea behind it is very simple—learn how to translate from data, not by manually crafting rules. Specifically, SMT systems learn how to translate from datasets that contain a collection of texts in the source language and their translation in the target language. Such datasets are called parallel corpora (or parallel texts, or bitexts). By looking at a collection of paired sentences in both languages, the algorithm seeks patterns of how words in one language should be translated to another. The resulting statistical model is called a translation model. At the same time, by looking at a collection of target sentences, the algorithm can learn what valid sentences in the target languages should look like. Sounds familiar? This is exactly what a language model is all about (see the previous chapter). The final SMT model combines these two models and produces output that is a plausible translation of the input and is a valid, fluent sentence in the target language on its own.

Now, let’s represent word embeddings as patterns. We’ll draw a black circle for value -1 and a white one for 1. Then you can represent each word vector as a pair of two circles (see the table on the left in figure 7.3). Similarly, you can represent each two-word sentence as a small “patch” of two vectors, or four circles (see the table on the right in figure 7.3). Our task is beginning to look more like a pattern recognition task, where the network needs to learn black-and-white patterns that correspond to grammatical sentences.

Then, let’s think of a “filter” of same size (2 circles x 2 circles) that acts as the colored glass we talked about above. Each circle of this filter is also either black or white, corresponding to values -1 and 1. You are going to look at a pattern through this filter and determine if the pattern is the one you are looking for or not. You do this by putting the filter over a pattern and counting the number of color matches between the two. For each one of four positions, you get a score of +1 if the colors match (black-black or white-white) and a score of -1 if they don’t (black-white or white-black). Your final score is the sum of four scores, which varies from -4 (no matches) to +4 (four matches). See figure 7.4 for some examples.

Figure 7.4: Examples of convolutional filters
A close up of electronics Description automatically generated

The score you get varies depending on the pattern and the filter, but as you see in the figure, the score becomes larger when the filter looks similar to the pattern and becomes smaller when the two are not. You get the largest score (4) when the two match exactly and the smallest score (-4) when the two are exactly opposite. The filter is acting as a pattern detector against the input. Although this is a very simplified example, this is basically what a convolutional layer is doing. In convolutional neural networks, such filters are called kernels.

Natural Language Processing in Action: Understanding, analyzing, and generating text with Python

This is an excerpt from Manning's book Natural Language Processing in Action: Understanding, analyzing, and generating text with Python.

We won’t stay in this confusing binary world of logic for long, but let’s imagine we’re famous World War II-era code-breaker Mavis Batey at Bletchley Park and we’ve just been handed that binary, Morse code message intercepted from communication between two German military officers. It could hold the key to winning the war. Where would we start? Well the first step in our analysis would be to do something statistical with that stream of bits to see if we can find patterns. We can first use the Morse code table (or ASCII table, in our case) to assign letters to each group of bits. Then, if the characters are gibberish to us, as they are to a computer or a cryptographer in WWII, we could start counting them up, looking up the short sequences in a dictionary of all the words we’ve seen before and putting a mark next to the entry every time it occurs. We might also make a mark in some other log book to indicate which message the word occurred in, creating an encyclopedic index to all the documents we’ve read before. This collection of documents is called a corpus, and the collection of words or sequences we’ve listed in our index is called a lexicon.

If we’re lucky, and we’re not at war, and the messages we’re looking at aren’t strongly encrypted, we’ll see patterns in those German word counts that mirror counts of English words used to communicate similar kinds of messages. Unlike a cryptographer trying to decipher German Morse code intercepts, we know that the symbols have consistent meaning and aren’t changed with every key click to try to confuse us. This tedious counting of characters and words is just the sort of thing a computer can do without thinking. And surprisingly, it’s nearly enough to make the machine appear to understand our language. It can even do math on these statistical vectors that coincides with our human understanding of those phrases and words. When we show you how to teach a machine our language using Word2Vec in later chapters, it may seem magical, but it’s not. It’s just math, computation.

Even the phrase “machine-generated text” inspires dread of a hollow, tinned voice issuing a chopped list of words. Machines may get the point across, but little more than that. What’s missing? The tone, the flow, the character that you expect a person to express in even the most passing of engagements. Those subtleties exist between the words, underneath the words, in the patterns of how they’re constructed. As a person communicates, they will underlay patterns in their text and speech. Truly great writers and speakers will actively manipulate these patterns, to great effect. And your innate ability to recognize them, even if on a less-than-conscious level, is the reason machine-produced text tends to sound terrible. The patterns aren’t there. But you can find them in human-generated text and impart them to your machine friends.

In the past few years, research has quickly blossomed around neural networks. With widely available open source tools, the power of neural networks to find patterns in large datasets quickly transformed the NLP landscape. The perceptron quickly became the feedforward network (a multilayer perceptron), which led to the development of new variants: convolutional neural nets and recurrent neural nets, ever more efficient and precise tools to fish patterns out of large datasets.

  • Sentiment exists not only in the words but in the patterns that are used.
  • Listing 11.1. Pattern hardcoded in Python
    >>> def find_greeting(s):
    ...     """ Return greeting str (Hi, etc) if greeting pattern matches """
    ...     if s[0] == 'H':
    ...         if s[:3] in ['Hi', 'Hi ', 'Hi,', 'Hi!']:
    ...             return s[:2]
    ...         elif s[:6] in ['Hello', 'Hello ', 'Hello,', 'Hello!']:
    ...             return s[:5]
    ...     elif s[0] == 'Y':
    ...         if s[1] == 'o' and s[:3] in ['Yo', 'Yo,', 'Yo ', 'Yo!']:
    ...             return s[:2]
    ...     return None