问题
what I want to achieve is that I want to obtain the context of an acronym. Can you help me pls with the regular expression?
I am looping over the text (String) and looking for dots, after match I am trying to get the context of the particular found acronym, so that I can do some other processing after that, but I cant get the context. I need to take at least 5 words before and 5 words after the acronym.
//Pattern to match each word ending with dot
Pattern pattern = Pattern.compile("(\\w+)\\b([.])");
Matcher matchDot = pattern.matcher(textToCorrect);
while (matchDot.find()) {
System.out.println("zkratka ---"+matchDot.group()+" ---");
//5 words before and after tha match = context
// Matcher matchContext = Pattern.compile("(.{25})("+matchDot.group()+")(.{25})").matcher(textToCorrect);
Pattern patternContext = Pattern.compile("(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,10}"+matchDot.group()+"(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,10}");
Matcher matchContext = patternContext.matcher(textToCorrect);
if (matchContext.find()) {
System.out.println("context: "+matchContext.group()+" :");
// System.out.println("context: "+matchContext.group(1)+" :");
// System.out.println("context: "+matchContext.group(2)+" :");
}
}
Example:
input: Some 84% of Paris residents see fighting pol. as a priority and 54% supported a diesel ban in the city by 2020, according a poll carried out for the Journal du Dimanche.
output:
1-st regex will find pol.
2-nd regex will find "of Paris residents see fighting pol. as a priority and 54%"
Another example with more text
I need to loop through this once and every time I match an acronym to get the context of this particular acronym. After that I am processing some datamining. Here's the original text
neklidná nemocná, vyš. je možné provést pouze nativně
Na mozku je patrna hyperdenzita v počátečním úseku a. cerebri media vlevo, vlevo se objevuje již smazání hranic mezi bazálními ganglii a okolní bílou hmotou a mírná difuzní hypointenzita v periventrikulární bílé hmotě. Kromě těchto čerstvých změn jsou patrné staré postmalatické změny temporálně a parietookcipitálně vlevo. Oboustranně jsou patrné vícečetné vaskulární mikroléze v centrum semiovale bilat. Nejsou známky nitrolebního krvácení. skelet kalvy orientačně nihil tr.
Z á v ě r: Známky hyperakutní ischemie v povodí ACM vlevo, staré postmalatickéé změny T,P a O vlevo, vaskulární mikroléze v centrum semiovale bilat.
CT AG: vyš. po bolu k.l..
Po zklidnění nemocné se podařilo provést CT AG. Na krku je naznačený kinkink na ACC vlevo a ACI vlevo pod bazí. Kalcifikace v karotických sifonech nepůsobí hemodynamicky významné stenozy. Intrakraniálně je patrný konický uzávěr operkulárního úseku a. cerebri media vlevo pro parietální lalok. Ostatní nález na intrakraniálním tepenném řečišti je v mezích normy.
Z á v ě r: uzávěr operkulárního úseku a. cerebri media vlevo.
Of course if it matches end of sentence is ok for me :-) The question is to find all the acronyms even if they are before new line (\n)
回答1:
((?:[\w!@#$%&*]+\s+){5}([\w!@#$%&*]+\.)(?:\s+[\w!@#$%&*]+){5})
Try this.See demo.
https://regex101.com/r/aQ3zJ3/9
回答2:
I would try this out:
(?:\w+\W+){5}((?:\w.?)+)(?:\w+\W+){5}
Though natural language processing with regular expressions cannot be accurate.
来源:https://stackoverflow.com/questions/27346080/regex-java-word-context