質問編集履歴

2

文の修正

2023/01/17 07:50

投稿

haruchin
haruchin

スコア0

test CHANGED
File without changes
test CHANGED
@@ -3,31 +3,24 @@
3
3
  知恵をお貸しください。
4
4
 
5
5
  「単語の出現頻度を調べるプログラム」
6
- # 全てのテキストを巡回して単語データベースを作成する
6
+
7
7
  import os, glob
8
8
  import MeCab
9
9
  import numpy as np
10
10
  import pickle
11
11
 
12
- # 保存ファイル名
13
12
  savefile = "./ok-spam.pickle"
14
- # MeCabの準備 --- (*1)
15
13
  tagger = MeCab.Tagger()
16
- # 変数の準備 --- (*2)
17
- word_dic = {"__id": 0} # 単語辞書
14
+ word_dic = {"__id": 0}
18
- files = [] # 読み込んだ単語データを追加する
15
+ files = []
19
16
 
20
- # 指定したディレクトリ内のファイル一覧を読む --- (*3)
21
17
  def read_files(dir, label):
22
- # テキストファイルの一覧を得る
23
18
  files = glob.glob(dir + '/*.txt')
24
19
  for f in files:
25
20
  read_file(f, label)
26
21
 
27
- # ファイルを読む --- (*4)
28
22
  def read_file(filename, label):
29
23
  words = []
30
- # ファイルの内容を読む
31
24
  with open(filename, "rt", encoding="utf-8") as f:
32
25
  text = f.read()
33
26
  files.append({
@@ -35,41 +28,31 @@
35
28
  "words": text_to_ids(text)
36
29
  })
37
30
 
38
- # テキストを単語IDのリストに変換
39
31
  def text_to_ids(text):
40
- # 形態素解析 --- (*5)
41
32
  word_s = tagger.parse(text)
42
33
  words = []
43
- # 単語を辞書に登録 --- (*6)
44
34
  for line in word_s.split("\n"):
45
35
  if line == 'EOS' or line == '': continue
46
36
  word = line.split("\t")[0]
47
37
  params = line.split("\t")[4].split("-")
48
- hinsi = params[0] # 品詞
38
+ hinsi = params[0]
49
- hinsi2 = params[1] if len(params) > 1 else '' # 品詞の説明
39
+ hinsi2 = params[1] if len(params) > 1 else ''
50
- org = line.split("\t")[3] # 単語の原型
40
+ org = line.split("\t")[3]
51
- # 助詞・助動詞・記号・数字は捨てる --- (*7)
52
41
  if not (hinsi in ['名詞', '動詞', '形容詞']): continue
53
42
  if hinsi == '名詞' and hinsi2 == '数詞': continue
54
- # 単語をidに変換 --- (*8)
55
43
  id = word_to_id(org)
56
44
  words.append(id)
57
45
  return words
58
46
 
59
- # 単語をidに変換 --- (*9)
60
47
  def word_to_id(word):
61
- # 単語が辞書に登録されているか?
62
48
  if not (word in word_dic):
63
- # 登録されていないので新たにIDを割り振る
64
49
  id = word_dic["__id"]
65
50
  word_dic["__id"] += 1
66
51
  word_dic[word] = id
67
52
  else:
68
- # 既存の単語IDを返す
69
53
  id = word_dic[word]
70
54
  return id
71
55
 
72
- # 単語の頻出頻度のデータを作る --- (*10)
73
56
  def make_freq_data_allfiles():
74
57
  y = []
75
58
  x = []
@@ -79,21 +62,17 @@
79
62
  return y, x
80
63
 
81
64
  def make_freq_data(words):
82
- # 単語の出現回数を調べる
83
65
  cnt = 0
84
66
  dat = np.zeros(word_dic["__id"], 'float')
85
67
  for w in words:
86
68
  dat[w] += 1
87
69
  cnt += 1
88
- # 回数を出現頻度に直す --- (*11)
89
70
  dat = dat / cnt
90
71
  return dat
91
72
 
92
- # ファイルの一覧から学習用のデータベースを作る
93
73
  if __name__ == "__main__":
94
74
  read_files("ok", 0)
95
75
  read_files("spam", 1)
96
76
  y, x = make_freq_data_allfiles()
97
- # ファイルにデータを保存
98
77
  pickle.dump([y, x, word_dic], open(savefile, 'wb'))
99
78
  print("単語頻出データ作成完了")

1

文の修正

