如何同时在多列中拆分文本

时间:2019-03-17 12:16:20

标签: r

我有一个数据框,其中保存着有关我感兴趣的文本的全部信息(主要是词性)。数据保持与提取原始文本相同的格式(语音的每个部分用分号分隔,每个句子用换行符分隔。

我有时想查询词类的不同内容,例如获取顺序为NOUN PUNCT NOUN NOUN NOUN的句子,然后将所有与此相关的行添加到数据框中,并在另一列中告诉我该文本涉及到。我想为head_token_id获取相同的数据。

我想我必须将换行符转换为列表,获取能显示正则表达式搜索匹配项的任何行的索引,然后在相邻列中检索相同的索引,但是我不知道该怎么做。我的数据结构如下,其中包含一些假数据:

数据

  structure(list(sentence = c(",\n,Biopsies taken from top of stricture-metal marking clips in situ.\n,Diaphragmatic pinch:40cm .,She has a small hiatus hernia .\nDr. Martin, Marche Date of procedure: 2009-11-11 Endoscopist:\nDr. Sullivan, Shelby Second endoscopist: Dr. al-Basha, Mahfoodha Medications:\nExtent of Exam: GOJ Indications: Follow-up ULCER HEALING Procedure Performed: Gastroscopy (OGD) Findings:\nFentanyl 12.5mcg Midazolam 6mg Instrument: FG5\nHALO 90 done with good effect Endoscopic Diagnosis:\nHe is on Barrett's Screeling List in October 2017 at St Thomas'.\nHospital: Random NHS Foundation Trust Hospital Number: J6044658\nNo evidence of Barrett's oesophagus, short 2 cn hiatus hernia.\n,Oesophageal biopsies taken from three levels as requested.\n,OGD today to assess for ulceration/ongoing bleeding.\nPatient Name: Jargon, Victoria General Practitioner:\nPost chemo-radiotherapy stricture\n,The varices flattened well with air insufflation.\nTiny erosions at the antrum.\nWe will re-book for 2 weeks, rebanding.", 
"Date of procedure: 2008-06-15 Endoscopist:\nDr. al-Safi, Lutfiyya\nDr. Kekich, Annabelle Second endoscopist: Dr. Needham, April Medications:\nDuodenitis with a small erosion .\nEndoscopic Diagnosis:\nEsophageal candidiasis\nExtent of Exam: Pylorus Indications: Weight Loss Procedure Performed: Gastroscopy (OGD) Findings: Duodenum:\nFentanyl 125mcg Midazolam 7mg Instrument: FG6\nHospital: Random NHS Foundation Trust Hospital Number: Y6417773 Patient Name: Powell, Destiny General Practitioner:\n,STOMACH: diffuse gastritis with angiodysplasia and punctate bleeding site on greater curve mid body - no obvious ulcer- antrum scar ?,No immediate complications.\n,Z-line at: 38cm - Bravo placed at 32cm- good positionat check endoscopy."
), HospitalNumber = c(" J6044658\n", " Y6417773\n"), upos = c("PUNCT;PUNCT;PUNCT\nPUNCT;AUX;VERB;ADP;NOUN;ADP;NOUN;PUNCT;NOUN;NOUN;NOUN;ADP;NOUN;PUNCT\nPUNCT;ADJ;NOUN;NUM;NOUN;PUNCT;PUNCT;PRON;VERB;DET;ADJ;NOUN;NOUN;PUNCT\nPROPN;PROPN;PUNCT;DET;NOUN;ADP;NOUN;PUNCT;NUM;SYM;NUM;SYM;NUM;NOUN;PUNCT\nPROPN;PROPN;PUNCT;PROPN;PROPN;NOUN;PUNCT;PROPN;PROPN;PUNCT;PROPN;PUNCT;PROPN;NOUN;PUNCT\nNOUN;ADP;PROPN;PUNCT;PROPN;NOUN;PUNCT;NOUN;PUNCT;NOUN;ADP;PROPN;PROPN;PROPN;PUNCT;PROPN;PUNCT;PROPN;PUNCT;NOUN;PUNCT\nVERB;NUM;NOUN;PROPN;NUM;NOUN;NOUN;PUNCT;NUM\nSYM;NUM;ADJ;ADP;ADJ;NOUN;PROPN;PROPN;PUNCT\nPRON;AUX;ADP;PROPN;PART;NOUN;NOUN;ADP;PROPN;NUM;ADP;PROPN;PROPN;PUNCT;PUNCT\nNOUN;PUNCT;PROPN;PROPN;PROPN;PROPN;PROPN;PROPN;PUNCT;PROPN\nDET;NOUN;ADP;PROPN;PART;NOUN;PUNCT;ADJ;NUM;NOUN;NOUN;NOUN;PUNCT\nPUNCT;ADJ;NOUN;VERB;ADP;NUM;NOUN;SCONJ;VERB;PUNCT\nVERB;NOUN;PART;VERB;ADP;NOUN;PUNCT;ADJ;NOUN;PUNCT\nADJ;NOUN;PUNCT;PROPN;PUNCT;PROPN;PROPN;PROPN;PUNCT\nVERB;NOUN;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7;1;1;1\n3;1;0;8;6;8;8;3;3\n7;7;7;7;4;7;0;9;7;9;13;13;7;13;7\n0;1;6;3;3;7;1;7;7;7\n2;0;6;6;4;2;2;11;10;11;12;2;2\n3;3;0;3;7;7;4;9;4;3\n0;1;4;1;6;4;9;9;6;1\n2;0;2;8;4;4;4;2;2\n0;4;4;5;1\n4;3;4;0;4;8;8;4;4\n2;0;5;5;2;2\n3;3;0;5;3;8;8;5;3;3;3", 
"0;3;1;1;1;7;5;9;7;1;10\n0;1;1;1;1;1\n0;1;1;1;1;1;1;1;8;1;12;1;1\n0;5;5;5;1;1\n0;1;1\n2;0\n0;3;1;1;6;1;6;10;10;6;17;13;11;15;13;13;1;17;17;17\n2;0;6;3;6;2;2;2\n0;1;6;3;3;7;1;1;12;12;12;8;12;12;18;18;16;14;1\n2;0;2;2;4;7;5;9;4;9;10;16;14;16;16;10;2;23;23;22;22;23;2;2;2;28;28;2;2\n3;3;9;7;7;7;3;7;10;0;18;13;15;15;16;18;18;10;10"
)), row.names = c(NA, -2L), class = "data.frame")

如果我正在寻找NOUN PUNCT NOUN NOUN NOUN(与原始数据帧一起),则需要的输出

Match_sentence
1. ",Biopsies taken from top of stricture-metal marking clips in situ."
2. "Extent of Exam: Pylorus Indications: Weight Loss Procedure Performed: Gastroscopy (OGD) Findings: Duodenum:"

1 个答案:

答案 0 :(得分:0)

这是使用data.tablegrepl的方法。这将为您提供您要查找的两条记录。

library(data.table)
setDT(df)[grepl("NOUN;PUNCT;NOUN;NOUN;NOUN;",upos)]

使用data.frame,可以起作用:

df[grepl("NOUN;PUNCT;NOUN;NOUN;NOUN;",df$upos),]

数据

df <- structure(list(sentence = c(",\n,Biopsies taken from top of stricture-metal marking clips in situ.\n,Diaphragmatic pinch:40cm .,She has a small hiatus hernia .\nDr. Martin, Marche Date of procedure: 2009-11-11 Endoscopist:\nDr. Sullivan, Shelby Second endoscopist: Dr. al-Basha, Mahfoodha Medications:\nExtent of Exam: GOJ Indications: Follow-up ULCER HEALING Procedure Performed: Gastroscopy (OGD) Findings:\nFentanyl 12.5mcg Midazolam 6mg Instrument: FG5\nHALO 90 done with good effect Endoscopic Diagnosis:\nHe is on Barrett's Screeling List in October 2017 at St Thomas'.\nHospital: Random NHS Foundation Trust Hospital Number: J6044658\nNo evidence of Barrett's oesophagus, short 2 cn hiatus hernia.\n,Oesophageal biopsies taken from three levels as requested.\n,OGD today to assess for ulceration/ongoing bleeding.\nPatient Name: Jargon, Victoria General Practitioner:\nPost chemo-radiotherapy stricture\n,The varices flattened well with air insufflation.\nTiny erosions at the antrum.\nWe will re-book for 2 weeks, rebanding.", 
                            "Date of procedure: 2008-06-15 Endoscopist:\nDr. al-Safi, Lutfiyya\nDr. Kekich, Annabelle Second endoscopist: Dr. Needham, April Medications:\nDuodenitis with a small erosion .\nEndoscopic Diagnosis:\nEsophageal candidiasis\nExtent of Exam: Pylorus Indications: Weight Loss Procedure Performed: Gastroscopy (OGD) Findings: Duodenum:\nFentanyl 125mcg Midazolam 7mg Instrument: FG6\nHospital: Random NHS Foundation Trust Hospital Number: Y6417773 Patient Name: Powell, Destiny General Practitioner:\n,STOMACH: diffuse gastritis with angiodysplasia and punctate bleeding site on greater curve mid body - no obvious ulcer- antrum scar ?,No immediate complications.\n,Z-line at: 38cm - Bravo placed at 32cm- good positionat check endoscopy."
), HospitalNumber = c(" J6044658\n", " Y6417773\n"), upos = c("PUNCT;PUNCT;PUNCT\nPUNCT;AUX;VERB;ADP;NOUN;ADP;NOUN;PUNCT;NOUN;NOUN;NOUN;ADP;NOUN;PUNCT\nPUNCT;ADJ;NOUN;NUM;NOUN;PUNCT;PUNCT;PRON;VERB;DET;ADJ;NOUN;NOUN;PUNCT\nPROPN;PROPN;PUNCT;DET;NOUN;ADP;NOUN;PUNCT;NUM;SYM;NUM;SYM;NUM;NOUN;PUNCT\nPROPN;PROPN;PUNCT;PROPN;PROPN;NOUN;PUNCT;PROPN;PROPN;PUNCT;PROPN;PUNCT;PROPN;NOUN;PUNCT\nNOUN;ADP;PROPN;PUNCT;PROPN;NOUN;PUNCT;NOUN;PUNCT;NOUN;ADP;PROPN;PROPN;PROPN;PUNCT;PROPN;PUNCT;PROPN;PUNCT;NOUN;PUNCT\nVERB;NUM;NOUN;PROPN;NUM;NOUN;NOUN;PUNCT;NUM\nSYM;NUM;ADJ;ADP;ADJ;NOUN;PROPN;PROPN;PUNCT\nPRON;AUX;ADP;PROPN;PART;NOUN;NOUN;ADP;PROPN;NUM;ADP;PROPN;PROPN;PUNCT;PUNCT\nNOUN;PUNCT;PROPN;PROPN;PROPN;PROPN;PROPN;PROPN;PUNCT;PROPN\nDET;NOUN;ADP;PROPN;PART;NOUN;PUNCT;ADJ;NUM;NOUN;NOUN;NOUN;PUNCT\nPUNCT;ADJ;NOUN;VERB;ADP;NUM;NOUN;SCONJ;VERB;PUNCT\nVERB;NOUN;PART;VERB;ADP;NOUN;PUNCT;ADJ;NOUN;PUNCT\nADJ;NOUN;PUNCT;PROPN;PUNCT;PROPN;PROPN;PROPN;PUNCT\nVERB;NOUN;PUNCT;NOUN;NOUN\nPUNCT;DET;NOUN;VERB;ADV;ADP;NOUN;NOUN;PUNCT\nDET;NOUN;ADP;DET;NOUN;PUNCT\nPRON;AUX;VERB;PUNCT;NOUN;ADP;NUM;NOUN;PUNCT;VERB;PUNCT", 
                                                              "NOUN;ADP;NOUN;PUNCT;NUM;SYM;NUM;SYM;NUM;NOUN;PUNCT\nPROPN;PROPN;PUNCT;PROPN;PUNCT;PROPN\nPROPN;PROPN;PUNCT;PROPN;PROPN;NOUN;PUNCT;PROPN;PROPN;PUNCT;PROPN;NOUN;PUNCT\nNOUN;ADP;DET;ADJ;NOUN;PUNCT\nPROPN;PROPN;PUNCT\nADJ;NOUN\nNOUN;ADP;PROPN;PUNCT;ADJ;NOUN;PUNCT;NOUN;NOUN;NOUN;VERB;PUNCT;PROPN;PUNCT;PROPN;PUNCT;PROPN;PUNCT;PROPN;PUNCT\nINTJ;VERB;PROPN;NUM;NOUN;NOUN;PUNCT;NUM\nNOUN;PUNCT;PROPN;PROPN;PROPN;PROPN;PROPN;PROPN;PUNCT;PROPN;PROPN;PROPN;PUNCT;PROPN;PUNCT;PROPN;PROPN;PROPN;PUNCT\nPUNCT;VERB;PUNCT;VERB;NOUN;ADP;NOUN;CCONJ;VERB;VERB;NOUN;ADP;ADJ;NOUN;X;NOUN;PUNCT;DET;ADJ;NOUN;PUNCT;NOUN;NOUN;PUNCT;PUNCT;DET;ADJ;NOUN;PUNCT\nADJ;PUNCT;NOUN;ADP;PUNCT;NUM;NOUN;PUNCT;PROPN;VERB;ADP;NUM;NOUN;PUNCT;ADJ;NOUN;NOUN;NOUN;PUNCT"
), head_token_id = c("0;0;0\n3;3;0;5;3;11;9;9;11;11;5;13;11;3\n5;3;5;5;9;5;9;9;0;13;13;13;9;9\n0;1;1;5;1;7;5;1;14;11;9;9;14;1;1\n0;1;1;1;1;1;1;1;8;8;8;8;8;1;1\n0;3;1;1;6;1;6;10;10;6;13;13;20;13;14;14;18;14;18;6;1\n0;3;7;3;6;7;1;1;1\n3;1;0;8;6;8;8;3;3\n7;7;7;7;4;7;0;9;7;9;13;13;7;13;7\n0;1;6;3;3;7;1;7;7;7\n2;0;6;6;4;2;2;11;10;11;12;2;2\n3;3;0;3;7;7;4;9;4;3\n0;1;4;1;6;4;9;9;6;1\n2;0;2;8;4;4;4;2;2\n0;4;4;5;1\n4;3;4;0;4;8;8;4;4\n2;0;5;5;2;2\n3;3;0;5;3;8;8;5;3;3;3", 
                     "0;3;1;1;1;7;5;9;7;1;10\n0;1;1;1;1;1\n0;1;1;1;1;1;1;1;8;1;12;1;1\n0;5;5;5;1;1\n0;1;1\n2;0\n0;3;1;1;6;1;6;10;10;6;17;13;11;15;13;13;1;17;17;17\n2;0;6;3;6;2;2;2\n0;1;6;3;3;7;1;1;12;12;12;8;12;12;18;18;16;14;1\n2;0;2;2;4;7;5;9;4;9;10;16;14;16;16;10;2;23;23;22;22;23;2;2;2;28;28;2;2\n3;3;9;7;7;7;3;7;10;0;18;13;15;15;16;18;18;10;10"
)), row.names = c(NA, -2L), class = "data.frame")
相关问题