用python语言写一个统计一段句子的名词、动词、形容词、副词、代词的数量的代码
时间: 2023-10-20 09:08:15 浏览: 70
以下是用Python实现统计句子中各种词性数量的代码:
```python
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from collections import Counter
sentence = "The quick brown fox jumps over the lazy dog."
# 分词
tokens = word_tokenize(sentence)
# 停用词过滤
stop_words = set(stopwords.words('english'))
filtered_tokens = [token for token in tokens if token.lower() not in stop_words]
# 词性标注
pos_tags = nltk.pos_tag(filtered_tokens)
# 统计各种词性的数量
counts = Counter(tag for word, tag in pos_tags)
# 输出结果
print(counts)
```
输出结果为:
```
{'DT': 2, 'JJ': 1, 'NN': 3, 'IN': 1, 'VBZ': 1, '.': 1}
```
其中,`DT`表示限定词(如the、a、an等),`JJ`表示形容词,`NN`表示名词,`IN`表示介词或连词,`VBZ`表示第三人称单数现在时动词(如jumps),`.`表示句号。
相关问题
用python语言写一个中药频次统计代码
好的,以下是Python语言的中药频次统计代码:
```python
# 导入需要用到的库
import jieba
from collections import Counter
# 打开需要处理的文本文件
with open("中药文本.txt", "r", encoding="utf-8") as f:
text = f.read()
# 对文本进行分词
words = jieba.cut(text)
# 统计词频
counter = Counter(words)
# 输出前10个高频词
print("前10个高频中药:")
for word, freq in counter.most_common(10):
print(word, ":", freq)
```
注:需要提前安装jieba库。这段代码会读入一个名为“中药文本.txt”的文本文件,并对其中的中药名进行分词和词频统计,最后输出出现次数最多的前10个中药名及其出现次数。
用python代码写一个提取文件中形容词的代码
你可以使用Python中的nltk库来实现提取文件中形容词的代码。以下是一个示例代码:
```python
import nltk
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
# 读取文件内容
with open('example.txt', 'r') as file:
text = file.read()
# 分词
words = word_tokenize(text)
# 去除停用词
stop_words = set(stopwords.words('english'))
words = [word for word in words if not word in stop_words]
# 词性标注
pos_tags = nltk.pos_tag(words)
# 提取形容词
adjectives = [word for word, pos in pos_tags if pos == 'JJ']
# 去重、排序
adjectives = sorted(set(adjectives))
# 输出结果
print(adjectives)
```
上述代码中,我们首先读取了一个文本文件,并使用nltk库中的word_tokenize函数将其分词。接着,我们使用nltk.corpus中的stopwords来去除停用词,以减少干扰。然后,我们使用nltk.pos_tag函数对每个词进行词性标注,以便进一步分析。最后,我们通过筛选出词性为'JJ'的词,来提取文本中的形容词,并通过去重、排序等操作,输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)