1. git https://github.com/linyi0604/MachineLearning
  2.  
  3. 分别使用词袋法和nltk自然预言处理包提供的文本特征提取
  1. from sklearn.feature_extraction.text import CountVectorizer
  2. import nltk
  3. # nltk.download("punkt")
  4. # nltk.download('averaged_perceptron_tagger')
  5.  
  6. '''
  7. 分别使用词袋法和nltk自然预言处理包提供的文本特征提取
  8. '''
  9.  
  10. sent1 = "The cat is walking in the bedroom."
  11. sent2 = "A dog was running across the kitchen."
  12. # 使用词袋法 将文本转化为特征向量
  13. count_vec = CountVectorizer()
  14. sentences = [sent1, sent2]
  15. # 输出转化后的特征向量
  16. # print(count_vec.fit_transform(sentences).toarray())
  17. '''
  18. [[0 1 1 0 1 1 0 0 2 1 0]
  19. [1 0 0 1 0 0 1 1 1 0 1]]
  20. '''
  21. # 输出转化后特征的含义
  22. # print(count_vec.get_feature_names())
  23. '''
  24. ['across', 'bedroom', 'cat', 'dog', 'in', 'is', 'kitchen', 'running', 'the', 'walking', 'was']
  25. '''
  26.  
  27. # 使用nltk对文本进行语言分析
  28. # 对句子词汇分割和正则化 把aren't 分割成 are 和 n't I'm 分割成 I和'm
  29. tokens1 = nltk.word_tokenize(sent1)
  30. tokens2 = nltk.word_tokenize(sent2)
  31. # print(tokens1)
  32. # print(tokens2)
  33. '''
  34. ['The', 'cat', 'is', 'walking', 'in', 'the', 'bedroom', '.']
  35. ['A', 'dog', 'was', 'running', 'across', 'the', 'kitchen', '.']
  36. '''
  37. # 整理词汇表 按照ASCII的顺序排序
  38. vocab_1 = sorted(set(tokens1))
  39. vocab_2 = sorted(set(tokens2))
  40. # print(vocab_1)
  41. # print(vocab_2)
  42. '''
  43. ['.', 'The', 'bedroom', 'cat', 'in', 'is', 'the', 'walking']
  44. ['.', 'A', 'across', 'dog', 'kitchen', 'running', 'the', 'was']
  45. '''
  46. # 初始化stemer 寻找每个单词最原始的词根
  47. stemmer = nltk.stem.PorterStemmer()
  48. stem_1 = [stemmer.stem(t) for t in tokens1]
  49. stem_2 = [stemmer.stem(t) for t in tokens2]
  50. # print(stem_1)
  51. # print(stem_2)
  52. '''
  53. ['the', 'cat', 'is', 'walk', 'in', 'the', 'bedroom', '.']
  54. ['A', 'dog', 'wa', 'run', 'across', 'the', 'kitchen', '.']
  55. '''
  56. # 利用词性标注器 对词性进行标注
  57. pos_tag_1 = nltk.tag.pos_tag(tokens1)
  58. pos_tag_2 = nltk.tag.pos_tag(tokens2)
  59. # print(pos_tag_1)
  60. # print(pos_tag_2)
  61. '''
  62. [('The', 'DT'), ('cat', 'NN'), ('is', 'VBZ'), ('walking', 'VBG'), ('in', 'IN'), ('the', 'DT'), ('bedroom', 'NN'), ('.', '.')]
  63. [('A', 'DT'), ('dog', 'NN'), ('was', 'VBD'), ('running', 'VBG'), ('across', 'IN'), ('the', 'DT'), ('kitchen', 'NN'), ('.', '.')]
  64. '''

