2016-05-13 90 views
0

我创建了一个函数来搜索文本中给定单词(w)的上下文,其中左侧和右侧是用于记录单词数灵活性的参数。使用正则表达式查找单词上下文

import re 
def get_context (text, w, left, right): 
    text.insert (0, "*START*") 
    text.append ("*END*") 

    all_contexts = [] 

    for i in range(len(text)): 

     if re.match(w,text[i], 0): 

      if i < left: 
       context_left = text[:i] 

      else: 
       context_left = text[i-left:i] 

      if len(text) < (i+right): 
       context_right = text[i:] 

      else: 
       context_right = text[i:(i+right+1)] 

      context = context_left + context_right 

      all_contexts.append(context) 
    return all_contexts 

因此,例如,如果一个具有在像这样的列表的形式的文本:

文本= [ '的Python', '是', '动态', '类型','语言','Python', 'functions','really','care','about','what','you','pass','to', 'them','but','你','有','它','','错','方式','如果','你','想','到','通','一','千' ','arguments','to','your', 'function','then','you','can','explicit','define','every', 'parameter','in ','你的','功能','定义','和','你的', '功能','将','是','自动','能','到','处理', 'all',' ”, '参数', '你', '通', '到', '他们', '对', '你']

的功能,例如工作正常:

get_context(text, "function",2,2) 
[['language', 'python', 'functions', 'really', 'care'], ['to', 'your', 'function', 'then', 'you'], ['in', 'your', 'function', 'definition', 'and'], ['and', 'your', 'function', 'will', 'be']] 

现在我想建立的每一个字的文本上下文的字典执行以下操作:

d = {} 
for w in set(text): 
    d[w] = get_context(text,w,2,2) 

但我正在逐渐这个错误。

Traceback (most recent call last): 
    File "<pyshell#32>", line 2, in <module> 
    d[w] = get_context(text,w,2,2) 
    File "<pyshell#20>", line 9, in get_context 
    if re.match(w,text[i], 0): 
    File "/usr/lib/python3.4/re.py", line 160, in match 
    return _compile(pattern, flags).match(string) 
    File "/usr/lib/python3.4/re.py", line 294, in _compile 
    p = sre_compile.compile(pattern, flags) 
    File "/usr/lib/python3.4/sre_compile.py", line 568, in compile 
    p = sre_parse.parse(p, flags) 
    File "/usr/lib/python3.4/sre_parse.py", line 760, in parse 
    p = _parse_sub(source, pattern, 0) 
    File "/usr/lib/python3.4/sre_parse.py", line 370, in _parse_sub 
    itemsappend(_parse(source, state)) 
    File "/usr/lib/python3.4/sre_parse.py", line 579, in _parse 
    raise error("nothing to repeat") 
sre_constants.error: nothing to repeat 

我不明白这个错误。谁能帮我这个?

回答

1

问题是“* START *”和“* END *”被解释为正则表达式。另外请注意,在函数开始处插入“* START *”和“* END *”text会导致问题。你应该只做一次。

这里的工作代码完整版:

import re 

def get_context(text, w, left, right): 
    all_contexts = [] 
    for i in range(len(text)): 
     if re.match(w,text[i], 0): 
      if i < left: 
       context_left = text[:i] 
      else: 
       context_left = text[i-left:i] 
      if len(text) < (i+right): 
       context_right = text[i:] 
      else: 
       context_right = text[i:(i+right+1)] 
      context = context_left + context_right 
      all_contexts.append(context) 
    return all_contexts 

text = ['Python', 'is', 'dynamically', 'typed', 'language', 
     'Python', 'functions', 'really', 'care', 'about', 'what', 
     'you', 'pass', 'to', 'them', 'but', 'you', 'got', 'it', 'the', 
     'wrong', 'way', 'if', 'you', 'want', 'to', 'pass', 'one', 
     'thousand', 'arguments', 'to', 'your', 'function', 'then', 
     'you', 'can', 'explicitly', 'define', 'every', 'parameter', 
     'in', 'your', 'function', 'definition', 'and', 'your', 
     'function', 'will', 'be', 'automagically', 'able', 'to', 'handle', 
     'all', 'the', 'arguments', 'you', 'pass', 'to', 'them', 'for', 'you'] 

text.insert(0, "START") 
text.append("END") 

d = {} 
for w in set(text): 
    d[w] = get_context(text,w,2,2) 

也许你可以用w == text[i]取代re.match(w,text[i], 0)

+0

好吧,这就是问题所在。我没有想到这两个* START *和* END *。我想到了==文本[我],但我想知道为什么这不起作用。谢谢 – Wunter

0

text中至少有一个元素包含正则表达式中特殊的字符。如果你只是想查找的单词是否是字符串中,只需用str.startswith,即

if text[i].startswith(w): # instead of re.match(w,text[i], 0): 

但我不明白为什么你反正检查为,而不是平等。

+0

我认为使用're.match'会增加一些灵活性,例如在匹配'functions?'的同时寻找函数和函数。感谢您的建议 – Wunter

1

整个东西可以重新写得很简洁如下,

text = 'Python is dynamically typed language Python functions really care about what you pass to them but you got it the wrong way if you want to pass one thousand arguments to your function then you can explicitly define every parameter in your function definition and your function will be automagically able to handle all the arguments you pass to them for you' 

保持它str,假设context = 'function',

pat = re.compile(r'(\w+\s\w+\s)functions?(?=(\s\w+\s\w+))') 
pat.findall(text) 
[('language Python ', ' really care'), 
('to your ', ' then you'), 
('in your ', ' definition and'), 
('and your ', ' will be')] 

现在,少量的定制将需要在正则表达式允许,像说的话,functionalfunctioning不仅functionfunctions。但重要的想法是废除索引和更多的功能。

请注意,如果这不适合你,当你批量应用它。

+0

我认为如果我想改变单词的左侧和右侧,处理列表会更容易。我想过使用正则表达式,但我想不出设置双方字数的方法。感谢您的建议 – Wunter

+0

@Wunter如果您使用'list',请务必知道'inset'和'+'是红旗。他们很慢。 'append'没问题。 –

+0

感谢您的咨询。我开始学习编程。我会牢记在心:) – Wunter