2017-05-11 3 views
0

gensim을 사용하여 CountVectorizer()에서 n_gram 매개 변수를 모방하려고합니다. 내 목표는 Scikit 또는 Gensim에서 LDA를 사용하고 매우 유사한 바이 그램을 찾을 수있게하는 것입니다.gensim으로 Scikit ngram을 모방하려고 시도했습니다.

예를 들어, 우리가 scikit 다음과 같은 bigrams 찾을 수 있습니다 "설문 조사", "ABC 컴퓨터", "정렬되지 않은 바이너리"와 gensim과를, "마이너는"...

내가 아래에있는 내 코드 첨부 Gensim과 Scikit을 바이 그램/유니 그램의 관점에서 비교합니다. 우리는 48 고유 한 토큰을 찾을 gensim 모델 당신의 도움이

documents = [["Human" ,"machine" ,"interface" ,"for" ,"lab", "abc" ,"computer" ,"applications"], 
     ["A", "survey", "of", "user", "opinion", "of", "computer", "system", "response", "time"], 
     ["The", "EPS", "user", "interface", "management", "system"], 
     ["System", "and", "human", "system", "engineering", "testing", "of", "EPS"], 
     ["Relation", "of", "user", "perceived", "response", "time", "to", "error", "measurement"], 
     ["The", "generation", "of", "random", "binary", "unordered", "trees"], 
     ["The", "intersection", "graph", "of", "paths", "in", "trees"], 
     ["Graph", "minors", "IV", "Widths", "of", "trees", "and", "well", "quasi", "ordering"], 
     ["Graph", "minors", "A", "survey"]] 

에 대한

덕분에, 우리는/인쇄와 bigrams (dictionary.token2id)

# 1. Gensim 
from gensim.models import Phrases 

# Add bigrams and trigrams to docs (only ones that appear 20 times or more). 
bigram = Phrases(documents, min_count=1) 
for idx in range(len(documents)): 
    for token in bigram[documents[idx]]: 
     if '_' in token: 
      # Token is a bigram, add to document. 
      documents[idx].append(token) 

documents = [[doc.replace("_", " ") for doc in docs] for docs in documents] 
print(documents) 

dictionary = corpora.Dictionary(documents) 
print(dictionary.token2id) 

유니 그램을 인쇄하고 scikit와 수 96 고유 한 토큰, 우리는 인쇄와 scikit의 어휘를 인쇄 할 수 있습니다 (Vocab의)

# 2. Scikit 
import re 
token_pattern = re.compile(r"\b\w\w+\b", re.U) 

def custom_tokenizer(s, min_term_length = 1): 
    """ 
    Tokenizer to split text based on any whitespace, keeping only terms of at least a certain length which start with an alphabetic character. 
    """ 
    return [x.lower() for x in token_pattern.findall(s) if (len(x) >= min_term_length and x[0].isalpha()) ] 

from sklearn.feature_extraction.text import CountVectorizer 

def preprocess(docs, min_df = 1, min_term_length = 1, ngram_range = (1,1), tokenizer=custom_tokenizer): 
    """ 
    Preprocess a list containing text documents stored as strings. 
    doc : list de string (pas tokenizé) 
    """ 
    # Build the Vector Space Model, apply TF-IDF and normalize lines to unit length all in one call 
    vec = CountVectorizer(lowercase=True, 
         strip_accents="unicode", 
         tokenizer=tokenizer, 
         min_df = min_df, 
         ngram_range = ngram_range, 
         stop_words = None 
        ) 
    X = vec.fit_transform(docs) 
    vocab = vec.get_feature_names() 

    return (X,vocab) 

docs_join = list() 

for i in documents: 
    docs_join.append(' '.join(i)) 

(X, vocab) = preprocess(docs_join, ngram_range = (1,2)) 

print(vocab) 

답변

1

gensimPhrases 클래스는 "문장의 스트림에서 일반적인 구문 (다중 단어 식)을 자동으로 검색"하도록 설계되었습니다. 그래서 "예상보다 자주 나타나는"바이 그램 만 제공합니다. 그런 이유로 gensim 패키지를 사용하면 다음과 같은 몇 가지 bigrams 만 얻을 수 있습니다 : 'response time', 'Graph minors', 'A survey'.

bigram.vocab을 보면이 bigrams가 2 번 나타나는 반면 다른 모든 bigrams는 한 번만 나타납니다.

scikit-learn의 클래스는 모든 바이 그램을 제공합니다.

관련 문제