我将其记录为备忘,以便偶然发现相同错误的人有更少的时间查找。
以下内容正在Google Colab上运行。
参照此处,在Colab上安装MeCab和拥抱型变压器。
1 2 3 4 | !apt install aptitude swig !aptitude install mecab libmecab-dev mecab-ipadic-utf8 git make curl xz-utils file -y !pip install mecab-python3 !pip install transformers |
尝试使用BERT的标记器编写日语。
1 2 3 4 5 6 7 8 9 10 | from transformers.tokenization_bert_japanese import BertJapaneseTokenizer # 日本語BERT用のtokenizerを宣言 tokenizer = BertJapaneseTokenizer.from_pretrained('cl-tohoku/bert-base-japanese-whole-word-masking') text = "自然言語処理はとても楽しい。" wakati_ids = tokenizer.encode(text, return_tensors='pt') print(tokenizer.convert_ids_to_tokens(wakati_ids[0].tolist())) print(wakati_ids) |
我收到以下错误。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | ---------------------------------------------------------- Failed initializing MeCab. Please see the README for possible solutions: https://github.com/SamuraiT/mecab-python3#common-issues If you are still having trouble, please file an issue here, and include the ERROR DETAILS below: https://github.com/SamuraiT/mecab-python3/issues issueを英語で書く必要はありません。 ------------------- ERROR DETAILS ------------------------ arguments: error message: [ifs] no such file or directory: /usr/local/etc/mecabrc --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-3-f828f6470517> in <module>() 2 3 # 日本語BERT用のtokenizerを宣言 ----> 4 tokenizer = BertJapaneseTokenizer.from_pretrained('cl-tohoku/bert-base-japanese-whole-word-masking') 5 6 text = "自然言語処理はとても楽しい。" 4 frames /usr/local/lib/python3.6/dist-packages/MeCab/__init__.py in __init__(self, rawargs) 122 123 try: --> 124 super(Tagger, self).__init__(args) 125 except RuntimeError: 126 error_info(rawargs) RuntimeError: |
错误输出会告诉我要在这里查看,因此,按照URL
的指示安装mecab-python3时
1 | pip install unidic-lite |
如果还执行
,则不会再通过初始化MeCab删除它。但是,这次我为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-f828f6470517> in <module>() 6 text = "自然言語処理はとても楽しい。" 7 ----> 8 wakati_ids = tokenizer.encode(text, return_tensors='pt') 9 print(tokenizer.convert_ids_to_tokens(wakati_ids[0].tolist())) 10 print(wakati_ids) 8 frames /usr/local/lib/python3.6/dist-packages/transformers/tokenization_bert_japanese.py in tokenize(self, text, never_split, **kwargs) 205 break 206 --> 207 token, _ = line.split("\t") 208 token_start = text.index(token, cursor) 209 token_end = token_start + len(token) ValueError: too many values to unpack (expected 2) |
关于此错误,这是mecab-python3的开发人员吗?如前所述,通过将mecab-python3的版本指定为
总之,在安装pip时,如果您按以下说明进行声明,则不会出现错误。
1 2 3 4 5 | !apt install aptitude swig !aptitude install mecab libmecab-dev mecab-ipadic-utf8 git make curl xz-utils file -y !pip install mecab-python3==0.996.5 !pip install unidic-lite !pip install transformers |
如果在运行
↑之前已经通过pip安装了最新版本的mecab-python3,请不要忘记重新连接colab会话。您可以通过单击colab屏幕右上角的▼(例如RAM或磁盘)来断开会话与会话管理的连接。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from transformers.tokenization_bert_japanese import BertJapaneseTokenizer # 日本語BERT用のtokenizerを宣言 tokenizer = BertJapaneseTokenizer.from_pretrained('cl-tohoku/bert-base-japanese-whole-word-masking') text = "自然言語処理はとても楽しい。" wakati_ids = tokenizer.encode(text, return_tensors='pt') print(tokenizer.convert_ids_to_tokens(wakati_ids[0].tolist())) print(wakati_ids) #Downloading: 100% #258k/258k [00:00<00:00, 1.58MB/s] # #['[CLS]', '自然', '言語', '処理', 'は', 'とても', '楽しい', '。', '[SEP]'] #tensor([[ 2, 1757, 1882, 2762, 9, 8567, 19835, 8, 3]]) |
我能够用
结束