teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

質問文の変更,エラーメッセージの追加

2019/10/16 08:04

投稿

farinelli
farinelli

スコア61

title CHANGED
File without changes
body CHANGED
@@ -1,9 +1,7 @@
1
- テキストファイルから特定の文字列を除去した後,形態素解析によりストップワードを含まない2文字以上の名詞を抽出,表示させたいと思っています.
1
+ テキストファイルから特定の文字列を除去した後,Mecab-python3を用いた形態素解析によりストップワードを含まない2文字以上の名詞を抽出,表示させたいと思っています.
2
- しかし,最終行で条件に合う名詞を出力しようとした際,以下の写真のような結果になりました.
2
+ しかし,名詞のセレクションの段階で以下のエラーが出てきました.("#該当箇所"の行です)
3
- ![イメージ説明](0b3707c27c658fc36ae04b3e9c6ab71f.png)
4
- 又,”#該当箇所”と記した行の箇所で文章中の名詞を出力させた結果,本来のテキストの一部が1文字ずつに分割されて出力された事は確認済みです.
5
3
 
6
- 今回このような結果になった事を受けて,何か不適切な書き方をしてしまっているのか,あるいは不足している情報があるのか自分の知識では判断できない状況です.
4
+ 従来natto-pyを使ていときでも同様の現象が飽きおり,何か不適切な書き方をしてしまっているのか,あるいは不足している情報があるのか自分の知識では判断できない状況です.
7
5
  この件に関する原因と解決策がご存知の方に是非ご教示頂きたいです.
8
6
  宜しくお願いします.
9
7
 
@@ -20,6 +18,14 @@
20
18
  ```
21
19
  [[“ゲノム”], [“配列”], [“決定”], [“容易”], [“結果”], [“多く”], [“新規”], [“遺伝子”],,,]#途中省略
22
20
  ```
21
+ ### エラー
22
+ ```test.txt
23
+ Traceback (most recent call last):
24
+ File "renshu2.py", line 64, in <module>
25
+ if w.feature.split(",")[0] == "名詞":
26
+ AttributeError: 'str' object has no attribute 'feature'
27
+ ```
28
+
23
29
  ### コード
24
30
  ```renshu.py
25
31
  import os
@@ -28,7 +34,7 @@
28
34
  from collections import Counter
29
35
  from collections import defaultdict
30
36
  import re
31
- from natto import MeCab
37
+ import MeCab
32
38
  import codecs
33
39
  import sys
34
40
  from sklearn.feature_extraction.text import TfidfVectorizer
@@ -38,17 +44,18 @@
38
44
  from gensim import corpora
39
45
  from itertools import chain
40
46
 
47
+ mecab = MeCab.Tagger('-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd')
48
+
41
49
  #日本語ツイートを読み込む + 文書数を表示
42
50
  count = 0
43
- with codecs.open("test.txt", "r", "utf-8") as f:
51
+ with codecs.open("protein.txt", "r", "utf-8") as f:
44
52
  corpus = f.read()
45
53
  #print(corpus)
46
54
 
47
55
  #テキストデータの行数を取得する
48
- num_lines = sum(1 for line in open("test.txt"))
56
+ num_lines = sum(1 for line in open("protein.txt"))
57
+ #print(num_lines)
49
58
 
50
- mecab = MeCab('-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd')
51
-
52
59
  #名詞の抽出と整形
53
60
  rm_list = ["RT","https","co"]
54
61
 
@@ -57,7 +64,6 @@
57
64
  path = 'stop_words.txt'
58
65
  with open(path) as g:
59
66
  stop_words = g.readlines()
60
-
61
67
  #url, 返信, RT, 絵文字の除去
62
68
  corpus = re.sub(r"http\S+", "", corpus)
63
69
  corpus = re.sub(r"@(\w+) ", "", corpus)
@@ -69,20 +75,22 @@
69
75
  u"\U0001F1E0-\U0001F1FF"
70
76
  "]+", flags=re.UNICODE)
71
77
  corpus = emoji_pattern.sub("", corpus)
72
- texts = str(corpus)
78
+ #texts = str(corpus)
73
- #print(texts)
79
+ print(corpus)
80
+ texts = mecab.parse(corpus)
74
81
 
75
82
  #名詞の抽出とスクリーニング
76
83
  docs = []
77
84
  for txt in texts:
78
- words = mecab.parse(txt, as_nodes=True)
85
+ words = mecab.parse(txt)
79
86
  doc = []
80
87
 
81
- for w in words:
88
+ for w in words:
82
- if w.feature.split(",")[0] == "名詞": #該当箇所
89
+ if w.feature.split(",")[0] == "名詞":#該当箇所
83
90
  if len(w.surface) >= 2:
84
91
  if w.surface not in rm_list:
85
92
  doc.append(w.surface)
93
+ #print(doc)
86
94
  docs.append(doc)
87
95
  #本来の意味とは別だがcorpusに単語群を格納
88
96
  corpus = docs