import re
text = "When_WRB it_PRP 's_VBZ time_NN for_IN their_PRP$ biannual_JJ powwow_NN ,_, the_DT nation_NN 's_POS manufacturing_NN titans_NNS typically_RB jet_VBP off_RP to_TO the_DT sunny_JJ confines_NNS of_IN resort_NN towns_NNS like_IN Boca_NNP Raton_NNP and_CC Hot_NNP Springs_NNP ._."
pos_file = "CC  CONJ  \nCD  NUM  \nDT  DET  \nEX  DT  \nFW  X  \nIN  ADP    \nJJ  ADJ    \nJJR ADJ   \nJJS ADJ    \nLS  X    \nMD  VERB    \nNN  NOUN  \nNNS NOUN  \nNNP NOUN  \nNNPS    NOUN  \nPDT DET \nPOS PRT  \nPRP PRON  \nPRP$    PRON  \nRB  ADV  \nRBR ADV  \nRBS ADV  \nRP  PRT  \nSYM X  \nTO  PRT  \nUH  X  \nVB  VERB  \nVBZ VERB  \nVBP VERB  \nVBD VERB  \nVBN VERB  \nVBG VERB  \nWDT DET  \nWP  PRON  \nWP$ PRON  \nWRB ADV  \n.   .  \n,   . \n:   .  \n(   .  \n)   .  "
dict_pos = {}
for line in pos_file.splitlines():
    c = line.strip().split()
    dict_pos[c[1]] = dict_pos.get(c[1], list()) + [c[0]]

def to_regex(x):
	r = []
	if x[0].isalnum() or x[0] == '_':
		r.append(r'(?<![^\W_])')
	else:
		if any(l.isalnum() or l=='_' for l in x):
			r.append(r'\B')
	r.append(re.escape(x))
	if x[-1].isalnum() or x[-1] == '_':
		r.append(r'\b')
	else:
		if any(l.isalnum() or l=='_' for l in x):
			r.append(r'\B')
	return "".join(r)

rx_dctvals = {}
for key, val in dict_pos.items():
    rx_dctvals[re.compile("|".join(sorted([to_regex(v) for v in val], key=len, reverse=True)))] = key

for rx, repl in rx_dctvals.items():
	text = rx.sub(repl.replace('\\', '\\\\'), text)

print(text)