机器学习之路: python nltk 文本特征提取的更多相关文章

  1. 机器学习之路: python k近邻分类器 KNeighborsClassifier 鸢尾花分类预测

    使用python语言 学习k近邻分类器的api 欢迎来到我的git查看源代码: https://github.com/linyi0604/MachineLearning from sklearn.da ...

  2. 机器学习之路--Python

    常用数据结构 1.list 列表 有序集合 classmates = ['Michael', 'Bob', 'Tracy'] len(classmates) classmates[0] len(cla ...

  3. 机器学习之路: python 回归树 DecisionTreeRegressor 预测波士顿房价

    python3 学习api的使用 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.datasets import ...

  4. 机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价

    python3学习使用api 线性回归,和 随机参数回归 git: https://github.com/linyi0604/MachineLearning from sklearn.datasets ...

  5. 机器学习之路: python 决策树分类DecisionTreeClassifier 预测泰坦尼克号乘客是否幸存

    使用python3 学习了决策树分类器的api 涉及到 特征的提取,数据类型保留,分类类型抽取出来新的类型 需要网上下载数据集,我把他们下载到了本地, 可以到我的git下载代码和数据集: https: ...

  6. python —— 文本特征提取 CountVectorize

    CountVectorize 来自:python学习 文本特征提取(二) CountVectorizer TfidfVectorizer 中文处理 - CSDN博客 https://blog.csdn ...

  7. 机器学习之路:python 文本特征提取 CountVectorizer, TfidfVectorizer

    本特征提取: 将文本数据转化成特征向量的过程 比较常用的文本特征表示法为词袋法词袋法: 不考虑词语出现的顺序,每个出现过的词汇单独作为一列特征 这些不重复的特征词汇集合为词表 每一个文本都可以在很长的 ...

  8. 【NLP】干货!Python NLTK结合stanford NLP工具包进行文本处理

    干货!详述Python NLTK下如何使用stanford NLP工具包 作者:白宁超 2016年11月6日19:28:43 摘要:NLTK是由宾夕法尼亚大学计算机和信息科学使用python语言实现的 ...

  9. 【NLP】Python NLTK处理原始文本

    Python NLTK 处理原始文本 作者:白宁超 2016年11月8日22:45:44 摘要:NLTK是由宾夕法尼亚大学计算机和信息科学使用python语言实现的一种自然语言工具包,其收集的大量公开 ...

随机推荐

  1. EF记录统一添加创建,修改时间

    public class BaseEntity { public DateTime? DateCreated { get; set; } public string UserCreated { get ...

  2. HDU 1069 Monkey and Banana(最长递减子序列)

    题目链接 题意:摞长方体,给定长方体的长宽高,个数无限制,可随意翻转,要求下面的长方体的长和宽都大于上面的,都不能相等,问最多能摞多高. 题解:个数无限,其实每种形态最多就用一次,把每种形态都单独算一 ...

  3. 最大团 HDU-1530

    传送门: 洛谷 Vjudge    (题目略有不同) 题目描述 • 给定一个图 tt = (V, E) • 求一个点集 S ,使得对于任意 x ≠ y ∈ S ,x 和 y 都有一条边 • |V | ...

  4. sql 存储过程导出指定数据到.txt文件(定时)

    需求:每天生成一份txt文件数据,供第三方通过http方式调用 方法: 1.新建存储过程: USE [LocojoyMicroMessage] GO /****** Object: StoredPro ...

  5. java.lang.IllegalArgumentException: class com.beisheng.maerte.mode.MyCouponVO declares multiple JSON fields named count

    原因是:子类和父类有相同的字段属性.解决办法:(1)将父类中的该字段去掉(不要),或者在需要打印的字段上加上注解@Expose (2):由于我报错的类都是在jar包里面,所以第一种方法不好使.只好采用 ...

  6. windows环境cmd下执行jar

    项目目录结构 一,在pom.xml文件里添加配置 <build> <plugins> <plugin> <groupId>org.apache.mave ...

  7. Python基础:获取平台相关信息

    Windows 10家庭中文版,Python 3.6.4, 本文介绍了使用os.platform.sys三个模块获取Python程序的运行平台相关的信息. os模块:提供 各种各样的操作系统接口 os ...

  8. 耗时任务DefaultEventExecutorGroup 定时任务

    一. 耗时任务 static final EventExecutorGroup group = new DefaultEventExecutorGroup(16); // Tell the pipel ...

  9. C++链接与装载

    1..obj文件的内部结构 2.映射到进程虚拟空间 3.链接的原理    C++ Code  123456789   1.未解决符号表:提供了所有在该编译单元里引用但是定义并不在本编译单元里的符号及其 ...

  10. T-sql语句修改数据库逻辑名、数据库名、物理名(sql2000)

    --更改MSSQL数据库物理文件名Sql语句的写法 --注意:要在活动监视器里面确保没有进程连接你要改名的数据库!!!!!!!!!!!!!!!!!!!! -- Sql语句如下 USE master - ...