正则表达式:带空格的重音字母

时间:2016-03-21 16:26:52

标签: python json regex string

我尝试从JSON字符串中提取关键字并获取该字词的上下文。我的字符串看起来像:

JSON

header( 'Link: <'https://www.myweb.com/mypath'>;rel="preload";as="video"', FALSE );

目前,我的Python代码是:

的Python

{"1" : "Na casa de meu Pai há muitos aposentos; se não fosse assim, eu lhes teria dito. Vou preparar-lhes lugar."}

我想提供一个单词(例如 Pai )并获取关键字之前和之后的字词。我的脚本会计算关键字的所有匹配项,并列出上下文单词。

我的问题是:如何用空格,逗号,圆点等来获得重音字母?什么是最好的方法:列出所需的字符或排除不需要的字符?类似的东西:

re.findall(regex, string)

1 个答案:

答案 0 :(得分:1)

通过json.load()json.loads()加载您的JSON数据,然后使用nltk.ConcordanceIndex来帮助您浏览文本中特定单词周围的单词,例如:

import nltk

text = 'Na casa de meu Pai há muitos aposentos; se não fosse assim, eu lhes teria dito. Vou preparar-lhes lugar.'
tokens = nltk.word_tokenize(text)

c = nltk.ConcordanceIndex(tokens, key=lambda s: s.lower())
result = []
for offset in c.offsets('Pai'):
    result += tokens[offset - 2: offset]
    result += tokens[offset + 1: offset + 3]

print(result)

打印['de', 'meu', 'há', 'muitos']