如何识别字符串中的名词并将其大写?

How to identify Nouns in string and capitalize them?

我有简单的小写纯文本,没有标点符号。是否有任何库可以帮助更改大写字母,例如名词所在的位置或需要的位置?像某某先生之后的名字?任何解决方案或指导性提示都可能非常有帮助。 例如: 英文 英文 .. 纯文本,在几个地方是名字。还有几个名字需要大写。喜欢

mr. john is living in canada

Mr. John is living in Canada

这是一个使用 nltk 库来识别名词的解决方法,使用 pos_tag 功能:

#Import nltk modules

import nltk
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag

text = "mr. john is living in canada"

#Define a function to extract nouns from the string

def ExtractNoun(sentence):
    sentence = nltk.word_tokenize(sentence)
    sentence = nltk.pos_tag(sentence)
    return sentence

sent = ExtractNoun(text)

#This will return a tuple of tokens and tags

print(sent)
[('mr.', 'NN'), ('john', 'NN'), ('is', 'VBZ'), ('living', 'VBG'), ('in', 'IN'), ('canada', 'NN')]

#Create a list of nouns

nn = [i[0] for i in sent if i[1] == 'NN']

#Capitalize the nouns which are matching with the list

text_cap = " ".join([x.capitalize() if x in nn else x for x in text.split()])
print(text_cap)

'Mr. John is living in Canada'

希望这有效!!