How to Extract Phone Numbers Using Apache Tika
There are many instances in which we may need to extract phone numbers in real-time applications. Learn how to do so with Apache Tika.
Join the DZone community and get the full member experience.
Join For FreeApache Tika is an open-source framework for extracting metadata from different file types. This framework has been widely used in many enterprise-grade applications. Since it supports wide varieties of documents for content handling, it is used in machine learning, artificial intelligence, natural language processing based applications, etc. In this article, I will show you one of the coolest features of Apache Tika.
Last time, I had difficulties detecting phone numbers from different types of documents. The challenge was that I had to use different parsers to parse and extract the phone numbers. For example, to extract phone numbers from a Word document, I had to use a library that supports Word. Also, I cannot use the same library or logic to parse a PDF file. Ultimately, I need to maintain different libraries for different document types, which, as you can image, can lead to many issues.
The Solution
First, download the latest version of Apache Tika and create a project. Add Tika-related JAR files to the classpath. Create a class called PhoneNumberDetector
.
Metadata
This class provides detailed information about attributes of the content. Using this object, we can query the attributes. Initialize this class in your main()
method.
Metadata metadata = new Metadata();
Parser
Apache Tika offers lots of parsers on different scenarios (such as on Word documents). In our case, we are going to parse the Word document to detect phone numbers automatically using the AutoDetectParser
class.
Parser parser = new AutoDetectParser();
The next step is to use some content handlers. We will use the PhoneExtractingContentHandler
class. This class will examine any characters for phone numbers before passing them to the underlying handler.
PhoneExtractingContentHandler handler = new PhoneExtractingContentHandler(new BodyContentHandler(), metadata);
Next, load the actual document and parse it.
InputStream stream = new BufferedInputStream(PhoneNumberDetector.class.getResourceAsStream("test.docx"));
parser.parse(stream, handler, metadata, new ParseContext());
We have parsed the content with the help of AutoDetect parser object. Now it is time to get all the phone numbers detected. Before looking for the result, we need to add some phone numbers into the document.
+1 8494823161
work Phone: 080 25451244, 8494823115, 12132121
I have added some phone numbers in the Word document, as shown above.
Lastly, we need to get these phone numbers detected for the display. For that, we will call below code:
String[] numbers = metadata.getValues("phonenumbers");
So what happened?
The parser parsed the above test.docx
and detected the phone numbers present in the document automatically. The above line of code will return the phone numbers detected from the document if there are any using the phonenumbers
property in the Metadata
object.
The output of phone numbers detected:
phone numbers: [8494823161, 8025451244, 8494823115]
But we can see only three phone numbers. The reason is that 12132121
is not a valid phone number. It will be rejected by the PhoneExtracingContentHandler
class. However, please be informed that Apache Tika will not validate the correctness of phone numbers present in the document.
In this way, you can detect and extract phone numbers very easily from any document. Please find the code below for your perusal:
Parser parser = new AutoDetectParser();
Metadata metadata = new Metadata();
PhoneExtractingContentHandler handler = new PhoneExtractingContentHandler(new BodyContentHandler(), metadata);
InputStream stream = new BufferedInputStream(PhoneNumberDetector.class.getResourceAsStream("test.docx"));
parser.parse(stream, handler, metadata, new ParseContext());
String[] numbers = metadata.getValues("phonenumbers");
stream.close()
return Arrays.asList(numbers);
Conclusion
There are many instances in which we need to extract phone numbers in real-time applications. One such scenario may be parsing a resume, extracting phone numbers, and sending automated messages. You can explore its API to know more about it.
Opinions expressed by DZone contributors are their own.
Comments