2017-05-16 1 views
0

나는 제품 설명의 거대한 데이터를 가지고 있으며, POS 태그로 텍스트를 태그 한 후에 NNP 태그를 구분하는 것을 발견 한 설명에서 제품 이름과 의도를 분리해야하는데, 이는 더 많은 클렌징에 도움이됩니다.목록에서 특정 POS 태그를 필터링하여 목록을 구분하는 방법은 무엇입니까?

NNP 태그 만 필터링하고 해당 목록에서 필터링하려고하지만 다음과 같이 비슷한 데이터가 있습니다.

['User', 
    'Coala', 
    'VWR', 
    'Arfter', 
    'COALA', 
    'Category', 
    'S9901', 
    'Dummy', 
    'POETcatalog', 
    'Vendor', 
    'VWR', 
    'COALA', 
    'COALA', 
    'User', 
    'Universitaet', 
    'Regensburg', 
    'Scout', 
    'P17', 
    'YESRMCDMUSER01', 
    'Merck', 
    'KGaA', 
    'Please'] 

내가 좀하고 싶습니다 출력은 다음과 같습니다 :

[['User', 
    'Coala', 
    'VWR'] 
['Arfter', 
'COALA', 
'Category', 
'S9901', 
'Dummy'] 
[], 
[], 
['POETcatalog'], 
['Vendor', 
'VWR', 
'COALA'], 
[], 
['COALA'], 
['User', 
'Universitaet', 
'Regensburg', 
'Scout', 
'P17', 
'YESRMCDMUSER01', 
'Merck', 
'KGaA'], 
['Please']] 

다음과 같은 출력을주고있다

def prodname(a): 
    p = [] 
    for i in a: 
     for j in range(len(i)): 
      if i[j][1]=='NNP': 
       p.append(i[j][0]) 
    return p 

:

data = [[('User', 'NNP'), 
    ('is', 'VBZ'), 
    ('not', 'RB'), 
    ('able', 'JJ'), 
    ('to', 'TO'), 
    ('order', 'NN'), 
    ('products', 'NNS'), 
    ('from', 'IN'), 
    ('iShopCatalog', 'NN'), 
    ('Coala', 'NNP'), 
    ('excluding', 'VBG'), 
    ('articles', 'NNS'), 
    ('from', 'IN'), 
    ('VWR', 'NNP')], 
[('Arfter', 'NNP'), 
    ('transferring', 'VBG'), 
    ('the', 'DT'), 
    ('articles', 'NNS'), 
    ('from', 'IN'), 
    ('COALA', 'NNP'), 
    ('to', 'TO'), 
    ('SRM', 'VB'), 
    ('the', 'DT'), 
    ('Category', 'NNP'), 
    ('S9901', 'NNP'), 
    ('Dummy', 'NNP'), 
    ('is', 'VBZ'), 
    ('maintained', 'VBN')], 
[('Due', 'JJ'), 
    ('to', 'TO'), 
    ('this', 'DT'), 
    ('the', 'DT'), 
    ('user', 'NN'), 
    ('is', 'VBZ'), 
    ('not', 'RB'), 
    ('able', 'JJ'), 
    ('to', 'TO'), 
    ('order', 'NN'), 
    ('the', 'DT'), 
    ('product', 'NN')], 
[('All', 'DT'), 
    ('other', 'JJ'), 
    ('users', 'NNS'), 
    ('can', 'MD'), 
    ('order', 'NN'), 
    ('these', 'DT'), 
    ('articles', 'NNS')], 
[('She', 'PRP'), 
    ('can', 'MD'), 
    ('order', 'NN'), 
    ('other', 'JJ'), 
    ('products', 'NNS'), 
    ('from', 'IN'), 
    ('a', 'DT'), 
    ('POETcatalog', 'NNP'), 
    ('without', 'IN'), 
    ('any', 'DT'), 
    ('problems', 'NNS')], 
[('Furtheremore', 'IN'), 
    ('she', 'PRP'), 
    ('is', 'VBZ'), 
    ('able', 'JJ'), 
    ('to', 'TO'), 
    ('order', 'NN'), 
    ('products', 'NNS'), 
    ('from', 'IN'), 
    ('the', 'DT'), 
    ('Vendor', 'NNP'), 
    ('VWR', 'NNP'), 
    ('through', 'IN'), 
    ('COALA', 'NNP')], 
[('But', 'CC'), 
    ('articles', 'NNS'), 
    ('from', 'IN'), 
    ('all', 'DT'), 
    ('other', 'JJ'), 
    ('suppliers', 'NNS'), 
    ('are', 'VBP'), 
    ('not', 'RB'), 
    ('orderable', 'JJ')], 
[('I', 'PRP'), 
    ('already', 'RB'), 
    ('spoke', 'VBD'), 
    ('to', 'TO'), 
    ('anic', 'VB'), 
    ('who', 'WP'), 
    ('maintain', 'VBP'), 
    ('the', 'DT'), 
    ('catalog', 'NN'), 
    ('COALA', 'NNP'), 
    ('and', 'CC'), 
    ('they', 'PRP'), 
    ('said', 'VBD'), 
    ('that', 'IN'), 
    ('the', 'DT'), 
    ('reason', 'NN'), 
    ('should', 'MD'), 
    ('be', 'VB'), 
    ('the', 'DT'), 
    ('assignment', 'NN'), 
    ('of', 'IN'), 
    ('the', 'DT'), 
    ('plant', 'NN')], 
[('User', 'NNP'), 
    ('is', 'VBZ'), 
    ('a', 'DT'), 
    ('assinged', 'JJ'), 
    ('to', 'TO'), 
    ('Universitaet', 'NNP'), 
    ('Regensburg', 'NNP'), 
    ('in', 'IN'), 
    ('Scout', 'NNP'), 
    ('but', 'CC'), 
    ('in', 'IN'), 
    ('P17', 'NNP'), 
    ('table', 'NN'), 
    ('YESRMCDMUSER01', 'NNP'), 
    ('she', 'PRP'), 
    ('is', 'VBZ'), 
    ('assigned', 'VBN'), 
    ('to', 'TO'), 
    ('company', 'NN'), 
    ('001500', 'CD'), 
    ('Merck', 'NNP'), 
    ('KGaA', 'NNP')], 
[('Please', 'NNP'), 
    ('find', 'VB'), 
    ('attached', 'JJ'), 
    ('some', 'DT'), 
    ('screenshots', 'NNS')]] 

