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
