It looks to me as though you want the numbers to align with the closest linework on [I assume] a particular Layer [the small black boxes], and the longer Text to align with the closest linework on another [the grey building outlines, if that's what they are]. I can only assume that in some cases if a routine were to find the closest linework element of any kind, that would be the same for both a number and a longer one.
But also, there are contradictions. Your images say that you want this:

to become this:

but your description suggests that the long string should be only slightly repositioned and given a slightly different rotation, close to where it is to begin with, rather than lined up with a completely different side of the outline. How should a routine decide to do that instead of align it with the nearest edge, and how could it decide which other edge to use? And from this starting condition:

the image says you want that changed to this:

in which the long string is not only farther from the nearest building edge linework, but perpendicular to it, rather than aligned with it. From your description, shouldn't it be more like this?

There are other examples in the images of results perpendicular to rather than aligned with nearby linework. Are there definable criteria to decide which way to do it?
The possibility of different Text justifications also complicates things. Would they all be the same?
One of the challenges would be for a routine to find the nearest linework element. I imagine a crossing-window selection that starts small relative to the Text height, and centered on the Text [or should it be centered on its insertion point?], looking for whatever it finds, and increases in size until it finds some linework-type object. But should it look for only such a thing on a certain Layer, determined by the Layer of the Text, or maybe by the content of it, if the numbers and longer strings are on the same Layer? Should it look for only a particular kind of thing [presumably only Lines or Polylines, but maybe Blocks?], or any kind of "linework" objects? What if at the same try it finds more than one? If it hits two edges of the same shape near a corner, which edge should it align with? Etc., etc.
Another is where to put a newly-aligned Text object relative to whatever edge it finds. Should the longer ones always go on the outside of the building perimeters? If the perimeters are Lines, I think a routine could have no way of determining to which side to put the Text. If they're Polylines, there are ways to figure out which side is outside, though they might get it wrong in some situations.
A very challenging problem....
Kent Cooper, AIA