나는 다음과 같은 코드를 작성도 사용하려고했습니다.은 해당 목록에 추가 할 수는 있지만 그렇게 할 수는 없습니다.

답변

1

당신은 그냥이 지능형리스트를 사용할 수 있습니다

[[j[0] for j in i if j[-1]=="NNP"] for i in data] 

출력 :

[['User', 'Coala', 'VWR'], ['Arfter', 'COALA', 'Category', 'S9901', 'Dummy'], [], [], ['POETcatalog'], ['Vendor', 'VWR', 'COALA'], [], ['COALA'], ['User', 'Universitaet', 'Regensburg', 'Scout', 'P17', 'YESRMCDMUSER01', 'Merck', 'KGaA'], ['Please']] 
+0

덕분에 많은

여기에 솔루션을 읽기 쉬운을합니다. 그거야. – sharathchandramandadi

0

목록 이해가 갈 수있는 방법입니다. 그러나 @McGrady 대답은 읽기가 조금 어려울 수도 있습니다.

document = [[('User', 'NNP'), ('is', 'VBZ'), ('not', 'RB'), ('able', 'JJ'), ('to', 'TO'), ('order', 'NN'), ('products', 'NNS'), ('from', 'IN'), ('iShopCatalog', 'NN'), ('Coala', 'NNP'), ('excluding', 'VBG'), ('articles', 'NNS'), ('from', 'IN'), ('VWR', 'NNP')], [('Arfter', 'NNP'), ('transferring', 'VBG'), ('the', 'DT'), ('articles', 'NNS'), ('from', 'IN'), ('COALA', 'NNP'), ('to', 'TO'), ('SRM', 'VB'), ('the', 'DT'), ('Category', 'NNP'), ('S9901', 'NNP'), ('Dummy', 'NNP'), ('is', 'VBZ'), ('maintained', 'VBN')], [('Due', 'JJ'), ('to', 'TO'), ('this', 'DT'), ('the', 'DT'), ('user', 'NN'), ('is', 'VBZ'), ('not', 'RB'), ('able', 'JJ'), ('to', 'TO'), ('order', 'NN'), ('the', 'DT'), ('product', 'NN')], [('All', 'DT'), ('other', 'JJ'), ('users', 'NNS'), ('can', 'MD'), ('order', 'NN'), ('these', 'DT'), ('articles', 'NNS')], [('She', 'PRP'), ('can', 'MD'), ('order', 'NN'), ('other', 'JJ'), ('products', 'NNS'), ('from', 'IN'), ('a', 'DT'), ('POETcatalog', 'NNP'), ('without', 'IN'), ('any', 'DT'), ('problems', 'NNS')], [('Furtheremore', 'IN'), ('she', 'PRP'), ('is', 'VBZ'), ('able', 'JJ'), ('to', 'TO'), ('order', 'NN'), ('products', 'NNS'), ('from', 'IN'), ('the', 'DT'), ('Vendor', 'NNP'), ('VWR', 'NNP'), ('through', 'IN'), ('COALA', 'NNP')], [('But', 'CC'), ('articles', 'NNS'), ('from', 'IN'), ('all', 'DT'), ('other', 'JJ'), ('suppliers', 'NNS'), ('are', 'VBP'), ('not', 'RB'), ('orderable', 'JJ')], [('I', 'PRP'), ('already', 'RB'), ('spoke', 'VBD'), ('to', 'TO'), ('anic', 'VB'), ('who', 'WP'), ('maintain', 'VBP'), ('the', 'DT'), ('catalog', 'NN'), ('COALA', 'NNP'), ('and', 'CC'), ('they', 'PRP'), ('said', 'VBD'), ('that', 'IN'), ('the', 'DT'), ('reason', 'NN'), ('should', 'MD'), ('be', 'VB'), ('the', 'DT'), ('assignment', 'NN'), ('of', 'IN'), ('the', 'DT'), ('plant', 'NN')], [('User', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ('assinged', 'JJ'), ('to', 'TO'), ('Universitaet', 'NNP'), ('Regensburg', 'NNP'), ('in', 'IN'), ('Scout', 'NNP'), ('but', 'CC'), ('in', 'IN'), ('P17', 'NNP'), ('table', 'NN'), ('YESRMCDMUSER01', 'NNP'), ('she', 'PRP'), ('is', 'VBZ'), ('assigned', 'VBN'), ('to', 'TO'), ('company', 'NN'), ('001500', 'CD'), ('Merck', 'NNP'), ('KGaA', 'NNP')], [('Please', 'NNP'), ('find', 'VB'), ('attached', 'JJ'), ('some', 'DT'), ('screenshots', 'NNS')]] 
output = [[word for word, pos in sentence if pos=='NNP'] for sentence in document] 

당신이 청소기 코드를 좋아하고 당신이 당신의 머리 주위에 중첩 된 지능형리스트를 포장 할 수있는 경우, https://stackoverflow.com/a/3633145/610569 :

output = [word for sentence in document for word, pos in sentence if pos=='NNP'] 
관련 문제