Мне было интересно, а что, если получить имена (и годы) из References
в конце документа и использовать их для поиска ссылок в документе.
В предыдущем вопросе вы получили код, который получает References
в конце документа.
Используя регулярное выражение '((.*)\. (\d{4})\.
Я могу получить имена как одну строку, год как одну строку (и, в конечном итоге, оба в одной строке)
authors_and_year = re.match('((.*)\. (\d{4})\.)', line)
text, authors, year = authors_and_year.groups()
ie.
text: Christer Samuelsson, Pasi Tapanainen, and Atro Voutilainen. 1996.
authors: Christer Samuelsson, Pasi Tapanainen, and Atro Voutilainen
year: 1996
Используя следующее регулярное выражение ',[ ]*and |,[ ]*| and '
Я могу разделить строку с именами на список имен
names = re.split(',[ ]*and |,[ ]*| and ', authors)
и, используя обычный split(" ")
, я могу получить фамилии (фамилии), которые могут будет более полезным, чем полное имя
names = [(name, name.split(' ')[-1]) for name in names]
ie.
names: [('Christer Samuelsson', 'Samuelsson'), ('Pasi Tapanainen', 'Tapanainen'), ('Atro Voutilainen', 'Voutilainen')]
И теперь я могу использовать эти имена (или, скорее, фамилии) и годы для генерации строк вроде surname (year)
, surname, year
и выполните поиск в документе.
Если фамилий много, я могу получить первую фамилию и сгенерировать surname et al. (year)
, et c.
И используя эту строку и строку starndard функция text.count(generated_string)
Я могу их сосчитать.
На данный момент это все, что у меня есть, но это все еще не идеально.
Вы можете найти все ссылки в документе вручную и использовать их для тестирования кода. И вы увидите, какие из них подсчитаны правильно, а какие требуют дополнительных изменений.
Например, есть ссылка с 's
в тексте We are using Atro Voutilainen's (1995)
. Возможно, документ следует очистить, как в NLP
(Обработка естественного языка), используя nltk
И некоторые собственные символы создают проблему - имя Järvinen
в одном месте извлекается как J~rvinen
и в другом месте как J/irvinen
import PyPDF2
from PyPDF2.pdf import * # to import function used in origimal `extractText`
# --- functions ---
def myExtractText(self, distance=None):
# original code from `page.extractText()`
# https://github.com/mstamy2/PyPDF2/blob/d7b8d3e0f471530267827511cdffaa2ab48bc1ad/PyPDF2/pdf.py#L2645
text = u_("")
content = self["/Contents"].getObject()
if not isinstance(content, ContentStream):
content = ContentStream(content, self.pdf)
prev_x = 0
prev_y = 0
for operands, operator in content.operations:
# used only for test to see values in variables
#print('>>>', operator, operands)
if operator == b_("Tj"):
_text = operands[0]
if isinstance(_text, TextStringObject):
text += _text
elif operator == b_("T*"):
text += "\n"
elif operator == b_("'"):
text += "\n"
_text = operands[0]
if isinstance(_text, TextStringObject):
text += operands[0]
elif operator == b_('"'):
_text = operands[2]
if isinstance(_text, TextStringObject):
text += "\n"
text += _text
elif operator == b_("TJ"):
for i in operands[0]:
if isinstance(i, TextStringObject):
text += i
text += "\n"
if operator == b_("Tm"):
if distance is True:
text += '\n'
elif isinstance(distance, int):
x = operands[-2]
y = operands[-1]
diff_x = prev_x - x
diff_y = prev_y - y
#print('>>>', diff_x, diff_y - y)
#text += f'| {diff_x}, {diff_y - y} |'
if diff_y > distance or diff_y < 0: # (bigger margin) or (move to top in next column)
text += '\n'
#text += '\n' # to add empty line between elements
prev_x = x
prev_y = y
return text
# --- main ---
pdfFileObj = open('A97-1011.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
text = ''
for page in pdfReader.pages:
#text += page.extractText() # original function
#text += myExtractText(page) # modified function (works like original version)
#text += myExtractText(page, True) # modified function (add `\n` after every `Tm`)
text += myExtractText(page, 17) # modified function (add `\n` only if distance is bigger then `17`)
# get only text after word `References`
pos = text.lower().find('references')
# only referencers as text
references = text[pos+len('references '):]
# doc without references
doc = text[:pos]
# referencers as list
references = references.split('\n')
# remove empty lines and lines which have 2 chars (ie. page number)
references = [item.strip() for item in references if len(item.strip()) > 2]
print('\n--- names ---\n')
data = []
for nubmer, line in enumerate(references, 1): # skip last element with page number
line = line.strip()
if line: # skip empty line
authors_and_year = re.match('((.*)\. (\d{4})\.)', line)
text, authors, year = authors_and_year.groups()
#print(text, '|', authors, '|', year)
names = re.split(',[ ]*and |,[ ]*| and ', authors)
#print(names)
# [(name, last_name), ...]
names = [(name, name.split(' ')[-1]) for name in names]
#print(names)
#print(' line:', line)
print(' text:', text)
print('authors:', authors)
print(' year:', year)
print(' names:', names)
print('---')
data.append((authors, names, year))
print('\n--- counting ---\n')
# https://guides.lib.monash.edu/citing-referencing/APA-In-text
# Tapanainen and J/~rvine,
for authors, names, year in data:
print('authors:', authors)
print(' year:', year)
print(' names:', names)
print(' et al.:', len(names) > 1)
print(' and :', len(names) == 2)
print('---')
first_lastname = names[0][-1]
print(doc.count(first_lastname), first_lastname)
print(doc.count(first_lastname + ', ' + year), first_lastname + ', ' + year)
print(doc.count(first_lastname + ' (' + year + ')'), first_lastname + ' (' + year + ')')
if len(names) > 1:
first_lastname_et_al = first_lastname + ' et al.'
print(doc.count(first_lastname_et_al), first_lastname_et_al)
print(doc.count(first_lastname_et_al + ', ' + year), first_lastname_et_al + ', ' + year)
print(doc.count(first_lastname_et_al + ' (' + year + ')'), first_lastname_et_al + ' (' + year + ')')
if len(names) == 2:
all_lastnames = ' and '.join(item[-1] for item in names)
print(doc.count(all_lastnames), all_lastnames)
print(doc.count(all_lastnames + ', ' + year), all_lastnames + ', ' + year)
print(doc.count(all_lastnames + ' (' + year + ')'), all_lastnames + ' (' + year + ')')
print('----------')
Результат для извлечения имен:
--- names ---
text: Arto Anttila. 1995.
authors: Arto Anttila
year: 1995
names: [('Arto Anttila', 'Anttila')]
---
text: Dekang Lin. 1996.
authors: Dekang Lin
year: 1996
names: [('Dekang Lin', 'Lin')]
---
text: Jason M. Eisner. 1996.
authors: Jason M. Eisner
year: 1996
names: [('Jason M. Eisner', 'Eisner')]
---
text: David G. Hays. 1964.
authors: David G. Hays
year: 1964
names: [('David G. Hays', 'Hays')]
---
text: Hans Jiirgen Heringer. 1993.
authors: Hans Jiirgen Heringer
year: 1993
names: [('Hans Jiirgen Heringer', 'Heringer')]
---
text: Richard Hudson. 1991.
authors: Richard Hudson
year: 1991
names: [('Richard Hudson', 'Hudson')]
---
text: Arvi Hurskainen. 1996.
authors: Arvi Hurskainen
year: 1996
names: [('Arvi Hurskainen', 'Hurskainen')]
---
text: Time J~rvinen. 1994.
authors: Time J~rvinen
year: 1994
names: [('Time J~rvinen', 'J~rvinen')]
---
text: Fred Karlsson, Atro Voutilainen, Juha Heikkil~, and Arto Anttila, editors. 1995.
authors: Fred Karlsson, Atro Voutilainen, Juha Heikkil~, and Arto Anttila, editors
year: 1995
names: [('Fred Karlsson', 'Karlsson'), ('Atro Voutilainen', 'Voutilainen'), ('Juha Heikkil~', 'Heikkil~'), ('Arto Anttila', 'Anttila'), ('editors', 'editors')]
---
text: Fred Karlsson. 1990.
authors: Fred Karlsson
year: 1990
names: [('Fred Karlsson', 'Karlsson')]
---
text: Michael McCord. 1990.
authors: Michael McCord
year: 1990
names: [('Michael McCord', 'McCord')]
---
text: Igor A. Mel'~uk. 1987.
authors: Igor A. Mel'~uk
year: 1987
names: [("Igor A. Mel'~uk", "Mel'~uk")]
---
text: Christer Samuelsson, Pasi Tapanainen, and Atro Voutilainen. 1996.
authors: Christer Samuelsson, Pasi Tapanainen, and Atro Voutilainen
year: 1996
names: [('Christer Samuelsson', 'Samuelsson'), ('Pasi Tapanainen', 'Tapanainen'), ('Atro Voutilainen', 'Voutilainen')]
---
text: Daniel Sleator and Davy Temperley. 1991.
authors: Daniel Sleator and Davy Temperley
year: 1991
names: [('Daniel Sleator', 'Sleator'), ('Davy Temperley', 'Temperley')]
---
text: Pasi Tapanainen and Time J/irvinen. 1994.
authors: Pasi Tapanainen and Time J/irvinen
year: 1994
names: [('Pasi Tapanainen', 'Tapanainen'), ('Time J/irvinen', 'J/irvinen')]
---
text: Pasi Tapanainen. 1996.
authors: Pasi Tapanainen
year: 1996
names: [('Pasi Tapanainen', 'Tapanainen')]
---
text: Lucien TesniSre. 1959.
authors: Lucien TesniSre
year: 1959
names: [('Lucien TesniSre', 'TesniSre')]
---
text: Atro Voutilainen. 1995.
authors: Atro Voutilainen
year: 1995
names: [('Atro Voutilainen', 'Voutilainen')]
---
Результат подсчета:
--- counting ---
authors: Arto Anttila
year: 1995
names: [('Arto Anttila', 'Anttila')]
et al.: False
and : False
---
1 Anttila
0 Anttila, 1995
1 Anttila (1995)
----------
authors: Dekang Lin
year: 1996
names: [('Dekang Lin', 'Lin')]
et al.: False
and : False
---
4 Lin
0 Lin, 1996
1 Lin (1996)
----------
authors: Jason M. Eisner
year: 1996
names: [('Jason M. Eisner', 'Eisner')]
et al.: False
and : False
---
2 Eisner
2 Eisner, 1996
0 Eisner (1996)
----------
authors: David G. Hays
year: 1964
names: [('David G. Hays', 'Hays')]
et al.: False
and : False
---
1 Hays
1 Hays, 1964
0 Hays (1964)
----------
authors: Hans Jiirgen Heringer
year: 1993
names: [('Hans Jiirgen Heringer', 'Heringer')]
et al.: False
and : False
---
1 Heringer
1 Heringer, 1993
0 Heringer (1993)
----------
authors: Richard Hudson
year: 1991
names: [('Richard Hudson', 'Hudson')]
et al.: False
and : False
---
2 Hudson
1 Hudson, 1991
1 Hudson (1991)
----------
authors: Arvi Hurskainen
year: 1996
names: [('Arvi Hurskainen', 'Hurskainen')]
et al.: False
and : False
---
1 Hurskainen
0 Hurskainen, 1996
1 Hurskainen (1996)
----------
authors: Time J~rvinen
year: 1994
names: [('Time J~rvinen', 'J~rvinen')]
et al.: False
and : False
---
2 J~rvinen
1 J~rvinen, 1994
1 J~rvinen (1994)
----------
authors: Fred Karlsson, Atro Voutilainen, Juha Heikkil~, and Arto Anttila, editors
year: 1995
names: [('Fred Karlsson', 'Karlsson'), ('Atro Voutilainen', 'Voutilainen'), ('Juha Heikkil~', 'Heikkil~'), ('Arto Anttila', 'Anttila'), ('editors', 'editors')]
et al.: True
and : False
---
3 Karlsson
0 Karlsson, 1995
0 Karlsson (1995)
2 Karlsson et al.
1 Karlsson et al., 1995
1 Karlsson et al. (1995)
----------
authors: Fred Karlsson
year: 1990
names: [('Fred Karlsson', 'Karlsson')]
et al.: False
and : False
---
3 Karlsson
0 Karlsson, 1990
1 Karlsson (1990)
----------
authors: Michael McCord
year: 1990
names: [('Michael McCord', 'McCord')]
et al.: False
and : False
---
1 McCord
1 McCord, 1990
0 McCord (1990)
----------
authors: Igor A. Mel'~uk
year: 1987
names: [("Igor A. Mel'~uk", "Mel'~uk")]
et al.: False
and : False
---
1 Mel'~uk
0 Mel'~uk, 1987
1 Mel'~uk (1987)
----------
authors: Christer Samuelsson, Pasi Tapanainen, and Atro Voutilainen
year: 1996
names: [('Christer Samuelsson', 'Samuelsson'), ('Pasi Tapanainen', 'Tapanainen'), ('Atro Voutilainen', 'Voutilainen')]
et al.: True
and : False
---
1 Samuelsson
0 Samuelsson, 1996
0 Samuelsson (1996)
1 Samuelsson et al.
0 Samuelsson et al., 1996
1 Samuelsson et al. (1996)
----------
authors: Daniel Sleator and Davy Temperley
year: 1991
names: [('Daniel Sleator', 'Sleator'), ('Davy Temperley', 'Temperley')]
et al.: True
and : True
---
1 Sleator
0 Sleator, 1991
0 Sleator (1991)
0 Sleator et al.
0 Sleator et al., 1991
0 Sleator et al. (1991)
0 Sleator and Temperley
0 Sleator and Temperley, 1991
0 Sleator and Temperley (1991)
----------
authors: Pasi Tapanainen and Time J/irvinen
year: 1994
names: [('Pasi Tapanainen', 'Tapanainen'), ('Time J/irvinen', 'J/irvinen')]
et al.: True
and : True
---
6 Tapanainen
0 Tapanainen, 1994
0 Tapanainen (1994)
0 Tapanainen et al.
0 Tapanainen et al., 1994
0 Tapanainen et al. (1994)
0 Tapanainen and J/irvinen
0 Tapanainen and J/irvinen, 1994
0 Tapanainen and J/irvinen (1994)
----------
authors: Pasi Tapanainen
year: 1996
names: [('Pasi Tapanainen', 'Tapanainen')]
et al.: False
and : False
---
6 Tapanainen
3 Tapanainen, 1996
0 Tapanainen (1996)
----------
authors: Lucien TesniSre
year: 1959
names: [('Lucien TesniSre', 'TesniSre')]
et al.: False
and : False
---
0 TesniSre
0 TesniSre, 1959
0 TesniSre (1959)
----------
authors: Atro Voutilainen
year: 1995
names: [('Atro Voutilainen', 'Voutilainen')]
et al.: False
and : False
---
3 Voutilainen
0 Voutilainen, 1995
1 Voutilainen (1995)
----------