POS-tagging code examples
The following code snippets illustrate some of the simple tasks we can do with knowledge of POS-tags. These examples don't achieve too much in terms of in-depth text analysis, but offer a quick glance at text manipulation once we have our text processed.
def make_verb_upper(text, pos): return text.upper() if pos == "VERB" else text doc = nlp(u'Tom ran swiftly and walked slowly') text = ''.join(make_verb_upper(w.text_with_ws, w.pos_) for w in doc) print(text)
As the function name suggests, the preceding code is to change all the verbs of the sentence into uppercase. By doing a quick check of the POS-tag and the basic string function upper
, we can achieve this in 5 lines!
Another popular task often done during analysis of text is to count the occurrences of each kind of POS. This can be done quite quickly with the following code snippet, where we find out the number of occurrences of these words in the 1st Harry Potter book (which you would buy/download and save...