Python 如何在特定标记之后和之前获取单词?

Python 如何在特定标记之后和之前获取单词?,python,regex,nlp,text-processing,trigram,Python,Regex,Nlp,Text Processing,Trigram,我目前正在从事一个项目,该项目只是创建基本的语料库数据库和标记文本。但我似乎陷入了一件事。假设我们有这些东西: blue My blue car # What I exactly want. he blue jac # That's not what I want. That must be "the blue jacket." eir blue phone # Wrong! > their a blue ali # Wrong! > alien

我目前正在从事一个项目,该项目只是创建基本的语料库数据库和标记文本。但我似乎陷入了一件事。假设我们有这些东西:

blue
    My blue car # What I exactly want.
    he blue jac # That's not what I want. That must be "the blue jacket."
    eir blue phone # Wrong! > their
    a blue ali # Wrong! > alien
    . Blue is # Okay.
    is blue. # Okay.
    ...
导入操作系统,重新
文本=[]
对于os.listdir(somedir)中的i:#somedir包含包含非常大的纯文本的文本文件。
以开放式(i,'r')作为f:
text.append(f.read())
现在我想在标记前后找到单词

myToken = 'blue'
found = []
for i in texts:
    fnd = re.findall('[a-zA-Z0-9]+ %s [a-zA-Z0-9]+|\. %s [a-zA-Z0-9]+|[a-zA-Z0-9]+ %s\.' %(myToken, myToken, myToken), i, re.IGNORECASE|re.UNICODE)
    found.extend(fnd)

print myToken
for i in found:
    print '\t\t%s' %(i)
我认为有三种可能:标记可能会开始句子,标记可能会结束句子,或者标记可能会出现在句子的某个地方,所以我使用了上面的正则表达式规则。当我跑步时,我会遇到这些事情:

blue
    My blue car # What I exactly want.
    he blue jac # That's not what I want. That must be "the blue jacket."
    eir blue phone # Wrong! > their
    a blue ali # Wrong! > alien
    . Blue is # Okay.
    is blue. # Okay.
    ...
我还尝试了\b\w\b或\b\w\b方法,但不幸的是,这些方法没有返回任何结果,而是返回了错误的结果。我试过:

'\b\w\b%s\b[a-zA-Z0-9]+|\.\b%s\b\w\b|\b\w\b%s\.'
'\b\W\b%s\b[a-zA-Z0-9]+|\.\b%s\b\W\b|\b\W\b%s\.'

我希望问题不要太模糊。

假设令牌是测试

        (?=^test\s+.*|.*?\s+test\s+.*?|.*?\s+test$).*
你可以使用lookahead,它不会消耗任何东西,同时也会验证


假设令牌是测试

        (?=^test\s+.*|.*?\s+test\s+.*?|.*?\s+test$).*
你可以使用lookahead,它不会消耗任何东西,同时也会验证


我想你想要的是:

  • (可选)单词和空格
  • (始终)
    “蓝色”
  • (可选)空格和单词
  • 因此,一个合适的正则表达式是:

    r'(?i)((?:\w+\s)?blue(?:\s\w+)?)'
    
    例如:

    >>> import re
    >>> text = """My blue car
    the blue jacket
    their blue phone
    a blue alien
    End sentence. Blue is
    is blue."""
    >>> re.findall(r'(?i)((?:\w+\s)?{0}(?:\s\w+)?)'.format('blue'), text)
    ['My blue car', 'the blue jacket', 'their blue phone', 'a blue alien', 'Blue is', 'is blue']
    

    请参阅演示和逐个令牌的解释。

    我想您需要的是:

  • (可选)单词和空格
  • (始终)
    “蓝色”
  • (可选)空格和单词
  • 因此,一个合适的正则表达式是:

    r'(?i)((?:\w+\s)?blue(?:\s\w+)?)'
    
    例如:

    >>> import re
    >>> text = """My blue car
    the blue jacket
    their blue phone
    a blue alien
    End sentence. Blue is
    is blue."""
    >>> re.findall(r'(?i)((?:\w+\s)?{0}(?:\s\w+)?)'.format('blue'), text)
    ['My blue car', 'the blue jacket', 'their blue phone', 'a blue alien', 'Blue is', 'is blue']
    

    请参阅演示和令牌对令牌的解释。

    正则表达式有时会很慢(如果没有正确实现),而且在某些情况下,接受的答案对我来说并不适用

    所以我选择了暴力解决方案(不是说它是最好的),其中关键字可以由几个词组成:

    @staticmethod
    def find_neighbours(word, sentence):
        prepost_map = []
    
        if word not in sentence:
            return prepost_map
    
        split_sentence = sentence.split(word)
        for i in range(0, len(split_sentence) - 1):
            prefix = ""
            postfix = ""
    
            prefix_list = split_sentence[i].split()
            postfix_list = split_sentence[i + 1].split()
    
            if len(prefix_list) > 0:
                prefix = prefix_list[-1]
    
            if len(postfix_list) > 0:
                postfix = postfix_list[0]
    
            prepost_map.append([prefix, word, postfix])
    
        return prepost_map
    

    关键字前后的空字符串分别表示关键字是句子中的第一个或最后一个单词。

    正则表达式有时速度较慢(如果没有正确实现),而且在某些情况下,接受的答案对我不起作用

    所以我选择了暴力解决方案(不是说它是最好的),其中关键字可以由几个词组成:

    @staticmethod
    def find_neighbours(word, sentence):
        prepost_map = []
    
        if word not in sentence:
            return prepost_map
    
        split_sentence = sentence.split(word)
        for i in range(0, len(split_sentence) - 1):
            prefix = ""
            postfix = ""
    
            prefix_list = split_sentence[i].split()
            postfix_list = split_sentence[i + 1].split()
    
            if len(prefix_list) > 0:
                prefix = prefix_list[-1]
    
            if len(postfix_list) > 0:
                postfix = postfix_list[0]
    
            prepost_map.append([prefix, word, postfix])
    
        return prepost_map
    

    关键字前后的空字符串分别表示关键字是句子中的第一个或最后一个单词。

    这很好。仍然存在一些问题,但我认为我可以克服,再次感谢。:)这很好。仍然存在一些问题,但我认为我可以克服,再次感谢。:)