2009-02-08 12 views

risposta

26

Ecco il mio esempio pratico a beneficio di chiunque altro alla ricerca questa domanda fino (scusa il testo di esempio, è stata la prima cosa che ho trovato su Wikipedia):

import nltk 
import pprint 

tokenizer = None 
tagger = None 

def init_nltk(): 
    global tokenizer 
    global tagger 
    tokenizer = nltk.tokenize.RegexpTokenizer(r'\w+|[^\w\s]+') 
    tagger = nltk.UnigramTagger(nltk.corpus.brown.tagged_sents()) 

def tag(text): 
    global tokenizer 
    global tagger 
    if not tokenizer: 
     init_nltk() 
    tokenized = tokenizer.tokenize(text) 
    tagged = tagger.tag(tokenized) 
    tagged.sort(lambda x,y:cmp(x[1],y[1])) 
    return tagged 

def main(): 
    text = """Mr Blobby is a fictional character who featured on Noel 
    Edmonds' Saturday night entertainment show Noel's House Party, 
    which was often a ratings winner in the 1990s. Mr Blobby also 
    appeared on the Jamie Rose show of 1997. He was designed as an 
    outrageously over the top parody of a one-dimensional, mute novelty 
    character, which ironically made him distinctive, absurd and popular. 
    He was a large pink humanoid, covered with yellow spots, sporting a 
    permanent toothy grin and jiggling eyes. He communicated by saying 
    the word "blobby" in an electronically-altered voice, expressing 
    his moods through tone of voice and repetition. 

    There was a Mrs. Blobby, seen briefly in the video, and sold as a 
    doll. 

    However Mr Blobby actually started out as part of the 'Gotcha' 
    feature during the show's second series (originally called 'Gotcha 
    Oscars' until the threat of legal action from the Academy of Motion 
    Picture Arts and Sciences[citation needed]), in which celebrities 
    were caught out in a Candid Camera style prank. Celebrities such as 
    dancer Wayne Sleep and rugby union player Will Carling would be 
    enticed to take part in a fictitious children's programme based around 
    their profession. Mr Blobby would clumsily take part in the activity, 
    knocking over the set, causing mayhem and saying "blobby blobby 
    blobby", until finally when the prank was revealed, the Blobby 
    costume would be opened - revealing Noel inside. This was all the more 
    surprising for the "victim" as during rehearsals Blobby would be 
    played by an actor wearing only the arms and legs of the costume and 
    speaking in a normal manner.[citation needed]""" 
    tagged = tag(text)  
    l = list(set(tagged)) 
    l.sort(lambda x,y:cmp(x[1],y[1])) 
    pprint.pprint(l) 

if __name__ == '__main__': 
    main() 

uscita:

[('rugby', None), 
('Oscars', None), 
('1990s', None), 
('",', None), 
('Candid', None), 
('"', None), 
('blobby', None), 
('Edmonds', None), 
('Mr', None), 
('outrageously', None), 
('.[', None), 
('toothy', None), 
('Celebrities', None), 
('Gotcha', None), 
(']),', None), 
('Jamie', None), 
('humanoid', None), 
('Blobby', None), 
('Carling', None), 
('enticed', None), 
('programme', None), 
('1997', None), 
('s', None), 
("'", "'"), 
('[', '('), 
('(', '('), 
(']', ')'), 
(',', ','), 
('.', '.'), 
('all', 'ABN'), 
('the', 'AT'), 
('an', 'AT'), 
('a', 'AT'), 
('be', 'BE'), 
('were', 'BED'), 
('was', 'BEDZ'), 
('is', 'BEZ'), 
('and', 'CC'), 
('one', 'CD'), 
('until', 'CS'), 
('as', 'CS'), 
('This', 'DT'), 
('There', 'EX'), 
('of', 'IN'), 
('inside', 'IN'), 
('from', 'IN'), 
('around', 'IN'), 
('with', 'IN'), 
('through', 'IN'), 
('-', 'IN'), 
('on', 'IN'), 
('in', 'IN'), 
('by', 'IN'), 
('during', 'IN'), 
('over', 'IN'), 
('for', 'IN'), 
('distinctive', 'JJ'), 
('permanent', 'JJ'), 
('mute', 'JJ'), 
('popular', 'JJ'), 
('such', 'JJ'), 
('fictional', 'JJ'), 
('yellow', 'JJ'), 
('pink', 'JJ'), 
('fictitious', 'JJ'), 
('normal', 'JJ'), 
('dimensional', 'JJ'), 
('legal', 'JJ'), 
('large', 'JJ'), 
('surprising', 'JJ'), 
('absurd', 'JJ'), 
('Will', 'MD'), 
('would', 'MD'), 
('style', 'NN'), 
('threat', 'NN'), 
('novelty', 'NN'), 
('union', 'NN'), 
('prank', 'NN'), 
('winner', 'NN'), 
('parody', 'NN'), 
('player', 'NN'), 
('actor', 'NN'), 
('character', 'NN'), 
('victim', 'NN'), 
('costume', 'NN'), 
('action', 'NN'), 
('activity', 'NN'), 
('dancer', 'NN'), 
('grin', 'NN'), 
('doll', 'NN'), 
('top', 'NN'), 
('mayhem', 'NN'), 
('citation', 'NN'), 
('part', 'NN'), 
('repetition', 'NN'), 
('manner', 'NN'), 
('tone', 'NN'), 
('Picture', 'NN'), 
('entertainment', 'NN'), 
('night', 'NN'), 
('series', 'NN'), 
('voice', 'NN'), 
('Mrs', 'NN'), 
('video', 'NN'), 
('Motion', 'NN'), 
('profession', 'NN'), 
('feature', 'NN'), 
('word', 'NN'), 
('Academy', 'NN-TL'), 
('Camera', 'NN-TL'), 
('Party', 'NN-TL'), 
('House', 'NN-TL'), 
('eyes', 'NNS'), 
('spots', 'NNS'), 
('rehearsals', 'NNS'), 
('ratings', 'NNS'), 
('arms', 'NNS'), 
('celebrities', 'NNS'), 
('children', 'NNS'), 
('moods', 'NNS'), 
('legs', 'NNS'), 
('Sciences', 'NNS-TL'), 
('Arts', 'NNS-TL'), 
('Wayne', 'NP'), 
('Rose', 'NP'), 
('Noel', 'NP'), 
('Saturday', 'NR'), 
('second', 'OD'), 
('his', 'PP$'), 
('their', 'PP$'), 
('him', 'PPO'), 
('He', 'PPS'), 
('more', 'QL'), 
('However', 'RB'), 
('actually', 'RB'), 
('also', 'RB'), 
('clumsily', 'RB'), 
('originally', 'RB'), 
('only', 'RB'), 
('often', 'RB'), 
('ironically', 'RB'), 
('briefly', 'RB'), 
('finally', 'RB'), 
('electronically', 'RB-HL'), 
('out', 'RP'), 
('to', 'TO'), 
('show', 'VB'), 
('Sleep', 'VB'), 
('take', 'VB'), 
('opened', 'VBD'), 
('played', 'VBD'), 
('caught', 'VBD'), 
('appeared', 'VBD'), 
('revealed', 'VBD'), 
('started', 'VBD'), 
('saying', 'VBG'), 
('causing', 'VBG'), 
('expressing', 'VBG'), 
('knocking', 'VBG'), 
('wearing', 'VBG'), 
('speaking', 'VBG'), 
('sporting', 'VBG'), 
('revealing', 'VBG'), 
('jiggling', 'VBG'), 
('sold', 'VBN'), 
('called', 'VBN'), 
('made', 'VBN'), 
('altered', 'VBN'), 
('based', 'VBN'), 
('designed', 'VBN'), 
('covered', 'VBN'), 
('communicated', 'VBN'), 
('needed', 'VBN'), 
('seen', 'VBN'), 
('set', 'VBN'), 
('featured', 'VBN'), 
('which', 'WDT'), 
('who', 'WPS'), 
('when', 'WRB')] 
+5

