get_eng_dicts(datas):需要装什么包
时间: 2024-02-20 14:02:17 浏览: 25
根据函数名来看,可能需要使用 Python 中的一些英语词典。如果是这样的话,常用的英语词典包括 nltk 和 PyDictionary。因此,你需要安装这些包。你可以在终端中使用以下命令来安装它们:
nltk:
```
pip install nltk
```
PyDictionary:
```
pip install PyDictionary
```
相关问题
def split_dot(words): dots=[',','.','!','?'] str_data = [] for w in words: ##*****——请在下方补充代码——***** ##*****——请在上方补充代码——***** return(str_data) def get_eng_dicts(datas): w_all_dict = {} for sample in datas: for token in sample.split(" "): ##*****——请在下方补充代码——***** ##*****——请在上方补充代码——***** sort_w_list = sorted(w_all_dict.items(), key=lambda d: d[1], reverse=True) w_keys = [x for x,_ in sort_w_list[:7000-2]] w_keys.insert(0,"<PAD>") w_keys.insert(0,"<UNK>") w_dict = { x:i for i,x in enumerate(w_keys) } i_dict = { i:x for i,x in enumerate(w_keys) } return w_dict,i_dict
在第一个函数的注释中,我们可以看到需要将输入的words字符串列表中的句子分割成单词,并将其中的标点符号去除。我们可以使用Python的内置方法split()来分割字符串,并使用字符串的replace()方法来去除标点符号。
下面是修改后的代码:
```python
def split_dot(words):
dots=[',','.','!','?']
str_data = []
for w in words:
w = w.replace(".", "").replace(",", "").replace("!", "").replace("?", "") # 去除标点符号
str_data += w.split(" ") # 分割字符串
return(str_data)
```
在第二个函数的注释中,我们可以看到需要将datas中的所有单词统计出现次数,并根据出现次数从多到少排序,然后选取出现次数最多的前7000个单词作为词典。我们可以使用Python的collections模块中的Counter类来统计单词出现次数,并使用sorted()函数来排序。
下面是修改后的代码:
```python
from collections import Counter
def get_eng_dicts(datas):
w_all_dict = Counter()
for sample in datas:
words = sample.split(" ")
# 统计单词出现次数
w_all_dict.update(words)
# 按照出现次数从多到少排序
sort_w_list = sorted(w_all_dict.items(), key=lambda d: d[1], reverse=True)
# 选取出现次数最多的前7000个单词作为词典
w_keys = [x for x,_ in sort_w_list[:7000-2]]
w_keys.insert(0,"<PAD>")
w_keys.insert(0,"<UNK>")
w_dict = { x:i for i,x in enumerate(w_keys) }
i_dict = { i:x for i,x in enumerate(w_keys) }
return w_dict,i_dict
```
pretrain_dict = load_dict['state_dicts'] KeyError: 'state_dicts'
这个错误是由于在load_dict字典中没有名为'state_dicts'的键所导致的。你需要检查一下load_dict字典是否包含了这个键。如果没有,你需要确认你的模型是否被正确地保存,并且在加载模型时传递了正确的路径和文件名。如果确定模型被正确地保存和加载,但仍然出现这个错误,你可能需要检查一下模型的版本和代码是否匹配。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)