私はユニグラムとバイグラムのカウントをしたいテキストファイルは、このPythonを使用してクラス変数とともにcsvにテキストファイルのユニグラムとバイグラムの数列を作成する方法は?
Text Class
I love the movie Pos
I hate the movie Neg
のように見える2つの列が含まれているPythonの を使用してCSVにクラス変数と一緒にテキストファイルのユニグラムとバイグラムカウントマトリックスを作成したいですテキスト列と出力用のCSVファイルに書き込まれるべき
I hate love movie the class
1 0 1 1 1 Pos
1 1 0 1 1 Neg
バイグラム
I love love the the movie I hate hate the class
1 1 1 0 0 Pos
0 0 1 1 1 Neg
誰も私が上記の出力形式に以下のコードを改善するのを助けることができますか?
>>> import nltk
>>> from collections import Counter
>>> fo = open("text.txt")
>>> fo1 = fo.readlines()
>>> for line in fo1:
bigm = list(nltk.bigrams(line.split()))
bigmC = Counter(bigm)
for key, value in bigmC.items():
print(key, value)
('love', 'the') 1
('the', 'movie') 1
('I', 'love') 1
('I', 'hate') 1
('hate', 'the') 1
('the', 'movie') 1