对具有双引号

时间:2017-05-24 10:53:50

标签: python-2.7 nlp nltk

我的表格中有一个字符串:

'I am going to visit "Huge Hotel" and the "Grand River"'

我想把它标记为

['I', 'am', 'going',..., 'Huge Hotel','and' ,'the' ,'Grand River']

如所见'Huge Hotel''Grand River'被视为一个单词,因为它们出现在引号中。

import nltk
text = 'I am going to visit "Huge Hotel" and the "Grand River"'
b = nltk.word_tokenize(text)

我已经编写了上面的代码,但它无法正常工作

1 个答案:

答案 0 :(得分:1)

看起来很奇怪,但确实有效:

  1. re.findall('"([^"]*)"', s):查找用双引号括起来的所有子字符串
  2. phrase.replace(' ', '_'):在步骤1的这些子字符串中替换所有带下划线的空格。
  3. 用步骤2中的下划线子串替换双引号中的所有字符串。
  4. 在修改后的字符串上使用word_tokenize()
  5. [OUT]:

    >>> import re
    >>> from nltk import word_tokenize
    >>> s = 'I am going to visit "Huge Hotel" and the "Grand River"'
    >>> for phrase in re.findall('"([^"]*)"', s):
    ...     s = s.replace('"{}"'.format(phrase), phrase.replace(' ', '_'))
    ... 
    >>> s
    'I am going to visit Huge_Hotel and the Grand_River'
    >>> word_tokenize(s)
    ['I', 'am', 'going', 'to', 'visit', 'Huge_Hotel', 'and', 'the', 'Grand_River']
    

    我确信这是一个更简单的正则表达式操作,可以取代一系列正则表达式+字符串操作。

相关问题