String manipulation patterns

Just wondering if there is a set of design patterns for complex string manipulations?

Basically the problem I'm trying to solve is that I need to read a line, for example:

"[name_of_kicker] is looking at the punch, but under some real pressure from the players [name_of_defending_team] he gets a punch [length_of_kick], but he is in full contact."

or

"[name_of_kicker] gets the ball from [name_of_passer] and fires the bomb. [name_of_kicker] really made good contact, he was given a couple of [name_of_attacking_team] chasers enough time to get under the ball when he goes down."

And replace each "tag" with a possible value and check if the string is equal to the other string.

So, for example, any tag that represents the player, I need to replace any of the 22 string values ​​that represent the player. But I should also be able to make sure that I went through every combination of players for the various tags that I can find in the line. NOTE. Tags listed in the above two samples are not the only signs, there are many others that may appear in any sentence.

I tried to create a loading of nested loops to go through a collection of players, etc. and try to replace tags every time, but with many tag features, I just created one nested loop for another, and it became unmanageable, and I also suspect it is inefficient, since I need to scroll over 1000 base lines, as above, and replace markup tags with players, etc. for each of them ...

So, are there any String manipulation patterns that I could learn, or does anyone have any possible solutions to solve such a problem.

+6
source share
3 answers

First, to answer your question.

Just wondering if there is a set of design patterns for complex string manipulations?

Not really. There are several methods, but they are hardly qualified as design patterns. Two methods that spring should consider are template expansion and pattern matching.

What you are doing / proposing to do now is a form of template expansion. However, typical template engines do not support the combinatorial extension you are trying to do, and as you expect, this will be an inefficient way to solve your problem.

The best technique would be pattern matching. Let's look at your first example and turn it into a template:

"(Ronaldino | Maradonna | Peter Shilton | Jackie Charlton) hopes for a blow, but is under some real pressure from the players (Everton Real Madrid Adelaide United). He receives ([0-9] + meter), but he is in full contact . "

What I did was insert all possible alternatives into a pseudo-pattern to turn it into a regular expression. Now I can compile this regular expression in java.util.Pattern and use it to match with your list of other strings.


Having said that, if you are trying to do this to "analyze" the text, I do not assess your chances of success. I think it would be better for you to go down the NLP route.

+1
source

What you are describing is a bit like what template engines are used for.

Two popular for Java:

But there are many , many more, of course.

+1
source

MY two cents. As you said, "I just created one nested for loop inside another, and it became unmanageable",

You are looking in the wrong direction, my friend has a whole universe of solutions to the problems that you face, just know how the rule mechanism. There are various types of rule mechanisms (business rule mechanisms, web template engines, etc.), but for the above requirement, I propose business rule mechanisms.

It is impossible to comment which one to use, since it depends on

  • Multithreading.
  • Open source / commercial.
  • Load factor / processing time, etc.

Hope this helps

http://ratakondas.blogspot.in/2012/06/business-rules-engines-white-paper.html [read the summary section, it gives the best advice.]

https://en.wikipedia.org/wiki/Business_rules_engine#Types_of_rule_engines https://en.wikipedia.org/wiki/Comparison_of_web_template_engines

Welcome to the world of rule engines :)

0
source

Source: https://habr.com/ru/post/896583/


All Articles