python
1① 2word2id = collections.defaultdict(lambda: len(word2id) ) 3sentence = rl 4 5def convert_word(sentence): 6 return [word2id[word.lower()] for word in sentence.split()] 7 8print(sentence) 9print(*convert_word(sentence) ) 10print(dict(word2id) )
を
python
1② 2bindata = open("test.txt").read() 3text = bindata 4 5t = Tokenizer(mmap=True) 6results = [] 7 8lines = text.split("\r\n") 9for line in tqdm(lines): 10 s = line 11 s = s.replace("|", "") 12 s = re.sub(r"《#.+?》", "", s) 13 s = re.sub(r"[#.+?]", "", s) 14 tokens = t.tokenize(s) 15 # 必要な語句だけを対象とする - 5 16 r = [] 17 for tok in tqdm(tokens): 18 if tok.base_form == "*": 19 w = tok.surface 20 else: 21 w = tok.base_form 22 ps = tok.part_of_speech 23 hinsi = ps.split(",")[0] 24 if hinsi in ["名詞", "形容詞", "副詞", "動詞", "記号"]: 25 r.append(w) 26 rl = (" ".join(r)).strip() 27 results.append(rl) 28 print(rl)
の中に組み込んで
python
1[アンパンマン 、 側近 バノン 氏 解任 政権 屋台骨 揺らぐ] 2[0, 1, 2, 3, 4, 5, 6, 7, 8] 3[ドナルド・トランプ 、 側近 バノン 氏 解任 政権 屋台骨 揺らぐ] 4[9, 1, 2, 3, 4, 5, 6, 7, 8] 5 6{'アンパンマン': 0, '、': 1, '側近': 2, 'バノン': 3, '氏': 4, '解任': 5, '政権': 6, '屋台骨': 7, '揺らぐ': 8, 'ドナルド・トランプ': 9}
のように、
1文
1ベクトル
1文
1ベクトル
ラベルの説明
で出力したいのですが、別々で実行するとどうしても
python
1アンパンマン 、 側近 バノン 氏 解任 政権 屋台骨 揺らぐ 2 ドナルド・トランプ 、 側近 バノン 氏 解任 政権 屋台骨 揺らぐ 30 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 4{'アンパンマン': 0, '、': 1, '側近': 2, 'バノン': 3, '氏': 4, '解任': 5, '政権': 6, '屋台骨': 7, '揺らぐ': 8, 'ドナルド・トランプ': 9}
のように1文の後にベクトルが来ず、さらにベクトルが1行に連なって出力されてしまいます。
求める出力
python
1[アンパンマン 、 側近 バノン 氏 解任 政権 屋台骨 揺らぐ] 2[0, 1, 2, 3, 4, 5, 6, 7, 8] 3[ドナルド・トランプ 、 側近 バノン 氏 解任 政権 屋台骨 揺らぐ] 4[9, 1, 2, 3, 4, 5, 6, 7, 8] 5 6{'アンパンマン': 0, '、': 1, '側近': 2, 'バノン': 3, '氏': 4, '解任': 5, '政権': 6, '屋台骨': 7, '揺らぐ': 8, 'ドナルド・トランプ': 9}
にするにはどのように②の中に①の何をどのようにを挿入すれば良いのでしょうか...、どなたかご教授お願い致します。
回答1件
あなたの回答
tips
プレビュー