我想在终端上为句子着色,这样名词就会变成蓝色而动词会变成绿色。其他一切都是黑色的。
到目前为止,为了这个目的,我尝试使用nltk
和colorama
模块。
import nltk
from colorama import Fore
此代码会找出名词和动词,因此动词为VB
或VBD
,名词为NN
。
s = nltk.word_tokenize(sample_sentence)
tagged_text = nltk.pos_tag(s)
print tagged_text
[('Stately', 'RB'), (',', ','), ('plump', 'VB'), ('Buck', 'NNP'), ('Mulligan', 'NNP'), ('came', 'VBD'), ('from', 'IN'), ('the', 'DT'), ('stairhead', 'NN'), (',', ','), ('bearing', 'VBG'), ('a', 'DT'), ('bowl', 'NN'), ('of', 'IN'), ('lather', 'NN'), ('on', 'IN'), ('which', 'WDT'), ('a', 'DT'), ('mirror', 'NN'), ('and', 'CC'), ('a', 'DT'), ('razor', 'NN'), ('lay', 'NN'), ('crossed', 'VBD'), ('.', '.')]
当我想要打印彩色文字时,我会使用:
print Fore.BLUE + some_noun
print Fore.GREEN + some_verb
print Fore.BLACK + something_else
打印句子时遇到问题。如何循环tagged_text
以便打印sample_sentence
不变(仅应用所需的颜色)?
答案 0 :(得分:1)
这个怎么样?它保留了与原始文本完全相同的空白。我确实认为动词应该是红色的。
from colorama import Fore, init
import re
init()
tagged_text = [('Stately', 'RB'), (',', ','), ('plump', 'VB'), ('Buck', 'NNP'), ('Mulligan', 'NNP'), ('came', 'VBD'),
('from', 'IN'), ('the', 'DT'), ('stairhead', 'NN'), (',', ','), ('bearing', 'VBG'), ('a', 'DT'),
('bowl', 'NN'), ('of', 'IN'), ('lather', 'NN'), ('on', 'IN'), ('which', 'WDT'), ('a', 'DT'),
('mirror', 'NN'), ('and', 'CC'), ('a', 'DT'),('razor', 'NN'), ('lay', 'NN'), ('crossed', 'VBD'),
('.', '.'), ('The', 'DET'), ('function', 'NN'), ('f', 'SYM'), ('(','('),('x','SYM'),(',',','),
('y','SYM'),(')',')'),('takes','VB'), ('two', 'CD'), ('arguments', 'NN'), ('.','.')]
origtext = 'Stately, plump Buck Mulligan came from the stairhead, bearing a bowl of lather on which a mirror and a razor lay crossed. The function f(x,y) takes two arguments.'
colordict = {'VB': Fore.GREEN, 'VBD': Fore.GREEN, 'NN': Fore.BLUE}
colorwords = ''
for word, tag in tagged_text:
color = Fore.BLACK
word = re.match(r'\s*%s\s*' % re.escape(word), origtext).group()
origtext = origtext.split(word,1)[1]
if tag in colordict:
color = colordict[tag]
colorwords += color + word
print colorwords