# Use AI to interpret XML?

My question is more about "is it possible?" and "is this the right approach?" So let me explain what my idea is:

I think about a system which gets XML documents in various structures but with essentially the same data structure in it. For the example, lets assume each document contains data about one or more persons. So the AI would recognize a name. Somewhere else in the document there is the post address of our fictional person. The AI should now "see" the address and conclude, it belongs to our person. Anywhere else, there is a phone number in the document. Again, our AI should see the connection between our person and this phone number.

This wouldn't be a job for an AI if there wasn't a catch. If the task was merely to find and map strings like addresses and phone numbers, we could simply use a regex to match our "target strings". The catch in this scenario would be this: the XML document might contain other data, which does not belong to our person but is a valid phone number for example and thus will match an regex.

So the big question is: Would it be possible for an AI to learn this and if yes, with which framework would someone create such an AI?

Sample XML document:

<?xml version="1.0" encoding="utf-8" ?>
<document>
<data>
<foo>
<bar>
<person>
<name>John Doe</name>
</person>
</bar>
<street>Main street 1</street>
<city>1111 Twilight town</city>
<country>sample country</country>
<phone>+123 123 123</phone>
</foo>
<foo>
<bar>
<person>
<name>Jane Doe</name>
</person>
</bar>
<city>4521 Traverse town</city>
<country>sample country</country>
<phone>+123 412123</phone>
</foo>
</data>
<creator>
<!-- Note: While this looks like a valid person, -->
<!-- this data should not be matched by the AI -->
<name>Sam Smith</name>
<office>
<city>4521 Traverse town</city>
<country>sample country</country>
</office>
<phone>+123 555 555</phone>
</creator>
</document>

• The first question I'd ask is "can a human make the distinction you're asking about, and how?" – mindcrime Sep 1 '17 at 18:08
• Welcome to AI! (I very much like mindcrime's comment, b/c I know how I'd go about validating the information, and suspect an AI could do it much more quickly.) – DukeZhou Sep 1 '17 at 18:27
• I'm guessing that what you really want is an AI that, given an arbitrary XML schema, can guess which elements/attributes the data you want is going to be encoded in. So to train your AI, you would need a large number of different schemas with the correct elements already tagged. – antlersoft Sep 1 '17 at 18:55
• @mindcrime The human can make the distinction between, because the data belonging together would be more or less under the same parent. I updated my question with a sample XML. As you can see, the data about one person is below the <foo> parent. For humans it's clear that each <foo> represents one person. – DBX12 Sep 2 '17 at 11:19
• @antlersoft That was my initial idea. I think I have enough XML schemas at hand (otherwise I will generate some via script from sample data). My line of thought was "Humans can learn to differentiate that. AI can learn like humans so AI should be able to learn this too". But then the question "how to start and are there frameworks?" rose, resulting in this question, – DBX12 Sep 2 '17 at 11:23

XML, HTML and less formal languages all respond quite nicely to being transformed or interrogated within a graph framework. XML and HTML are particularly useful in that they conform strictly to a tree-structure. That means that any good data components can be measured in terms of tree-distance to any other "good" data components.

If you extract your regex-friendly terms and keep track of where within their tree they are found, you may be able to cast those values into a general document-space vector (it might only need to be one-dimensional), allowing you to identify clusters of "good" vs anomalous sections of "bad" data based on a simple distance metric, or anomaly detection algorithm - say an isolation-forest that runs on information density for example.

This depends on your data, and how much of it you can find, ideally already tagged up containing good vs bad.

If you're looking to scrape reliable address-contents, then yes, you're likely to score hits on names, addresses, postcodes and phone-numbers all appearing as tightly connected clustered groups, all within one or two nodes-distance from one another.

Meanwhile, an annotation containing a phone number lodged somewhere else is less likely to be a match.

Different documents will have different threshold densities, and differing anomaly to conformant ratios, so you'd have a task on your hands to figure out some way to automatically tune your parameters on any given document set.

In the past, I've tried doing this against html by flattening all the content into a single string of text and a similar approach yielded half-decent results, but if you're looking at XML, it's fair to expect the structure to yield more information.

• This is a great answer derived from practical, real world experience. Welcome to AI! – DukeZhou Sep 4 '17 at 20:02
• Thanks for the answer.Do you have any good graph frameworks as starting point? I'm pretty new to machine learning, so a few pointers into the right direction would be highly appreciated. – DBX12 Sep 30 '17 at 12:41
• I use python a lot for things like this, and networkx is a fantastic graph-framework. I might use of of the xml parsing modules discussed here to parse and marshal the content, perhaps pushing that into a networkx graph for the added analytical flexibility that might give you. – Thomas Kimber Sep 30 '17 at 14:34

You would need to define 'frames', 'templates', or sets of data belonging together to form an address or other kind of data, with typical labels. So phone or tel etc would indicate a phone number, provided that their content also looks like a phone number. That's how you as a human recognise it. So you encode your domain knowledge as entities with possible attributes. Then you try and match the attributes and recognise which entity they belong to. You could have several entities with a shared subset of attributes (like a company or a person, which would both have an address). There would be other clues to tell you which it is. If name ends in "Ltd." or "Co", then it'll be a company, for example.

So you mix heuristics for identifying attributes, templates of which attributes combine to form an entity class you want to identify, and then pick the one that best matches. If you have several entities where all that's filled is phone, then you can't really tell what it is and would discard it. In your example, name matches various entities, but office would not be a valid attribute for a person (unless you decided it was). An oversimplistic heuristic might think "Sam Smith" is a company, which presumably has an office attribute, so you need to be careful with how you design your templates.

A criterion for putting attributes together could be that they are in the same subtree of the data structure. The exact definition really depends on the data and the types of information you want to extract from it.

So yes, it is definitely possible. I'm not really sure which framework, but it should be fairly trivial to code this in a programming language of your choice.