回答編集履歴

1

条件の付与について、追記します。

2019/08/15 05:16

投稿

m3yrin
m3yrin

スコア132

test CHANGED
@@ -33,3 +33,61 @@
33
33
  corpus = docs
34
34
 
35
35
  ```
36
+
37
+
38
+
39
+ ### 追記
40
+
41
+
42
+
43
+ > あとすみません、重ねて恐縮なんですが、本来ならfor w in words以降で名詞のみを選んで並べ替えをしたいのですが、このコードだと名詞を表示させることしかできておらず、tfidfのスコアを出す上で必要のない他の品詞も混ざってしまっているように見受けられます。
44
+
45
+ この場合、どのように書き直せば宜しいでしょうか?
46
+
47
+
48
+
49
+
50
+
51
+ ご質問のコードに含まれていた条件分岐のloopを再利用して、次のようにかけるかと思います。
52
+
53
+ 条件はif文を追加・削除して調整してみてください。
54
+
55
+
56
+
57
+ ```python
58
+
59
+ # corpus = [mecab.parse(txt, as_nodes=True) for line in corpus]
60
+
61
+
62
+
63
+ rm_list = ["RT","https","co"]
64
+
65
+ docs = []
66
+
67
+ for txt in corpus:
68
+
69
+ words = mecab.parse(txt, as_nodes=True)
70
+
71
+ doc = []
72
+
73
+
74
+
75
+ for w in words:
76
+
77
+ if w.feature.split(",")[0] == "名詞":
78
+
79
+ if len(w.surface) >= 2:
80
+
81
+ if not any(rm in w.surface for rm in rm_list):
82
+
83
+ doc.append(str(w.surface))
84
+
85
+
86
+
87
+ doc = ' '.join(doc)
88
+
89
+ docs.append(doc)
90
+
91
+ corpus = docs
92
+
93
+ ```