【问题标题】:Creating bigrams from a string using regex使用正则表达式从字符串创建二元组
【发布时间】:2016-08-17 17:08:36
【问题描述】:

我有一个类似的字符串:

"[u'LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT', u'LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT']"

取自 Excel 文件。这看起来像一个数组,但因为它是从文件中提取的,所以它只是一个字符串。

我需要做的是:

a) 删除[ ]

b) 通过, 拆分字符串,从而实际创建一个新列表

c) 只取第一个字符串,即u'LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT'

d) 将结果字符串的二元组创建为由空格吐出的实际字符串(而不是二元组):

LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to *extend*to~prepc_according_to+expectancy~-nsubj expectancy~-nsubj+is~parataxis  is~parataxis+NUMBER~nsubj NUMBER~nsubj+NUMBER_SLOT

我一直在玩的当前代码 sn-ps。

text = "[u'LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT', u'LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT']"
text = re.sub('^\[(.*)\]',"\1",text)
text = [text.split(",")[0]]
bigrams = [b for l in text for b in zip(l.split("+")[:-1], l.split("+")[1:])]
bigrams = [("+").join(bigram).encode('utf-8') for bigram in bigrams]
bigrams = (' ').join(map(str, bigrams))
bigrams = ('').join(bigrams)

我的正则表达式似乎什么也没返回。

【问题讨论】:

  • 你的意思是textsubsplit之后是空的吗?
  • @Dhruv Ghulati:什么二元组?

标签: python regex string list n-gram


【解决方案1】:

你的字符串看起来像一个 Python 的 unicode 字符串列表,对吧?

您可以评估它以获取 unicode 字符串列表。一个好方法是使用 ast 模块中的 ast.literal_eval 函数。

简单写:

text = "[u'LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT'," \
       " u'LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT']"

import ast

lines = ast.literal_eval(text)

结果是 unicode 字符串列表:

for line in  lines:
    print(line)

你会得到:

LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT
LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT    

计算 二元组

bigrams = [b for l in lines for b in zip(l.split("+")[:-1], l.split("+")[1:])]
bigrams = ["+".join(bigram).encode('utf-8') for bigram in bigrams]
bigrams = ' '.join(map(str, bigrams))
bigrams = ''.join(bigrams)

【讨论】:

    【解决方案2】:

    我已经解决了这个问题。正则表达式需要经过两次先替换括号,然后获取第一个字符串,然后删除引号:

       text = "[u'LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT', u'LOCATION_SLOT~-prep_in+*extend*to~prepc_according_to+expectancy~-nsubj+is~parataxis+NUMBER~nsubj+NUMBER_SLOT']"
                            text =  re.sub(r'\[u|\]',"",text)
                            text = text.split(",")[0]
                            text = re.sub(r'\'',"",text)
                            text = text.split("+")
                            bigrams = [text[i:i+2] for i in xrange(len(text)-2)]
                            bigrams = [("+").join(bigram).encode('utf-8') for bigram in bigrams]
                            bigrams = (' ').join(map(str, bigrams))
    

    【讨论】:

    • u'...' 不是实际的分隔符吗?
    • 我会尝试匹配所有u'(.*?)'(?<=u').*?(?='),然后在+ 上拆分这些数组元素
    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2016-01-13
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 2013-05-06
    相关资源
    最近更新 更多