1
votes

Regroupement de mots consécutifs contenant des balises B ou I

J'ai des données regardées:

data=[[('Natural', 'JJ', 'B'), ('language', 'NN', 'I'), ('processing', 'NN', 'I'), ('is', 'VBZ', 'O'), ('one', 'CD', 'O'), ('of', 'IN', 'O'), ('the', 'DT', 'O'), ('important', 'JJ', 'O'), ('branch', 'NN', 'O'), ('of', 'IN', 'O'), ('CS', 'NNP', 'B'), ('.', '.', 'I')],
[('Machine', 'NN', 'B'), ('learning', 'NN', 'I'), (',', ',', 'I'), ('deep', 'JJ', 'I'), ('learning', 'NN', 'I'), ('are', 'VBP', 'O'), ('heavily', 'RB', 'O'), ('used', 'VBN', 'O'), ('in', 'IN', 'O'), ('natural', 'JJ', 'B'), ('language', 'NN', 'I'), ('processing', 'NN', 'I'), ('.', '.', 'I')],
[('It', 'PRP', 'O'), ('is', 'VBZ', 'O'), ('too', 'RB', 'O'), ('cool', 'JJ', 'O'), ('.', '.', 'O')]]
Key_words = []
index = 0
for sen in data:
    for i in range(len(sen)):
        while index < len(sen):

Je veux regrouper les mots consécutifs qui ont des balises B ou I et ignorer ceux qui ont des balises «O».

Les mots-clés de sortie doivent ressembler à:

Traitement du langage naturel , CS , Apprentissage automatique , apprentissage en profondeur

J'ai codé comme suit:

[[('Natural', 'JJ', 'B'), ('language', 'NN', 'I'), ('processing', 'NN', 'I'), ('is', 'VBZ', 'O'), ('one', 'CD', 'O'), ('of', 'IN', 'O'), ('the', 'DT', 'O'), ('important', 'JJ', 'O'), ('branch', 'NN', 'O'), ('of', 'IN', 'O'), ('CS', 'NNP', 'B'), ('.', '.', 'I')] ... ...]]

Je ne sais pas quoi faire ensuite. Quelqu'un pourrait-il s'il vous plaît m'aider ?.

Merci


0 commentaires

3 Réponses :


1
votes

J'espère que cela vous aidera.

remove_o = list(filter(lambda x: x[2] in ['I', 'B'], data))
words = [item[0] for item in remove_o]
reuslt = ' '.join(words)


0 commentaires

1
votes

Vous devriez utiliser itertools.groupby pour une solution assez compacte:

import itertools
import string

data = [[('Natural', 'JJ', 'B'), ('language', 'NN', 'I'), ('processing', 'NN', 'I'), ('is', 'VBZ', 'O'), ('one', 'CD', 'O'), ('of', 'IN', 'O'), ('the', 'DT', 'O'), ('important', 'JJ', 'O'), ('branch', 'NN', 'O'), ('of', 'IN', 'O'), ('CS', 'NNP', 'B'), ('.', '.', 'I')],
[('Machine', 'NN', 'B'), ('learning', 'NN', 'I'), (',', ',', 'I'), ('deep', 'JJ', 'I'), ('learning', 'NN', 'I'), ('are', 'VBP', 'O'), ('heavily', 'RB', 'O'), ('used', 'VBN', 'O'), ('in', 'IN', 'O'), ('natural', 'JJ', 'B'), ('language', 'NN', 'I'), ('processing', 'NN', 'I'), ('.', '.', 'I')],
[('It', 'PRP', 'O'), ('is', 'VBZ', 'O'), ('too', 'RB', 'O'), ('cool', 'JJ', 'O'), ('.', '.', 'O')]]

punctuation = set(string.punctuation)
keywords = [[' '.join(w[0] for w in g) for k, g in itertools.groupby(sen, key=lambda x: x[0] not in punctuation and x[2] != 'O') if k] for sen in data]

print(keywords)
# [['Natural language processing', 'CS'],
#  ['Machine learning', 'deep learning', 'natural language processing'],
#  []]


0 commentaires

0
votes

Vous devez obtenir les premières valeurs du tuple partout où «O» n'est pas présent comme troisième élément, n'est-ce pas? Vous pouvez le faire de cette façon.

for i in data:
    for j in i:
        if(j[2]!='O'): # if(j[2] in ['I','B']) also works
            print(j[0]) # Or append to the output list

Le code ci-dessus est le même que

output = [j[0] for i in data for j in i if(j[2]!='O')]


0 commentaires