vitaly/bibliography-parser
0
1import io2import logging3import timeit4from typing import Optional5 6import gradio as gr7import numpy as np8import spacy9from spacy import displacy10from spacy.matcher import Matcher11from spacy.training import Example12 13from bib_tokenizers import create_references_tokenizer14from schema import spankey_sentence_start, tags_ent15 16# 1.0.117# pip install https://huggingface.co/vitaly/en_bib_references_trf/resolve/main/en_bib_references_trf-any-py3-none-any.whl18MODEL = "en_bib_references_trf"19 20logging.basicConfig(level=logging.INFO)21log = logging.getLogger(__name__)22_LOG_STR_LEN = 1623 24nlp = spacy.load(MODEL)25# return score for each token:26# with threshold set to zero each suggested span is returned, and span == token,27# because suggester is configured to suggest spans with len(span) == 1:28# [components.spancat.suggester]29# @misc = "spacy.ngram_suggester.v1"30# sizes = [1]31nlp.get_pipe("spancat").cfg["threshold"] = 0.0 # see )32log.info("spancat config: %s", nlp.get_pipe("spancat").cfg)33 34 35def create_bib_item_start_scorer_for_doc(doc):36 37 span_group = doc.spans[spankey_sentence_start]38 assert not span_group.has_overlap39 assert len(span_group) == len(40 doc41 ), "Check suggester config and the spancat threshold to make sure that spangroup contains single token span for each token"42 43 def scorer(token_index_in_doc, fuzzy_in_tokens=(0, 0)):44 i = token_index_in_doc45 46 span = span_group[i] # our spans are one token length47 assert i == span.start48 49 # fuzzines might improve fault tolerance if the model made a small mistake,50 # e.g., if a number from prev line is classified as "citation number",51 # see example at https://www.deeplearningbook.org/contents/bib.html52 # if fuzzy == (0,0), it return score for the selected span only53 return span, max(54 span_group.attrs["scores"][i]55 for i in range(i - fuzzy_in_tokens[0], i + fuzzy_in_tokens[1] + 1)56 if i >= 0 and i < len(doc)57 )58 59 return scorer60 61 62nlp_blank = spacy.blank("en")63nlp_blank.tokenizer = create_references_tokenizer()(nlp_blank)64# nlp_blank.tokenizer = nlp.tokenizer65 66 67def _tokenize_test(nlp):68 _text = """MNRAS, 216, 51P69Comito"""70 tokens = [f"'{t}'" for t in nlp(_text)]71 log.info("tokens: %s", tokens)72 return tokens73 74 75assert len(_tokenize_test(nlp)) == len(76 _tokenize_test(nlp_blank)77), "Check that the same tokenizer is used for both: trained model (in its config) and nlp_blank"78 79 80def _token_index_in_norm_doc(81 token_index_in_target_doc: int, alignment_data: np.ndarray82) -> Optional[int]:83 84 index_in_norm_doc = np.where(alignment_data == token_index_in_target_doc)85 if type(index_in_norm_doc) == tuple:86 index_in_norm_doc = index_in_norm_doc[0] # depends on numpy version...87 88 if index_in_norm_doc.size > 0:89 return index_in_norm_doc[0].item()90 91 92def split_up_references(93 references: str, is_eol_mode=True, ner=True, nlp=nlp, nlp_blank=nlp_blank94):95 """96 Args:97 references - a references section, ideally without a header98 nlp - a model that splits up references into separate sentences99 nlp_blank - a blank nlp with the same tokenizer/language100 """101 102 _timeit_start = timeit.default_timer()103 log.info(104 "start processing: '%s...'",105 references[: _LOG_STR_LEN if len(references) > _LOG_STR_LEN else references],106 )107 108 target_doc = nlp_blank(references)109 target_tokens_idx = {110 offset: t.i for t in target_doc for offset in range(t.idx, t.idx + len(t))111 }112 f = io.StringIO(references)113 lines = [line for line in f]114 115 # disable unused components to speedup inference && parse normalized referenences116 disable = []117 if is_eol_mode:118 disable.append("senter")119 else:120 disable.append("spancat")121 if not ner:122 disable.append("ner")123 with nlp.select_pipes(disable=disable):124 # normalization applied: strip lines and remove any extra space between lines125 norm_doc = nlp(" ".join([line.strip() for line in lines if line.strip()]))126 127 # extremely useful spacy API for alignment normalized and target(created from non-modified input) docs128 example = Example(target_doc, norm_doc)129 130 # copy ner annotations:131 for label in tags_ent:132 target_doc.vocab[label]133 target_doc.ents = example.get_aligned_spans_y2x(norm_doc.ents)134 135 # set senter annotations136 if is_eol_mode:137 alignment_data = example.alignment.y2x.data138 139 # use SpanCat scores to set sentence boundaries on the target doc140 # init senter annotations141 for i, t in enumerate(target_doc):142 t.is_sent_start = i == 0143 144 token_scorer = create_bib_item_start_scorer_for_doc(norm_doc)145 146 def target_doc_token_scorer(token_index_in_target_doc):147 index_in_norm_doc = _token_index_in_norm_doc(148 token_index_in_target_doc, alignment_data149 )150 if index_in_norm_doc is not None:151 span, score = token_scorer(index_in_norm_doc)152 # print(span, score, index_in_norm_doc)153 return score154 return 0.0155 156 threshold = 0.5157 158 char_offset = 0159 for line_num, line in enumerate(lines):160 if not line.strip():161 # ignore empty line162 char_offset += len(line)163 continue164 165 token_index_in_target_doc = target_tokens_idx[char_offset]166 # scroll to the first non-space (if the line starts from space):167 while (168 token_index_in_target_doc < len(target_doc)169 and target_doc[token_index_in_target_doc].is_space170 ):171 token_index_in_target_doc += 1172 173 score = target_doc_token_scorer(token_index_in_target_doc)174 if score > threshold:175 target_doc[target_tokens_idx[char_offset]].is_sent_start = True176 177 char_offset += len(line)178 179 _level_off_references(target_doc, target_doc_token_scorer)180 else:181 # copy SentenceRecognizer annotations from doc without '\n' to the target doc182 sent_start = example.get_aligned("SENT_START")183 for i, t in enumerate(target_doc):184 target_doc[i].is_sent_start = sent_start[i] == 1185 186 log.info(187 "done: '%s...', elapsed: %s",188 references[: _LOG_STR_LEN if len(references) > _LOG_STR_LEN else references],189 timeit.default_timer() - _timeit_start,190 )191 return target_doc192 193 194def _level_off_references(doc, token_scorer):195 """196 Problem:197 if a model that predicts the reference boundaries was .99 accurate,198 the success rate for real papers would be still relative low199 given that a typical bibliography consists of dozens of references.200 201 This function attemps to detect references that contain more lines than202 others and split them somehow... The result will not neccessary be better.203 """204 205 lengths = np.array([len(ref.text.strip().split("\n")) for ref in doc.sents])206 median = np.median(lengths)207 mean = np.mean(lengths)208 sigma = np.std(209 lengths210 ) # read this: https://stackoverflow.com/questions/27600207/why-does-numpy-std-give-a-different-result-to-matlab-std211 212 log.info("median: %s, mean: %s, sigma: %s", median, mean, sigma)213 if sigma == 0.0:214 return215 216 sent_starts = []217 matcher = Matcher(nlp.vocab)218 pattern = [219 # {"TEXT": {"REGEX": "^(.*)(\\n)+(.*)$"}, "IS_SPACE": True},220 {"TEXT": {"REGEX": "^(.*\\n.*)+$"}, "IS_SPACE": True},221 {"IS_SPACE": True, "OP": "*"},222 {"IS_SPACE": False},223 ]224 matcher.add("line_start", [pattern])225 for n, ref in enumerate(doc.sents):226 # print([f"'{t}'" for t in ref])227 surprising = (lengths[n] - mean) / sigma228 if surprising > 1.6:229 log.info("surprising: %s: %s", surprising, ref.text[:_LOG_STR_LEN])230 scores = [token_scorer(t.i) for t in ref]231 median_score = np.median(scores)232 # check each first non-space token on each line233 start = None # next reference start is we decided to splip up the ref span234 for _, eol, token_i_after_eol in matcher(ref):235 i = token_i_after_eol - 1236 # using the predicted spancat score237 log.info(238 "line start: token=%s, score=%s, median_score=%s, ahead=%s",239 ref[i],240 scores[i],241 median_score,242 len(ref[token_i_after_eol:]),243 )244 # TODO: play with softmax temperature: find a way to get activations:245 # here we have an activated neuron in the softmax input, but corresponding sofmax output is still too low246 if scores[i] > 10 * median_score and len(ref[token_i_after_eol:]) > 10:247 sent_starts.append(ref[i])248 start = i249 continue250 251 # using ner output:252 # an edge case if newx line starts with citation number of namnes and253 # pref libes already contain names and title254 before_eol_ents = [255 ent.label_ for ent in ref[0 if start is None else start : eol].ents256 ]257 # 2 entities after eol, if any258 after_eol_ents = [ent.label_ for ent in ref[eol:].ents][:2]259 if (260 set(before_eol_ents) & set(["issued", "title", "container-title"])261 and set(before_eol_ents) & set(["family", "given"])262 and set(after_eol_ents)263 & set(264 [265 "family",266 "given",267 "citation-number",268 "citation-label",269 ]270 )271 ):272 log.info("splitting up using NER predictions: %s", ref[i])273 sent_starts.append(ref[i])274 start = i275 276 for t in sent_starts:277 t.is_sent_start = True278 279 280def text_analysis(text: str, more_than_one_ref_per_line: bool):281 282 if not text or not text.strip():283 return "<div style='max-width:100%; overflow:auto; color:grey'><p>Unparsed Bibliography Section is empty</p></div>"284 285 doc_with_linebreaks = split_up_references(286 text, is_eol_mode=not more_than_one_ref_per_line, nlp=nlp, nlp_blank=nlp_blank287 )288 289 html = ""290 options = {291 "ents": tags_ent,292 "colors": {293 "citation-number": "yellow",294 "citation-label": "yellow",295 "family": "DeepSkyBlue",296 "given": "LightSkyBlue",297 "title": "PeachPuff",298 "container-title": "Moccasin",299 "publisher": "PaleTurquoise",300 "issued": "Gold",301 },302 }303 for i, sent in enumerate(doc_with_linebreaks.sents):304 bib_item_doc = sent.as_doc()305 ref = displacy.render(bib_item_doc, style="ent", options=options)306 html += f"<tr><td>{i}</td><td>{ref}</td></tr>"307 308 html = (309 """<div style='max-width:100%; max-height:720px; overflow:auto'>310 <style>table {311 font-family: arial, sans-serif;312 border-collapse: collapse;313 width: 100%;314 }315 316 td, th {317 border: 1px solid #b0b0b0;318 text-align: left;319 padding: 8px;320 }321 322 tr:nth-child(even) {323 background-color: #f2f2f2;324 }</style>"""325 + "<table><tr><th>Index</th><th>Parsed Reference</th></tr>"326 + html327 + "</table>"328 + "</div>"329 )330 331 return html332 333 334gr.close_all()335demo = gr.Blocks()336with demo:337 338 textbox = gr.components.Textbox(339 label="Unparsed Bibliography Section",340 placeholder="Enter bibliography here...",341 lines=20,342 )343 more_than_one_ref_per_line = gr.components.Checkbox(344 value=False,345 label="My bibliography may contain more than one reference per line - the model will make a prediction for each token: more predictions, more chances to make a mistake",346 )347 html = gr.components.HTML(label="Parsed Bib Items")348 textbox.change(349 fn=text_analysis, inputs=[textbox, more_than_one_ref_per_line], outputs=[html]350 )351 more_than_one_ref_per_line.change(352 fn=text_analysis, inputs=[textbox, more_than_one_ref_per_line], outputs=[html]353 )354 355 gr.Examples(356 examples=[357 [ # https://arxiv.org/pdf/1910.01108v4.pdf358 """Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova. Bert: Pre-training of deep bidirectional transformers for language understanding. In NAACL-HLT, 2018.359Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, and Ilya Sutskever. Language models are unsupervised multitask learners. 2019.360Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar S. Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke S. Zettlemoyer, and Veselin Stoyanov. Roberta: A robustly optimized bert pretraining approach. ArXiv, abs/1907.11692, 2019.361Roy Schwartz, Jesse Dodge, Noah A. Smith, and Oren Etzioni. Green ai. ArXiv, abs/1907.10597, 2019. Emma Strubell, Ananya Ganesh, and Andrew McCallum. Energy and policy considerations for deep learning in362nlp. In ACL, 2019.363Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser,364and Illia Polosukhin. Attention is all you need. In NIPS, 2017.365Thomas Wolf, Lysandre Debut, Victor Sanh, Julien Chaumond, Clement Delangue, Anthony Moi, Pierric Cistac, Tim Rault, Rémi Louf, Morgan Funtowicz, and Jamie Brew. Transformers: State-of-the-art natural language processing, 2019.366Cristian Bucila, Rich Caruana, and Alexandru Niculescu-Mizil. Model compression. In KDD, 2006.367Geoffrey E. Hinton, Oriol Vinyals, and Jeffrey Dean. Distilling the knowledge in a neural network. ArXiv,368abs/1503.02531, 2015.369Yukun Zhu, Ryan Kiros, Richard S. Zemel, Ruslan Salakhutdinov, Raquel Urtasun, Antonio Torralba, and Sanja Fidler. Aligning books and movies: Towards story-like visual explanations by watching movies and reading books. 2015 IEEE International Conference on Computer Vision (ICCV), pages 19–27, 2015.370Alex Wang, Amanpreet Singh, Julian Michael, Felix Hill, Omer Levy, and Samuel R. Bowman. Glue: A multi-task benchmark and analysis platform for natural language understanding. In ICLR, 2018.371Matthew E. Peters, Mark Neumann, Mohit Iyyer, Matt Gardner, Christopher Clark, Kenton Lee, and Luke Zettlemoyer. Deep contextualized word representations. In NAACL, 2018.372Alex Wang, Ian F. Tenney, Yada Pruksachatkun, Katherin Yu, Jan Hula, Patrick Xia, Raghu Pappagari, Shuning Jin, R. Thomas McCoy, Roma Patel, Yinghui Huang, Jason Phang, Edouard Grave, Najoung Kim, Phu Mon Htut, Thibault F’evry, Berlin Chen, Nikita Nangia, Haokun Liu, Anhad Mohananey, Shikha Bordia, Nicolas Patry, Ellie Pavlick, and Samuel R. Bowman. jiant 1.1: A software toolkit for research on general-purpose text understanding models. http://jiant.info/, 2019.373Andrew L. Maas, Raymond E. Daly, Peter T. Pham, Dan Huang, Andrew Y. Ng, and Christopher Potts. Learning word vectors for sentiment analysis. In ACL, 2011.374Pranav Rajpurkar, Jian Zhang, Konstantin Lopyrev, and Percy Liang. Squad: 100, 000+ questions for machine comprehension of text. In EMNLP, 2016."""375 ],376 [ # https://isg.beel.org/blog/2019/12/10/giant-the-1-billion-annotated-synthetic-bibliographic-reference-string-dataset-for-deep-citation-parsing-pre-print/377 """Crossref, https://www.crossref.org378A JavaScript implementation of the Citation Style Language (CSL),379https://github.com/Juris-M/citeproc-js380Official repository for Citation Style Language (CSL),381https://github.com/citation-style-language/styles382Anzaroot, S., McCallum, A.: A New Dataset for fine-Grained Citation field Extraction (2013)383Councill, I.G., Giles, C.L., Kan, M.Y.: Parscit: an open-source crf reference string parsing package. In: LREC. vol. 8, pp. 661–667 (2008)384Fedoryszak, M., Tkaczyk, D., Bolikowski, L.: Large scale citation matching using apache hadoop. In: International Conference on Theory and Practice of Digital Libraries. pp. 362–365. Springer (2013)385Hetzner, E.: A simple method for citation metadata extraction using hidden markov models. In: Proceedings of the 8th ACM/IEEE-CS joint conference on Digital libraries. pp. 280–284. ACM (2008)386Lample, G., Ballesteros, M., Subramanian, S., Kawakami, K., Dyer, C.: Neural architectures for named entity recognition. arXiv preprint arXiv:1603.01360 (2016)387Lopez, P.: Grobid: Combining automatic bibliographic data recognition and term extraction for scholarship publications. In: International conference on theory and practice of digital libraries. pp. 473–474. Springer (2009)388Ma, X., Hovy, E.: End-to-end sequence labeling via bi-directional lstm-cnns-crf. arXiv preprint arXiv:1603.01354 (2016)389Mikolov, T., Sutskever, I., Chen, K., Corrado, G.S., Dean, J.: Distributed representations of words and phrases and their compositionality. In: Advances in neural information processing systems. pp. 3111–3119 (2013)390Ojokoh, B., Zhang, M., Tang, J.: A trigram hidden markov model for metadata extraction from heterogeneous references. Information Sciences 181(9), 1538–1551391(2011)392Okada, T., Takasu, A., Adachi, J.: Bibliographic component extraction using support vector machines and hidden markov models. In: International Conference on393Theory and Practice of Digital Libraries. pp. 501–512. Springer (2004)394Prasad, A., Kaur, M., Kan, M.Y.: Neural parscit: a deep learning-based reference string parser. International Journal on Digital Libraries 19(4), 323–337 (2018)395Rodrigues Alves, D., Colavizza, G., Kaplan, F.: Deep reference mining from scholarly literature in the arts and humanities. Frontiers in Research Metrics and Analytics 3, 21 (2018)396Tkaczyk, D., Collins, A., Sheridan, P., Beel, J.: Machine learning vs. rules and out-of-the-box vs. retrained: An evaluation of open-source bibliographic reference and citation parsers. In: Proceedings of the 18th ACM/IEEE on joint conference on digital libraries. pp. 99–108. ACM (2018)397Tkaczyk, D., Szostek, P., Dendek, P.J., Fedoryszak, M., Bolikowski, L.: Cermine– automatic extraction of metadata and references from scientific literature. In: 2014 11th IAPR International Workshop on Document Analysis Systems. pp. 217–221. IEEE (2014)398Yin, P., Zhang, M., Deng, Z., Yang, D.: Metadata extraction from bibliographies using bigram hmm. In: International Conference on Asian Digital Libraries. pp.399310–319. Springer (2004)400Zhang, X., Zou, J., Le, D.X., Thoma, G.R.: A structural svm approach for reference parsing. BMC bioinformatics 12(3), S7 (2011)"""401 ],402 [ # https://arxiv.org/pdf/1706.03762.pdf403 """[28] Romain Paulus, Caiming Xiong, and Richard Socher. A deep reinforced model for abstractive404summarization. arXiv preprint arXiv:1705.04304, 2017.405[29] Slav Petrov, Leon Barrett, Romain Thibaux, and Dan Klein. Learning accurate, compact,406and interpretable tree annotation. In Proceedings of the 21st International Conference on407Computational Linguistics and 44th Annual Meeting of the ACL, pages 433–440. ACL, July4082006.409[30] Ofir Press and Lior Wolf. Using the output embedding to improve language models. arXiv preprint410arXiv:1608.05859, 2016.411[31] Rico Sennrich, Barry Haddow, and Alexandra Birch. Neural machine translation of rare words412with subword units. arXiv preprint arXiv:1508.07909, 2015.413[32] Noam Shazeer, Azalia Mirhoseini, Krzysztof Maziarz, Andy Davis, Quoc Le, Geoffrey Hinton,414and Jeff Dean. Outrageously large neural networks: The sparsely-gated mixture-of-experts415layer. arXiv preprint arXiv:1701.06538, 2017.416[33] Nitish Srivastava, Geoffrey E Hinton, Alex Krizhevsky, Ilya Sutskever, and Ruslan Salakhutdi-417nov. Dropout: a simple way to prevent neural networks from overfitting. Journal of Machine418Learning Research, 15(1):1929–1958, 2014.419[34] Sainbayar Sukhbaatar, Arthur Szlam, Jason Weston, and Rob Fergus. End-to-end memory420networks. In C. Cortes, N. D. Lawrence, D. D. Lee, M. Sugiyama, and R. Garnett, editors,421Advances in Neural Information Processing Systems 28, pages 2440–2448. Curran Associates,422Inc., 2015.423[35] Ilya Sutskever, Oriol Vinyals, and Quoc VV Le. Sequence to sequence learning with neural424networks. In Advances in Neural Information Processing Systems, pages 3104–3112, 2014.425[36] Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, Jonathon Shlens, and Zbigniew Wojna.426Rethinking the inception architecture for computer vision. CoRR, abs/1512.00567, 2015.427[37] Vinyals & Kaiser, Koo, Petrov, Sutskever, and Hinton. Grammar as a foreign language. In428Advances in Neural Information Processing Systems, 2015.429[38] Yonghui Wu, Mike Schuster, Zhifeng Chen, Quoc V Le, Mohammad Norouzi, Wolfgang430Macherey, Maxim Krikun, Yuan Cao, Qin Gao, Klaus Macherey, et al. Google’s neural machine431translation system: Bridging the gap between human and machine translation. arXiv preprint432arXiv:1609.08144, 2016."""433 ],434 [435 """[Ein05] Albert Einstein. Zur Elektrodynamik bewegter K ̈orper. (German)436[On the electrodynamics of moving bodies]. Annalen der Physik,437322(10):891–921, 1905. 438[GMS93] Michel Goossens, Frank Mittelbach, and Alexander Samarin. The LATEX Companion. Addison-Wesley, Reading, Massachusetts, 1993. 439[Knu] Donald Knuth. Knuth: Computers and typesetting."""440 ],441 [442 """[1] B. Foxman, R. Barlow, H. D'Arcy, B. Gillespie, and J. D. Sobel, "Urinary tract infection: self-reported incidence and associated costs," Ann Epidemiol, vol. 10, pp. 509-515, 2000. [2] B. Foxman, "Epidemiology of urinary tract infections: incidence, morbidity, and economic costs," Am J Med, vol. 113, pp. 5-13, 2002. [3] L. Nicolle, "Urinary tract infections in the elderly," Clin Geriatr Med, vol. 25, pp. 423-436, 2009."""443 ],444 [445 """Barth, Fredrik, ed.446 1969 Ethnic groups and boundaries: The social organization of culture difference. Oslo: Scandinavian University Press.447Bondokji, Neven448 2016 The Expectation Gap in Humanitarian Operations: Field Perspectives from Jordan. Asian Journal of Peace Building 4(1):1-28.449Bourdieu, Pierre450 The forms of capital In Handbook of Theory and Research for the Sociology of Education. J. Richardson, ed. Pp. 241-258. New York: Greenwood Publishesrs.451Carrion, Doris452 2015 Are Syrian Refguees a Security Threat to the MIddle East Vol. 2016. London Reuters.453CFR454 2016 The Global Humanitarian Regime: Priorities and Prospects for Reform. Council on Foerign Relations, International Institutues and Global Governance Program"""455 ],456 [457 """(2) Hofmann, M.H. et al. Aberrant splicing caused by single nucleotide polymorphism c.516G>T [Q172H], a marker of CYP2B6*6, is responsible for decreased expression and activity of CYP2B6 in liver. J Pharmacol Exp Ther 325, 284-92 (2008).458(3) Zanger, U.M. & Klein, K. Pharmacogenetics of cytochrome P450 2B6 (CYP2B6): advances on polymorphisms, mechanisms, and clinical relevance. Front Genet 4, 24 (2013).459(4) Holzinger, E.R. et al. Genome-wide association study of plasma efavirenz pharmacokinetics in AIDS Clinical Trials Group protocols implicates several CYP2B6 variants. Pharmacogenet Genomics 22, 858-67 (2012).460"""461 ],462 ],463 inputs=textbox,464 )465demo.launch()466 