Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用NLTK/Python3标记未拆分的工作_Regex_Python 3.x_Nltk - Fatal编程技术网

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