我试图识别所有在期刊文章中以pdf格式被引用的句子。我将.pdf转换为.txt,并希望找到所有包含引文的句子,可能采用以下格式之一:
我首先将txt标记为句子:
import nltk
from nltk.tokenize import sent_tokenize
ss = sent_tokenize(text)

这将生成类型(Ss)列表,因此我将列表转换为str以使用re:
def listtostring(s):
str1 = ' '
return (str1. join(s))
ee = listtostring(ss)然后,我的想法是识别包含四个数字的句子:
import re
for sentence in ee:
zz = re.findall(r'\d{4}', ee)
if zz:
print (zz)然而,这只是提取年份,而不是包含年份的句子。
发布于 2020-01-06 21:17:50
使用regex时,一些东西(试试看)可以在避免不适当匹配的同时进行适当的回忆(\d{4}可能会给你一些)。
\(([^)]+)?(?:19|20)\d{2}?([^)]+)?\)然后是一个python示例(使用spaCy而不是NLTK)
import re
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp("One statement. Then according to (Smith, 1990) everything will be all right. Or maybe not.")
l = [sent.text for sent in doc.sents]
for sentence in l:
if re.findall(r'\(([^)]+)?(?:19|20)\d{2}?([^)]+)?\)', sentence):
print(sentence)发布于 2020-01-06 20:42:33
import re
l = ['This is 1234','Hello','Also 1234']
for sentence in l:
if re.findall(r'\d{4}',sentence):
print(sentence)输出
This is 1234
Also 1234https://stackoverflow.com/questions/59618657
复制相似问题