Regex 使用NLTK/Python3标记未拆分的工作
我有未拆分的单词,如Regex 使用NLTK/Python3标记未拆分的工作,regex,python-3.x,nltk,Regex,Python 3.x,Nltk,我有未拆分的单词,如PageMetadataServiceConsumer,PowerSellerUpdateConsumerApplication,MetaDataDomain等。这些单词没有任何标点符号或动词。但是当我们看到这个词时,我们知道它们是由什么组成的 是否有一种方法可以使用nltk将PowerSellerUpdateConsumerApplication拆分为Power、卖家、更新、消费者、应用程序?您可以尝试以下方法: 其思想是将拆分器字符串(在下面的字符串中是####)附加到大
PageMetadataServiceConsumer
,PowerSellerUpdateConsumerApplication
,MetaDataDomain
等。这些单词没有任何标点符号或动词。但是当我们看到这个词时,我们知道它们是由什么组成的
是否有一种方法可以使用nltk将
PowerSellerUpdateConsumerApplication
拆分为Power
、卖家
、更新
、消费者
、应用程序
?您可以尝试以下方法:
其思想是将拆分器字符串(在下面的字符串中是####)附加到大写字符的左侧。。。如果您认为#####可能以字符串形式出现,则可以使用~!@*@&$@!或者任何你认为100%安全的东西,根本不出现在字符串中
解释
[A-Z] #First char with capital letter
(?! #START Negative Look ahead: Do not match if the first char is followed by this
[a-z]*\b #do not match if it ends with a word boundary \b(last part)
) #END Negative Look ahead
[a-z]+ #Select all the remaining lower case chars.
a=re.sub(reg,'\g<0> ',s) #Replace the matches with match \g<0> by appending a space to it.
输出
Page Metadata Service Consumer, Power Seller Update Consumer Application, Meta Data Domain
Page
Metadata
Service
Consumer
Power
Seller
Update
Consumer
Application
Meta
Data
Domain
在
agePowerSellerUpd#####ateConsumer
甚至agePowerSellerUpdate###Consumer
你试图展示的东西可以通过一些永远不会出现或指定为单词或任何东西来解决,例如~!!*@!bulbus~!!@*@!!可以用来代替####。。。我想这很容易理解。我想说的是,你可以使用第一个re来拆分,而不是使用第二个re!不,在我的正则表达式中,如果我第一次使用split,则大写字符将消失,或者如果我将其作为捕获组,则大写字符将作为不同的单词单独出现。我的意思是find而不是split。你的正则表达式将bbcblabla变成B,BcBlablabla@RizwanM.Tuman不,没有,你能再试一次吗?我的链接可能有一个旧版本。与我的答案相比,使用“向前看”会使它变慢。。。您可以进行基准测试yourself@RizwanM.Tuman我必须给你答案!我投了赞成票yours@RizwanM.Tuman虽然根据OP的说法,他只需要说几句话,所以我的第二部分答案仍然比你的要快;-)这个解决方案对你有用吗?
[A-Z] #First char with capital letter
(?! #START Negative Look ahead: Do not match if the first char is followed by this
[a-z]*\b #do not match if it ends with a word boundary \b(last part)
) #END Negative Look ahead
[a-z]+ #Select all the remaining lower case chars.
a=re.sub(reg,'\g<0> ',s) #Replace the matches with match \g<0> by appending a space to it.
reg=r'[A-Z]+[a-z]+'
for a in re.findall(reg,s):
print(a)
Page
Metadata
Service
Consumer
Power
Seller
Update
Consumer
Application
Meta
Data
Domain