Python >> Tutoriel Python >  >> Python Tag >> NLTK

Extraction de tous les noms d'un fichier texte à l'aide de nltk

Si vous êtes ouvert à d'autres options que NLTK , consultez TextBlob . Il extrait facilement tous les noms et phrases nominales :

>>> from textblob import TextBlob
>>> txt = """Natural language processing (NLP) is a field of computer science, artificial intelligence, and computational linguistics concerned with the inter
actions between computers and human (natural) languages."""
>>> blob = TextBlob(txt)
>>> print(blob.noun_phrases)
[u'natural language processing', 'nlp', u'computer science', u'artificial intelligence', u'computational linguistics']

import nltk

lines = 'lines is some string of words'
# function to test if something is a noun
is_noun = lambda pos: pos[:2] == 'NN'
# do the nlp stuff
tokenized = nltk.word_tokenize(lines)
nouns = [word for (word, pos) in nltk.pos_tag(tokenized) if is_noun(pos)] 

print nouns
>>> ['lines', 'string', 'words']

Conseil utile :il arrive souvent que les compréhensions de liste soient une méthode plus rapide pour construire une liste que d'ajouter des éléments à une liste avec la méthode .insert() ou append(), dans une boucle 'for'.


Vous pouvez obtenir de bons résultats en utilisant nltk , Textblob , SpaCy ou l'une des nombreuses autres bibliothèques là-bas. Ces bibliothèques feront toutes le travail mais avec des degrés d'efficacité différents.

import nltk
from textblob import TextBlob
import spacy
nlp = spacy.load('en')
nlp1 = spacy.load('en_core_web_lg')

txt = """Natural language processing (NLP) is a field of computer science, artificial intelligence, and computational linguistics concerned with the interactions between computers and human (natural) languages."""

Sur mon Windows 10 2 cœurs, 4 processeurs, 8 Go de RAM i5 hp ordinateur portable, dans jupyter notebook, j'ai effectué quelques comparaisons et voici les résultats.

Pour TextBlob :

%%time
print([w for (w, pos) in TextBlob(txt).pos_tags if pos[0] == 'N'])

Et le résultat est

>>> ['language', 'processing', 'NLP', 'field', 'computer', 'science', 'intelligence', 'linguistics', 'inter', 'actions', 'computers', 'languages']
    Wall time: 8.01 ms #average over 20 iterations

Pour nltk :

%%time
print([word for (word, pos) in nltk.pos_tag(nltk.word_tokenize(txt)) if pos[0] == 'N'])

Et le résultat est

>>> ['language', 'processing', 'NLP', 'field', 'computer', 'science', 'intelligence', 'linguistics', 'inter', 'actions', 'computers', 'languages']
    Wall time: 7.09 ms #average over 20 iterations

Pour Spacy :

%%time
print([ent.text for ent in nlp(txt) if ent.pos_ == 'NOUN'])

Et le résultat est

>>> ['language', 'processing', 'field', 'computer', 'science', 'intelligence', 'linguistics', 'inter', 'actions', 'computers', 'languages']
    Wall time: 30.19 ms #average over 20 iterations

Il semble nltk et TextBlob sont raisonnablement plus rapides et il faut s'y attendre puisque ne stockez rien d'autre sur le texte d'entrée, txt . Spacy est beaucoup plus lent. Encore une chose. SpaCy manqué le nom NLP tandis que nltk et TextBlob j'ai compris. Je tirerais pour nltk ou TextBlob à moins qu'il y ait autre chose que je souhaite extraire de l'entrée txt .


Découvrez un démarrage rapide dans spacy ici.
Découvrez quelques notions de base sur TextBlob ici.
Découvrez nltk Tutoriels ici