2017-10-05 15 views
0

我有非分詞,例如PageMetadataServiceConsumer,PowerSellerUpdateConsumerApplication,​​等。這些是沒有任何標點符號或動詞的詞。但是當我們看這個詞的時候,我們知道它們是由什麼組成的。使用NLTK/Python3令未分割的工件Tokenizing

有沒有辦法來PowerSellerUpdateConsumerApplication分成PowerSellerUpdateConsumerApplication使用NLTK

+0

沒有爲您的解決方案的工作? –

回答

1

你可以試試下面的辦法:

的想法是附加一個分路器串(以下字符串是###)爲大寫字符(S)的左...如果你覺得莫名其妙###可能會顯示爲一個字符串,那麼您可以使用任何類似〜!@ * @ & $ @#!或者你認爲100%安全的任何東西都不會出現在字符串中。

Run Here

import re 

regex = r"([A-Z]+)" 
test_str = "agePowerSellerUpdateConsumerApplicationMetaDataDomainageMetadataServiceConsumerBBc" 
subst = "###\\1" 
result = re.sub(regex, subst, test_str, 0) 

if result: 
    print(re.split("###", result)) 
+0

試一試'agePowerSellerUpd ### ateConsumer'或甚至'agePowerSellerUpdate ### Consumer' – kaza

+0

你試圖放置的東西可以通過永遠不會發生或指定爲一個單詞或任何東西來解決〜!! @ @!延髓〜!! @ * @!可以用來代替### ...我認爲這很容易理解 –

+0

所有我想說的是你可以使用第一次重新分割本身,而不是使用第二次重新! – kaza

0
import re 
s='PageMetadataServiceConsumer, PowerSellerUpdateConsumerApplication, MetaDataDomain' 
reg=r'[A-Z](?![a-z]*\b)[a-z]+' 
a=re.sub(reg,'\g<0> ',s) 
print(a) 

OUTPUT

Page Metadata Service Consumer, Power Seller Update Consumer Application, Meta Data Domain 

說明

[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. 

工作正則表達式here。 工作python示例here


如果你只是想的話,然後用下面的: -

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 
+0

你的正則表達式使BBcBlablabla變成B,Bc Blablabla –

+0

@ RizwanM.Tuman不,它不會重試嗎?我的鏈接可能有一箇舊版本。 – kaza

+0

使用lookahead使它比我的答案慢...你可以標杆自己 –