Cosa fa? Puoi aggiungere qualche descrizione? e anche perché usare global, potresti averli usati direttamente a destra – avi

+1

@avi Produce tag Part of Speech per le parole (scorri verso il basso per vedere l'elenco completo). Es: '('chiamato', 'VBN')' sta dicendo che 'called' è un' verbo participio passato'. Sembra che Global sia stato utilizzato in modo che le variabili possano essere modificate nell'ambito della funzione (in modo che non debbano essere passate ogni volta che viene chiamata la funzione). – emh

+0

upvote 1 per Mr. Blobby – Aphire

13

Sono l'autore di streamhacker.com (e grazie per la menzione, ottengo una buona quantità di traffico click da questa particolare domanda). Cosa stai cercando di fare in particolare? NLTK ha molti strumenti per fare varie cose, ma in qualche modo mancano informazioni chiare su cosa utilizzare per gli strumenti e come usarli al meglio. È anche orientato verso problemi accademici e quindi può essere difficile tradurre gli esempi pedagogical in soluzioni pratiche.

16

La PNL in generale è molto utile quindi potresti voler ampliare la ricerca all'applicazione generale dell'analisi del testo. Ho usato NLTK per aiutare MOSS 2010 generando la tassonomia dei file estraendo mappe concettuali. Ha funzionato davvero bene. Non ci vuole molto prima che i file inizino a creare cluster in modi utili.

Spesso per capire l'analisi del testo devi pensare in modo tangente ai modi in cui sei abituato a pensare. Ad esempio, l'analisi del testo è estremamente utile alla scoperta. La maggior parte delle persone, tuttavia, non sa nemmeno quale sia la differenza tra ricerca e scoperta. Se leggi su questi argomenti, probabilmente "scoprirai" i modi in cui potresti voler far funzionare NLTK.

Inoltre, considerare la visualizzazione mondiale dei file di testo senza NLTK. Hai un mucchio di stringhe di lunghezza casuale separate da spazi bianchi e punteggiatura. Alcuni dei segni di punteggiatura cambiano il modo in cui viene utilizzato come il punto (che è anche un punto decimale e un indicatore postfissa per un'abbreviazione.) Con NLTK ottieni parole e altro fino al punto in cui ottieni parti del discorso. Ora hai una maniglia sul contenuto. Usa NLTK per scoprire i concetti e le azioni nel documento. Usa NLTK per ottenere il "significato" del documento. Il significato in questo caso si riferisce alle relazioni essenziali nel documento.

È una buona cosa essere curiosi di NLTK. L'analisi del testo è destinata a sbloccare in modo significativo nei prossimi anni. Chi lo capirà sarà più adatto a sfruttare meglio le nuove opportunità.

+0

Puoi pubblicare un link al riferimento MOSS 2010? – alvas

+0

Il miglior collegamento che ho è su un foglio che ho scritto qualche anno fa. Quest'anno ricostruirò la mia pagina web per concentrarmi sui miei radiotelescopi di data mining di lavoro, ma per un po 'questo articolo dovrebbe essere ancora aggiornato: http://www.nectarineimp.com/automated-folksonomy-whitepaper/ –