2023/01/17 07:48

投稿

haruchin
haruchin

スコア0

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,99 @@
1
- pythonで、メール文から句読点抜き出して合計し、
1
+ pythonで単語の出現頻度調べるプログラムを作成ました。形態素解析を使っいます。
2
- いくつかのメール同様に行い出現する句読点の平均求めような
2
+ これ応用して、句読点とそ前の品詞ペアにした出現頻度を調べプログラムを作成したいと思っています。
3
- プログラムを作りたいです。知恵をお貸しさい。
3
+ 知恵をお貸しください。
4
4
 
5
+ 「単語の出現頻度を調べるプログラム」
6
+ # 全てのテキストを巡回して単語データベースを作成する
7
+ import os, glob
8
+ import MeCab
9
+ import numpy as np
10
+ import pickle
11
+
12
+ # 保存ファイル名
13
+ savefile = "./ok-spam.pickle"
14
+ # MeCabの準備 --- (*1)
15
+ tagger = MeCab.Tagger()
16
+ # 変数の準備 --- (*2)
17
+ word_dic = {"__id": 0} # 単語辞書
18
+ files = [] # 読み込んだ単語データを追加する
19
+
20
+ # 指定したディレクトリ内のファイル一覧を読む --- (*3)
21
+ def read_files(dir, label):
22
+ # テキストファイルの一覧を得る
23
+ files = glob.glob(dir + '/*.txt')
24
+ for f in files:
25
+ read_file(f, label)
26
+
27
+ # ファイルを読む --- (*4)
28
+ def read_file(filename, label):
29
+ words = []
30
+ # ファイルの内容を読む
31
+ with open(filename, "rt", encoding="utf-8") as f:
32
+ text = f.read()
33
+ files.append({
34
+ "label": label,
35
+ "words": text_to_ids(text)
36
+ })
37
+
38
+ # テキストを単語IDのリストに変換
39
+ def text_to_ids(text):
40
+ # 形態素解析 --- (*5)
41
+ word_s = tagger.parse(text)
42
+ words = []
43
+ # 単語を辞書に登録 --- (*6)
44
+ for line in word_s.split("\n"):
45
+ if line == 'EOS' or line == '': continue
46
+ word = line.split("\t")[0]
47
+ params = line.split("\t")[4].split("-")
48
+ hinsi = params[0] # 品詞
49
+ hinsi2 = params[1] if len(params) > 1 else '' # 品詞の説明
50
+ org = line.split("\t")[3] # 単語の原型
51
+ # 助詞・助動詞・記号・数字は捨てる --- (*7)
52
+ if not (hinsi in ['名詞', '動詞', '形容詞']): continue
53
+ if hinsi == '名詞' and hinsi2 == '数詞': continue
54
+ # 単語をidに変換 --- (*8)
55
+ id = word_to_id(org)
56
+ words.append(id)
57
+ return words
58
+
59
+ # 単語をidに変換 --- (*9)
60
+ def word_to_id(word):
61
+ # 単語が辞書に登録されているか?
62
+ if not (word in word_dic):
63
+ # 登録されていないので新たにIDを割り振る
64
+ id = word_dic["__id"]
65
+ word_dic["__id"] += 1
66
+ word_dic[word] = id
67
+ else:
68
+ # 既存の単語IDを返す
69
+ id = word_dic[word]
70
+ return id
71
+
72
+ # 単語の頻出頻度のデータを作る --- (*10)
73
+ def make_freq_data_allfiles():
74
+ y = []
75
+ x = []
76
+ for f in files:
77
+ y.append(f['label'])
78
+ x.append(make_freq_data(f['words']))
79
+ return y, x
80
+
81
+ def make_freq_data(words):
82
+ # 単語の出現回数を調べる
83
+ cnt = 0
84
+ dat = np.zeros(word_dic["__id"], 'float')
85
+ for w in words:
86
+ dat[w] += 1
87
+ cnt += 1
88
+ # 回数を出現頻度に直す --- (*11)
89
+ dat = dat / cnt
90
+ return dat
91
+
92
+ # ファイルの一覧から学習用のデータベースを作る
93
+ if __name__ == "__main__":
94
+ read_files("ok", 0)
95
+ read_files("spam", 1)
96
+ y, x = make_freq_data_allfiles()
97
+ # ファイルにデータを保存
98
+ pickle.dump([y, x, word_dic], open(savefile, 'wb'))
99
+ print("単語頻出データ作成完了")