Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 空间查找指定单词之前的文本_Python_Regex_Nlp_Spacy - Fatal编程技术网

Python 空间查找指定单词之前的文本

Python 空间查找指定单词之前的文本,python,regex,nlp,spacy,Python,Regex,Nlp,Spacy,我正在写一句话霍尔是托尼奖获得者和格莱美提名者,我想用spaCy规则匹配器提取所获得的奖项(托尼奖),但我似乎无法告诉spaCy查找出现在获胜者之前的单词。可能吗?如果是这样的话,我们怎么能去做呢 nlp = en_core_web_sm.load() awards_lexical = [ {'TEXT': {'REGEX': '\s*'}, 'OP': '*'}, {'IS_PUNCT': True, 'OP': '*'},

我正在写一句话
霍尔是托尼奖获得者和格莱美提名者
,我想用spaCy规则匹配器提取所获得的奖项(
托尼奖
),但我似乎无法告诉spaCy查找出现在
获胜者
之前的单词。可能吗?如果是这样的话,我们怎么能去做呢

nlp = en_core_web_sm.load()

awards_lexical = [
            {'TEXT': {'REGEX': '\s*'}, 'OP': '*'},
            {'IS_PUNCT': True, 'OP': '*'},
            {'TEXT': {'REGEX': '^(winner|recipient)$'}},
            {'OP': '+'},
            ]
def matching(doc, pattern):
    result = []
    for sent in doc.sents:
        matcher = Matcher(nlp.vocab) 
        matcher.add("matching", None, pattern)  

        matches = matcher(nlp(str(sent))) 
        if len(matches)>0:
            match = matches[-1]
            span = sent[match[1]:match[2]] 
            result.append(span.text)

    return result

csv_reader = csv.reader(open('Matheus_Schmitz_hw02_bios.csv', encoding='utf-8'))
limit = 500
count = 0

open("hw2_lexical.jl", "w").close()
with open('hw2_lexical.jl', 'w') as hw2_lexical:
    for (idx, (url, bio)) in tqdm(enumerate(csv_reader), total=limit):
        count += 1
        result = {}
        result['url'] = url
        result['awards'] = matching(nlp(bio), awards_lexical)        
        hw2_lexical.write(str(result)+'\n')
        if count>=limit:
            break
        pass
    hw2_lexical.close()
print(count)

根据我的代码,我认为spaCy会在所选单词之前包含任何文本,但我所有的变体都只是给我从winner | won | Dewarded开始的文本,而不是之前的文本,这是奖品名称最常出现的地方。

你的想法似乎有效,您可以使用提取一个或多个大写单词,后跟
winner
recipient

导入空间
从spacy.matcher导入matcher
text=“霍尔是托尼奖得主和格莱美提名人”
nlp=空间负荷(“核心网络负荷”)
matcher=matcher(nlp.vocab)
add(“Winner”,None,[{'POS':'PROPN','OP':'+'},{'TEXT':{'REGEX':'(?i)^(?:Winner | recipient)$'}])
doc=nlp(文本)
匹配=匹配器(文档)
span=[doc[start:end]表示匹配项中的开始、结束]
对于spacy.util.filter_span中的span(span):
打印(span.text)
#=>托尼奖得主

模式中用作右手标记的
(?i)^(?:winner | recipient)$
正则表达式以不区分大小写的方式匹配整个
winner
recipient
标记。

您的代码不清楚,能否创建一个?什么是
匹配
?另外,如果您想在
winner
之前匹配某些内容,为什么不在您使用的模式中使用
winner
?注<代码>获奖> <代码>似乎在颁奖名前进行,不是吗?还想将“代码>优胜者< /代码>条件添加到现有的<代码> AdvaseLoad < /Cord>规则中,或者您是否考虑在此处添加另一个模式?赢家的模式是什么?您如何定义它(为了提取)?我将winner模式定义为一个或多个大写单词,后跟winner或recipient。这个alrady给了我一些提示,因为我没有使用大写的单词!抱歉,我忘了回复,但的确如此!在我的完整代码中进行修改后,效果非常好。非常感谢!一旦在我的代码中进行了修改,它就可以完美地工作:)