推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
tongyuelong
V2EX  ›  Python

Python nltk 如何解析出英语短语?

  •  
  •   tongyuelong · Jul 1, 2018 · 4158 views
    This topic created in 3021 days ago, the information mentioned may be changed or developed.

    花了点时间研究 nltk,也试着去写点代码,我有这样一段文字,

    
    >>> text = "i would't have the Scotland Yarders know it for the world"
    
    

    这段话里有一个短语, for the world, 有无论如何的意思,参考: https://baike.baidu.com/item/for%20the%20world/8417068

    >>> import nltk
    >>> from nltk.collocations import *
    >>> bigram_measures = nltk.collocations.BigramAssocMeasures()
    >>> trigram_measures = nltk.collocations.TrigramAssocMeasures()
    >>> text = "i would't have the Scotland Yarders know it for the world"
    >>> tokens = nltk.wordpunct_tokenize(text)
    >>> finder = BigramCollocationFinder.from_words(tokens)
    >>> scored = finder.score_ngrams(bigram_measures.raw_freq)
    >>> sorted(bigram for bigram, score in scored)
    [("'", 't'), ('I', 'would'), ('Scotland', 'Yarders'), ('Yarders', 'know'), ('for', 'the'), ('have', 'the'), ('it', 'for'), ('know', 'it'), ('t', 'have'), ('the', 'Scotland'), ('the', 'world'), ('would', "'")]
    

    这代码是 google 来的,不是我写的。最后运行的效果,似乎无法辨别出 for the world 这个短语。

    我想问,目前的 nltk 能准确识别出来这个短语吗?

    bugcoder
        1
    bugcoder  
       Jul 1, 2018   ❤️ 1
    bigram 是两个单词的短语,trigram 才是三个词的。你研究花的时间是 5 分钟?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2533 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 15:51 · PVG 23:51 · LAX 08:51 · JFK 11:51
    ♥ Do have faith in what you're doing.