如何用Python对单词分类为普通名词、专有名词、地点并识别动词?
嘿,我来帮你搞定这个单词分类的需求!你可以借助NLTK的词性标注工具来精准区分不同类型的单词,再结合命名实体识别处理地点分类,下面是完整的实现方案:
第一步:完整代码实现
先确保你已经安装了NLTK,然后运行以下代码就能自动完成分类:
import nltk # 下载必要的NLTK资源(首次运行需要) nltk.download('averaged_perceptron_tagger') nltk.download('maxent_ne_chunker') nltk.download('words') # 你的目标单词列表 word_list = ["abatable", "abate", "abated", "abatement", "abatements", "abbots", "abbreviate", "abbreviated", "abbreviates"] # 初始化各分类容器 common_nouns = [] proper_nouns = [] locations = [] verbs = [] # 对单词进行词性标注 tagged_words = nltk.pos_tag(word_list) # 根据词性标签分类 for word, tag in tagged_words: # 动词涵盖所有动词词性标签(原形、过去式、分词、三单形式等) if tag.startswith('VB'): verbs.append(word) # 普通名词包含单数和复数形式 elif tag in ['NN', 'NNS']: common_nouns.append(word) # 专有名词包含单数和复数形式 elif tag in ['NNP', 'NNPS']: proper_nouns.append(word) # 识别地点类单词(借助命名实体识别) for word in word_list: chunks = nltk.ne_chunk(nltk.pos_tag([word])) for chunk in chunks: if hasattr(chunk, 'label') and chunk.label() == 'GPE': locations.append(word) # 输出分类结果(也可以直接生成表格) print("普通名词:", common_nouns) print("专有名词:", proper_nouns) print("地点:", locations) print("动词:", verbs)
第二步:生成分类表格
根据你的单词列表,最终分类结果整理成表格如下:
| 分类类别 | 对应单词列表 |
|---|---|
| 普通名词(Common noun) | abatement, abatements, abbots |
| 专有名词(Proper noun) | 无 |
| 地点(Location) | 无 |
| 动词(Verb) | abate, abated, abbreviate, abbreviated, abbreviates |
补充说明
abatable是形容词,不属于你指定的四类分类,因此不会出现在任何一列中。- 你的单词列表里没有符合专有名词和地点类的词汇,所以这两列显示“无”。
内容的提问来源于stack exchange,提问作者Cyley Simon
相关产品推荐
相关产品推荐

