【问题标题】:Retain URLs using nltk Tokenizing使用 nltk Tokenizing 保留 URL
【发布时间】:2018-02-08 00:19:57
【问题描述】:

我使用 nltk 来标记单词。但我想保留句子中的网址。 例如:

输入

It’s word1 word2 https://www.google.com. Word3 word4 (word5). Word6 word7 http://visjs.org/#gallery word8. Word9 word10 (https://www.baidu.com). Word11-word12 word13 word14 http://visjs.org/#gallery. 

期望的输出:

It s word1 word2 https://www.google.com Word3 word4 word5 Word6 word7 word8 Word9 word10 https://www.baidu.com Word11 word12 word13 word14 http://visjs.org/#gallery

我使用tokens=WhitespaceTokenizer().tokenize(Input),但它无法删除分隔符和标点符号。谁能提供解决方案?

【问题讨论】:

  • 您使用的是 2.7 还是 3.5 或其他版本?我使用最新的 nltk 包在 3.5.1 中获得了正确的输出。它似乎将网址视为单个令牌
  • @jalomas7 你说的是python版本吗? github 上的 nltk 最新稳定版是 3.2.5 - 使用 nltk 和 python 3.5,url 被 word_tokenize() 分割

标签: python nltk tokenize


【解决方案1】:

nltk 有一个标记器 nltk.tokenize.casual_tokenize 可以做你想做的事,虽然它不能做 word_tokenize 用收缩等做的花哨的事情。

休闲文档是here

【讨论】:

    【解决方案2】:
    >>> text = "It’s word1 word2 https://www.google.com. Word3 word4 (word5). Word6 word7 http://visjs.org/#gallery word8. Word9 word10 (https://www.baidu.com). Word11-word12 word13 word14 http://visjs.org/#gallery."
    >>> _text = " ".join([w.strip('.,()') for w in text.split()])
    >>> print _text.replace("’s", " 's")
    It 's word1 word2 https://www.google.com Word3 word4 word5 Word6 word7 http://visjs.org/#gallery word8 Word9 word10 https://www.baidu.com Word11-word12 word13 word14 http://visjs.org/#gallery
    

    >>> text = "It’s word1 word2 https://www.google.com. Word3 word4 (word5). Word6 word7 http://visjs.org/#gallery word8. Word9 word10 (https://www.baidu.com). Word11-word12 word13 word14 http://visjs.org/#gallery."
    >>> _text = text.replace("’s", " 's")
    >>> _text = " ".join([w.strip(".,()'") for w in _text.split()])
    >>> print _text
    It s word1 word2 https://www.google.com Word3 word4 word5 Word6 word7 http://visjs.org/#gallery word8 Word9 word10 https://www.baidu.com Word11-word12 word13 word14 http://visjs.org/#gallery
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      相关资源
      最近更新 更多