DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • AI-Powered Knowledge: LlamaIndex and Apache Tika for Enterprises
  • From OCR Bottlenecks to Structured Understanding
  • How I Built an AI Portal for Document Q and A, Summarization, Transcription, Translation, and Extraction
  • How to Merge HTML Documents in Java

Trending

  • Effective Exception Handling in Java and Spring Boot Applications
  • When Incentives Sabotage Product Strategy
  • Self-Supervised Learning Techniques
  • Top Load Balancing Algorithms: Choosing the Right Strategy

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.

By 
Unni Mana user avatar
Unni Mana
·
Jul. 26, 17 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
7.9K Views

Join the DZone community and get the full member experience.

Join For Free

Apache 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.

Apache Tika Extract Document

Opinions expressed by DZone contributors are their own.

Related

  • AI-Powered Knowledge: LlamaIndex and Apache Tika for Enterprises
  • From OCR Bottlenecks to Structured Understanding
  • How I Built an AI Portal for Document Q and A, Summarization, Transcription, Translation, and Extraction
  • How to Merge HTML Documents in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: