liveBook · Manning

Part of speech

As explained in Natural Language Processing in Action, Second Edition

A part of speech is a grammatical category such as noun, verb, or adverb assigned to a word according to its syntactic role.

A part of speech (POS) labels a word by its grammatical role, such as adjective, noun, verb, or adverb. These labels let a text-processing system look for a pattern in how words are used, rather than merely matching their spelling. For example, a two-word pattern can look for an adjective followed by a noun. section 7.2.2

Find an adjective–noun pair

In “right ones in the right order,” spaCy labels both instances of “right” ADJ and labels “ones” and “order” NOUN. A sliding two-token stencil therefore matches “right ones” and “right order,” but not “ones in”: its labels are NOUN and ADP. ADP is the NLP category for adpositions, including prepositions. section 7.2.2

>>> pd.get_dummies(df, columns=['pos_'], prefix='', prefix_sep='')text  ADJ  ADP  AUX  DET  NOUN  PRON  VERB
0   right    1    0    0    0     0     0     0
1    ones    0    0    0    0     1     0     0
2      in    0    1    0    0     0     0     0
3     the    0    0    0    1     0     0     0
4   right    1    0    0    0     0     0     0
5   order    0    0    0    0     1     0     0
6     you    0    0    0    0     0     1     0
7     can    0    0    1    0     0     0     0
8   nudge    0    0    0    0     0     0     1
9     the    0    0    0    1     0     0     0
10  world    0    0    0    0     1     0     0

One-hot columns turn each POS label into numbers. A pattern matcher can inspect adjacent rows instead of comparing word strings. section 7.2.2

For a longer pattern, arrange POS indicators as channels in a token sequence. A PyTorch Conv1d filter with positions for ADV, VERB, and NOUN scores each three-token window. The sequence “rightly timed pause” receives a score of 3 because all three positions align; lower scores can indicate partial alignment. This hand-set filter illustrates convolution before moving to word embeddings that also represent aspects of meaning. section 7.2.6

Listing 7.9 Running a single example through a convolutional layer
>>> y = np.array(conv.forward(x).detach()).squeeze()
>>> df.loc['y'] = pd.Series(y)
>>> df
        0      1     2    3    4   ...   15       16     17     18   19
token  The  right  word  may   be  ...    a  rightly  timed  pause    .
ADV      0      0     0    0    0  ...    0        1      0      0    0
ADJ      0      1     0    0    0  ...    0        0      0      0    0
VERB     0      0     0    1    0  ...    0        0      1      0    0
NOUN     0      0     1    0    0  ...    0        0      0      1    0
y      1.0    0.0   1.0  0.0  0.0  ...  0.0      3.0    0.0    NaN  NaN

The output row shows the filter’s score at successive positions, including the score of 3 at “rightly timed pause.” section 7.2.6

POS can also affect normalization. A WordNet lemmatizer leaves “better” unchanged without a POS argument but returns “good” when told it is an adjective. Likewise, “goods” becomes “good” when treated as a noun, not as an adjective. Grammatical information is useful, but discarding cues such as capitalization can lose distinctions relevant to proper nouns and named entity recognition. section 2.3.2 section 2.3.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through one-hot POS vectors and hand-set convolution filters, without detailing how a tagger assigns labels.
Examples
Tests adjective–noun pairs, scores an adverb–verb–noun phrase, and compares WordNet lemmas under different POS arguments.
What it assumes
Assumes basic Python data handling and tokens; demonstrates POS-vector construction rather than teaching tagger implementation.
What you can do after
Apply it via spaCy and PyTorch to encode grammatical labels and score token patterns.
How it connects
Connects POS to one-hot encoding, convolutional neural networks, word embeddings, lemmatization, and named entity recognition.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.