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
Please enter at least three characters to search
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Google Cloud Document AI Basics
  • Thumbnail Generator Microservice for PDF in Spring Boot
  • Build a Local AI-Powered Document Summarization Tool
  • Designing a Blog Application Using Document Databases

Trending

  • Java Virtual Threads and Scaling
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  • AI, ML, and Data Science: Shaping the Future of Automation

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

  • Google Cloud Document AI Basics
  • Thumbnail Generator Microservice for PDF in Spring Boot
  • Build a Local AI-Powered Document Summarization Tool
  • Designing a Blog Application Using Document Databases

Partner Resources

×

Comments
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!