python语言单词_python – 在动词/名词/形容词之间转换单词

这更是一种启发式的方法。我已经编码了它的风格。它使用来自wordnet的derivationally_related_forms()。我已经实施了名词我猜动词的作用类似。从我所测试的工作相当不错:

from nltk.corpus import wordnet as wn

def nounify(verb_word):

""" Transform a verb to the closest noun: die -> death """

verb_synsets = wn.synsets(verb_word, pos="v")

# Word not found

if not verb_synsets:

return []

# Get all verb lemmas of the word

verb_lemmas = [l for s in verb_synsets \

for l in s.lemmas if s.name.split('.')[1] == 'v']

# Get related forms

derivationally_related_forms = [(l, l.derivationally_related_forms()) \

for l in verb_lemmas]

# filter only the nouns

related_noun_lemmas = [l for drf in derivationally_related_forms \

for l in drf[1] if l.synset.name.split('.')[1] == 'n']

# Extract the words from the lemmas

words = [l.name for l in related_noun_lemmas]

len_words = len(words)

# Build the result in the form of a list containing tuples (word, probability)

result = [(w, float(words.count(w))/len_words) for w in set(words)]

result.sort(key=lambda w: -w[1])

# return all the possibilities sorted by probability

return result

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,首先需要安装jieba和wordcloud模块,可以通过以下命令安装: ``` pip install jieba pip install wordcloud ``` 接下来,我们假设文章内容已经存储在一个txt文件中,代码如下: ```python import jieba from collections import Counter from wordcloud import WordCloud import matplotlib.pyplot as plt with open('article.txt', 'r', encoding='utf-8') as f: content = f.read() # 使用jieba进行分词 words = jieba.cut(content) # 定义需要统计的词性 word_type = ['n', 'v', 'a'] # 过滤出指定词性的词语 filtered_words = [word for word in words if len(word) >= 2 and word.strip() and word.startswith(tuple(word_type))] # 统计词频 word_count = Counter(filtered_words) # 绘制词云图 wc = WordCloud(font_path='simhei.ttf', background_color='white', max_words=200, width=1200, height=800) wc.generate_from_frequencies(word_count) plt.imshow(wc) plt.axis('off') plt.show() ``` 在上述代码中,我们使用jieba进行中文分词,并通过传入一个包含需要统计的词性的列表来过滤出名词动词形容词。然后使用Counter类来统计每个词语的出现频率。最后通过wordcloud模块来绘制词云图。 需要注意的是,我们在绘制词云图时,需要指定字体文件的路径,否则可能会出现乱码。在本例中,我们使用了宋体字体的一个变体simhei.ttf。 运行完上述代码后,就可以得到一个词云图,它会显示文章中出现频率较高的词语,可以帮助我们更好地了解文章的主题和关键词。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值