Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3760932
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:32:52+00:00 2026-05-19T10:32:52+00:00

Let’s say I have the following text (from wiki ): Java is a programming

  • 0

Let’s say I have the following text (from wiki):

Java is a programming language
originally developed by James Gosling
at Sun Microsystems (which is now a
subsidiary of Oracle Corporation) and
released in 1995 as a core component
of Sun Microsystems’ Java platform.
The language derives much of its
syntax from C and C++ but has a
simpler object model and fewer
low-level facilities. Java
applications are typically compiled to
bytecode (class file) that can run on
any Java Virtual Machine (JVM)
regardless of computer architecture.
Java is a general-purpose, concurrent,
class-based …

And I would like to parse out “java” and “programming” matches into google style result like this:

Java is a programming language
originally developed by James Gosling
at Sun Microsystems … Java
applications are typically compiled to
bytecode (class file) that can run on
any Java Virtual Machine (JVM)…

  1. What tools could I use and how should I used them to get the above result. Commons, Lucene, Compass?

  2. If there is an algorithm that will highlight the keywords and take care of cutting the strings and adding “…” at the end, please share it.

  3. How do you decide how many and which keywords to display in search result preview?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:32:53+00:00Added an answer on May 19, 2026 at 10:32 am

    I don’t know of any tools that will help with this, but I can offer an algorithm that will give you fairly decent results. *Edit: OP asked for sample code for the index. I am using a Trove TIntObjectHashMap to store this information, but you could do the same with a Java HashMap.

    Step 1: Search the text for each of the search words and make an index of the offset within the text that each of them appears.

    TIntObjectHashMap<String> matchIndex = new TIntObjectHashMap<String>();
    // for each word or other string to highlight
    // find each instance of each word in the string
    // this is pseudocode -v
    for (each instance of String searchString appearing at index int x)
      matchIndex.put(x, searchString);
    

    Step 2: Go through each combination of pairs of indices in Step 1 and record the number of characters between indices and number of hits.

    // class to hold a match
    private class Match implements Comparable {
      private int x1, x2;
      private int hitCount;
    
      public Match(int x1, int x2, int hitCount); // does the obvious
    
      private double sortValue() {
        return (double) hitCount / Math.abs(x1, x2);
      }  
    
      @Override
      public int compareTo(Match m) {
        double diff = this.sortValue() - m.sortValue();
        if (diff == 0.0) return 0;
        return (diff < 0.0) ? -1 : 1;
      }
    }
    
    // go through every combination of keys (string offsets) and record them
    // the treeset will automatically sort the results
    TreeSet<Match> matches = new TreeSet<Match>();
    int[] keys = matchIndex.keys();
    for (int x1 = 0; x1 < keys.length; x1++)
      for (int x2 = x1 + 1; x2 < keys.length; x2++)
        matches.put(new Match(keys[x1],
                              keys[x2] + matchIndex.get(keys[x2]).length(),
                              1 + x2 - x1));
    

    Step 3: Take the list generated in Step 2 and sort them by number of hits per character of length.

    // nicely done by the TreeSet
    

    Step 4: Start at the top of the list in Step 3 and mark each item as being included. Be sure to combine overlapping results into one larger result. Stop when the next item would push the total length of the string over 255 (or so) characters.

    Step 5: Display each of the selected items in Step 4, in order, with “…” between them. Be sure to include whatever markup is necessary to highlight the actual search words themselves within each item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.