1
これは私が今使用しているコードです。私が使用するcsvfileは2つの列、1つはテキストで、もう1つは所属する会話の番号です。今では、テキストとは異なるngramを取得することができましたが、会話の数をngramに関連付けることもできます。だからngramがx回現れたら、どの会話に出てくるのか見たいのですが、どうすればいいですか?ngram計算後にデータフレームに余分な列を追加するには
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
df = pd.read_csv("F:/textclustering/data/filteredtext1.csv", encoding="iso-8859-1" ,low_memory=False)
document = df['Data']
vectorizer = CountVectorizer(ngram_range=(2, 2))
X = vectorizer.fit_transform(document)
matrix_terms = np.array(vectorizer.get_feature_names())
matrix_freq = np.asarray(X.sum(axis=0)).ravel()
terms = vectorizer.get_feature_names()
freqs = X.sum(axis=0).A1
dictionary = dict(zip(terms, freqs))
df = pd.DataFrame(dictionary,index=[0]).T.reindex()
df.to_csv("F:/textclustering/data/terms2.csv", sep=',', na_rep="none")
入力CSV
text, id
example text is great, 1
this is great, 2
example text is great, 3
所望の出力(またはこれに近いもの)
ngram, count, id
example text, 2, [1,3]
text is, 2, [1,3]
is great, 3, [1,2,3]
this is, 1, [1]