2016-03-01 14 views
14

Per il pre-elaborazione del corpus stavo pianificando di estradare le frasi comuni dal corpus, per questo ho provato ad usare il modello Frasi in gensim, ho provato sotto il codice ma non mi sta dando l'output desiderato.Come estrarre le frasi da corpus usando gensim

Il mio codice

from gensim.models import Phrases 
documents = ["the mayor of new york was there", "machine learning can be useful sometimes"] 

sentence_stream = [doc.split(" ") for doc in documents] 
bigram = Phrases(sentence_stream) 
sent = [u'the', u'mayor', u'of', u'new', u'york', u'was', u'there'] 
print(bigram[sent]) 

uscita

[u'the', u'mayor', u'of', u'new', u'york', u'was', u'there'] 

Ma dovrebbe venire come

[u'the', u'mayor', u'of', u'new_york', u'was', u'there'] 

ma quando ho provato t o stampare il vocab dei dati del treno, posso vedere il bigram, ma non funziona con i dati del test, dove sto andando male?

print bigram.vocab 

defaultdict(<type 'int'>, {'useful': 1, 'was_there': 1, 'learning_can': 1, 'learning': 1, 'of_new': 1, 'can_be': 1, 'mayor': 1, 'there': 1, 'machine': 1, 'new': 1, 'was': 1, 'useful_sometimes': 1, 'be': 1, 'mayor_of': 1, 'york_was': 1, 'york': 1, 'machine_learning': 1, 'the_mayor': 1, 'new_york': 1, 'of': 1, 'sometimes': 1, 'can': 1, 'be_useful': 1, 'the': 1}) 

risposta

17

ho avuto la soluzione per il problema, c'erano due parametri non ho preso cura di lui, che dovrebbe essere passato a frasi() modello, quelli sono

  1. min_count ignora tutte le parole e i bigram con il conteggio totale raccolto inferiore a questo. Bydefault valore che è 5

  2. soglia rappresenta una soglia per formare le frasi (mezzi superiori meno frasi). Una frase di parole a e b è accettata se (cnt (a, b) - min_count) * N/(cnt (a) * cnt (b))> soglia, dove N è la dimensione totale del vocabolario. Bydefault che valore è 10,0

Con i miei dati dei treni di cui sopra con due affermazioni, il valore soglia è stata , così ho cambiare set di dati del treno e aggiungere questi due parametri.

Il mio nuovo codice

from gensim.models import Phrases 
documents = ["the mayor of new york was there", "machine learning can be useful sometimes","new york mayor was present"] 

sentence_stream = [doc.split(" ") for doc in documents] 
bigram = Phrases(sentence_stream, min_count=1, threshold=2) 
sent = [u'the', u'mayor', u'of', u'new', u'york', u'was', u'there'] 
print(bigram[sent]) 

uscita

[u'the', u'mayor', u'of', u'new_york', u'was', u'there'] 

Gensim è davvero impressionante :)

+1

Che voi per la risposta prezioso. Ma in questo esempio il bigram non cattura "machine", "learning" come "machine_learning". Sai perché succede? –

+1

Se aggiungete "machine learning" nella frase prima dell'allenamento due volte, quindi aggiungetelo nella variabile inviata, otterrete "machine_learning". Se non può vedere una frequenza di quella coppia, allora non lo saprà intuitivamente. – ethanenglish

Problemi correlati