nltk入门函数解释(concordance,similar,common_contexts)

  1. #from nltk.book import *;   #导入NTLK包中book中的所有的例子模型  
  2.   
  3. #print(text1);  #输出book中text1的相关信息  
  4.   
  5. #text1.concordance("monstrous");    #在text1中找到特定单词并显示出上下文  
  6. #print("monstrous in text1 similar to");  
  7. #text1.similar("monstrous");    #在text1找到用法、意义与该单词相似的词  
  8. #print();  
  9.   
  10. #print("monstrous in text2 similar to");  
  11. #text2.similar("monstrous");    #在text2找到用法、意义与该单词相似的词  
  12. #print();  
  13.   
  14. #text2.common_contexts(["monstrous", "very"]);    #在text2找到用法、意义与该单词集合相似的词  
  15. #print();  
  16.   
  17. #print(len(text3));     #显示text3长度  
  18. #print();  
  19.   
  20.   
  21. #print(sorted(set(text3)));     #筛选出text3中所有单词和字符(不重复)并按照字母顺序排序  
  22. #print(len(set(text3)));     #测定text3中出现的单词和字符数(不重复)  
  23.   
  24.   
  25. ''''' 
  26. from __future__ import  division; 
  27. from nltk.book import text3; 
  28. a = 1.0 * len(text3) / len(set(text3)); 
  29. print(a); 
  30. '''  
  31.   
  32.   
  33. from __future__ import  division;  
  34. from nltk.book import *;  
  35.   
  36. def lexical_diversity(text):  
  37.     return len(text) / len(set(text));      #建立函数测算某个文本中所有单词出现的平均次数  
  38.   
  39. def percentage(count, total):  
  40.     return 100 * count / total;     #建立函数测算某个单词在某个文本中所占的百分比  
  41.   
  42. print(lexical_diversity(text3));  
  43. print(percentage(text4.count("a"), len(text4)));  
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值