Skip to main content
 首页 » 编程设计

python :getting the count for the adjectives in a string

2025年05月04日31java哥

<分区>

我有一个字符串 s=“X 先生很棒。他很棒。Y 先生也很棒。”

我需要从字符串中提取所有形容词以及每个形容词的数量。例如 该字符串包含形容词“awesome”、“amazing”,其中 2 表示 awesome,1 表示 amazing。

为了提取形容词,我使用了 NLTK。这是提取形容词的代码,

adjectives =[token for token, pos in nltk.pos_tag(nltk.word_tokenize(b)) if pos.startswith('JJ')] 

我需要代码为字符串中的每个形容词获取一个计数器。 它应该像 形容词:反对