itertools.groupby, , , .
:
groups = itertools.groupby(bb, lambda x: x[1][:2] in ['IN', 'NN'])
result = [list(b) for a,b in groups if a]
result = [[w[0] for w in b] for b in result if 'IN' in [w[1] for w in b]]
print(result)
[['company', 'whose', 'stock', 'has', 'been', 'on'],
['week', 'already', 'sells', 'its', 'graphics'],
['biggest', 'cloud', 'companies', 'just', 'that']]
, bb , ( "IN" "NN" ) false true ( ). , , :
groups = itertools.groupby(bb, lambda x: x[1][:2] in ['IN', 'NN'])
print([(a,list(b)) for a,b in groups])
[(False, [('The', 'RB')]),
(True,
[('company', 'NN'),
('whose', 'NNS'),
('stock', 'IN'),
('has', 'NNP'),
('been', 'NNS'),
('on', 'NNP')]),
(False, [('tear', 'VBJ'), ('this', 'VB')]),
(True,
[('week', 'NNS'),
('already', 'NN'),
('sells', 'IN'),
('its', 'NNP'),
('graphics', 'NNS')]),
(False, [('processing', 'VB'), ('units', 'VBJ')]),
(True,
[('biggest', 'NNS'),
('cloud', 'NN'),
('companies', 'IN'),
('just', 'NNP'),
('that', 'IN')])]
, , . , , , true ( ), , 'IN' .
, , ( ), :
[[w[0] for w in b] for b in [list(b) for a,b in itertools.groupby(bb, lambda x: x[1][:2] in ['IN', 'NN']) if a] if 'IN' in [w[1] for w in b]]
, "IN" 'NN' , :
groups results, :
groups = itertools.groupby(bb, lambda x: x[1][:2] in ['IN', 'NN'])
result = [list(b) for a,b in groups if a]
groupby , , 'IN':
result = [[(a,list(b)) for a,b in itertools.groupby(r, lambda x: x[1] == 'IN')] for r in result]
result , boolean groupby true (POS - 'IN'), ( 0 > -1)
result = [[b for i,(a,b) in enumerate(r) if (a and i not in [0,len(r)-1]) or not a] for r in result]
, , POS, ( . )
result = [[w[0] for sub in r for w in sub] for r in result]
print(result)
[['company', 'whose', 'stock', 'has', 'been', 'on'],
['week', 'already', 'sells', 'its', 'graphics'],
['biggest', 'cloud', 'companies', 'just']]