[英].findall Regular Expression won't assign to a variable
我正在尝试创建一个函数,该函数搜索大量nltk.text.Text输入并输出“ contribute”或“ donate”之后的所有单词(请参见下面的正则表达式)。
正则表达式可以完美地工作,但是当我尝试将其分配给变量以使我的函数返回它时,该变量不会更新,并且我的函数什么也不返回。
即类型(捐赠)= NoneType对象
我最终希望将此功能应用于数据帧的每一行,并将捐赠值输出到该数据帧中的新列,但是当我现在尝试时,每个输出都是“无”
def find_donation_orgs(x):
text = nltk.Text(nltk.word_tokenize(x))
donation = text.findall(r"<\.> <.*>{,15}? <donat.*|contrib.*|Donat.*|Contrib.*> <.*>*? <to> (<.*>+?) <\.|\,|\;> ")
return donation
我的findall正则表达式本身可以正常工作:
text.findall(r"<\.> <.*>{,15}? <donat.*|contrib.*|Donat.*|Contrib.*> <.*>*? <to> (<.*>+?) <\.|\,|\;> ")
返回此内容作为示例文本:
visit brother Alfred Fuller; the research of Dr. Giuseppe Giaccone at
Georgetown University
为了您的利益:
text = nltk.Text(nltk.word_tokenize(df.Obit.iloc[7]))
print(text)
x = text.findall(r"<\.> <.*>{,15}? <donat.*|contrib.*|Donat.*|Contrib.*> <.*>*? <to> (<.*>+?) <\.|\,|\;> ")
print(x)
返回值:
<Text: M. Jay Janssen , age 95 of Zeeland...>
Resthaven Care Community
None
正如上面的R Nar所指出的那样,.findall正则表达式仅打印并且不返回任何内容。TokenSearcher完美地解决了该问题...它确实输出了一个与字符串相反的令牌列表,但是可以完成工作。
from nltk.text import TokenSearcher
def find_donation_orgs(x):
text = nltk.Text(nltk.word_tokenize(x))
donation = TokenSearcher(text).findall(r"<\.> <.*>{,15}? <donat.*|contrib.*|Donat.*|Contrib.*> <.*>*? <to> (<.*>+?) <\.|\,|\;> ")
return donation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.