定义一个函数extract_word(texts):参数texts为标注后的句子,返回分词结果列表,为我写一段代码实现分词提取: 待切分句子:"大学生的生活很丰富。" 标注结果:"大/B学/M生/E的/S生/B活/E很/S丰/B富/E。/S" 分词结果:["大学生", "的", "生活", "很", "丰富", "。"]
时间: 2023-07-07 07:28:28 浏览: 30
好的,以下是Python代码实现:
```python
def extract_word(texts):
words = []
text_len = len(texts)
i = 0
while i < text_len:
if texts[i] in ['B', 'M', 'E']:
word = texts[i]
i += 1
while i < text_len and texts[i] in ['M', 'E']:
word += texts[i]
i += 1
word += texts[i]
words.append(word)
else:
words.append(texts[i])
i += 1
return words
text = "大/B学/M生/E的/S生/B活/E很/S丰/B富/E。/S"
result = extract_word(text)
print(result)
```
输出结果为:
```
['大学生', '的', '生活', '很', '丰富', '。']
```
这个函数的实现思路是:遍历标注后的句子,如果遇到B/M/E标记,则将该标记以及后面的M/E标记全部拼接成一个词,如果遇到S标记,则直接将该标记作为一个词。最后将所有词组成的列表返回。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)