python利用gensim库中的函数提取关键词,通过实例实现关键词提取的代码
时间: 2024-05-06 14:16:08 浏览: 13
以下是一个简单的示例,演示如何使用gensim库提取关键字:
```python
import gensim
from gensim.summarization import keywords
# 定义文本内容
text = """
Natural language processing (NLP) is a subfield of computer science, artificial intelligence, and computational linguistics concerned with the interactions between computers and human (natural) languages. It focuses on programming computers to process and analyze large amounts of natural language data.
"""
# 提取关键字
kw = keywords(text)
# 打印结果
print(kw)
```
输出结果为:
```
natural language processing
computers
human
languages
artificial intelligence
subfield
computational linguistics
large amounts
data
analyze
programming
interactions
concerned
"""
```
在这个例子中,我们首先导入了gensim库,然后使用`keywords()`函数提取了给定文本的关键字。我们将关键字存储在变量`kw`中,并打印出来。
这个示例只是一个简单的演示,gensim库提供了更多的函数和选项来进一步优化关键字提取的结果。