Skip to main content
 首页 » 编程设计

python之你能用一致性做正则表达式吗

2024年08月27日33dudu

在NLTK introduction book他们向我们展示了如何通过索引来获取给定单词的上下文。但我想要更复杂的东西。我可以获得围绕特定模式的文本吗?像这样:

text.concordances(", [A-Za-z]+ , ") ~所有单词用空格和逗号包围

请您参考如下方法:

简而言之,nltk 无法从当前状态的正则表达式创建索引。从 nltk 的 ConcordanceIndex 类(或其子类)创建一致性的困难——这是您正在使用的——是该类接受标记列表作为参数(并且围绕那些标记)而不是完整的文本字符串。

我想我的建议是创建您自己的类,它接受字符串而不是标记作为参数。这是一个松散地基于 nltk 的 ConcordanceIndex 类的类,可以作为起点:

import re 
 
 
class RegExConcordanceIndex(object): 
    "Class to mimic nltk's ConcordanceIndex.print_concordance." 
 
    def __init__(self, text): 
        self._text = text 
 
    def print_concordance(self, regex, width=80, lines=25, demarcation=''): 
        """ 
        Prints n <= @lines contexts for @regex with a context <= @width". 
        Make @lines 0 to display all matches. 
        Designate @demarcation to enclose matches in demarcating characters. 
        """  
        concordance = [] 
        matches = re.finditer(regex, self._text, flags=re.M) 
        if matches: 
            for match in matches: 
                start, end = match.start(), match.end() 
                match_width = end - start 
                remaining = (width - match_width) // 2 
                if start - remaining > 0: 
                    context_start = self._text[start - remaining:start] 
                    #  cut the string short if it contains a newline character 
                    context_start = context_start.split('\n')[-1] 
                else: 
                    context_start = self._text[0:start + 1].split('\n')[-1] 
                context_end = self._text[end:end + remaining].split('\n')[0] 
                concordance.append(context_start + demarcation + self._text 
                                   [start:end] + demarcation + context_end) 
                if lines and len(concordance) >= lines: 
                    break 
            print("Displaying %s matches:" % (len(concordance))) 
            print '\n'.join(concordance) 
        else: 
            print "No matches" 

现在你可以像这样测试这个类:

>>> from nltk.corpus import gutenberg 
>>> emma = gutenberg.raw(fileids='austen-emma.txt') 
>>> comma_separated = RegExConcordanceIndex(emma) 
>>> comma_separated.print_concordance(r"(?<=, )[A-Za-z]+(?=,)", demarcation='**')  # matches are enclosed in double asterisks 
 
Displaying 25 matches: 
Emma Woodhouse, **handsome**, clever, and rich, with a comfortab 
Emma Woodhouse, handsome, **clever**, and rich, with a comfortable home 
The real evils, **indeed**, of Emma's situation were the power  
o her many enjoyments.  The danger, **however**, was at present 
well-informed, **useful**, gentle, knowing all the ways of the 
well-informed, useful, **gentle**, knowing all the ways of the family, 
a good-humoured, **pleasant**, excellent man, that he thoroughly  
"No, **papa**, nobody thought of your walking.  We  
"I believe it is very true, my dear, **indeed**," said Mr. Woodhouse, 
should not like her so well as we do, **sir**, 
e none for myself, papa; but I must, **indeed**, 
met with him in Broadway Lane, **when**, because it began to drizzle, 
like Mr. Elton, **papa**,--I must look about for a wife for hi 
"With a great deal of pleasure, **sir**, at any time," said Mr. Knightley, 
better thing.  Invite him to dinner, **Emma**, and help him to the best 
y.  He had received a good education, **but**, 
Miss Churchill, **however**, being of age, and with the full co 
From the expense of the child, **however**, he was soon relieved. 
It was most unlikely, **therefore**, that he should ever want his 
 strong enough to affect one so dear, **and**, as he believed, 
It was, **indeed**, a highly prized letter.  Mrs. Westo 
and he had, **therefore**, earnestly tried to dissuade them  
Fortunately for him, **Highbury**, including Randalls in the same par 
handsome, **rich**, nor married.  Miss Bates stood in th 
a real, **honest**, old-fashioned Boarding-school, wher