Keyword Density Analysis Using Java

This code counts number of repeated words in a paragraph, count its number of appearance and calculates the density of the word.

Tested with NetBeans 8.0

JSP Page to get Input from User:

//getting input from user
<html>
<head>
<title> Keyword Density Analysis </title>
<style>
.btn {
                height: 30px;
                width: 100px;
                background: #3498db;
                background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
                background-image: -moz-linear-gradient(top, #3498db, #2980b9);
                background-image: -ms-linear-gradient(top, #3498db, #2980b9);
                background-image: -o-linear-gradient(top, #3498db, #2980b9);
                background-image: linear-gradient(to bottom, #3498db, #2980b9);
                -webkit-border-radius: 28;
                -moz-border-radius: 28;
                border-radius: 28px;
                font-family: Arial;
                color: #ffffff;
                font-size: 17px;
                padding: 5px 20px 10px 20px;
                text-decoration: none;
}
</style>
</head>
<body>
<center>
        <form action="serv1">
            <h3><center>Paste your Text</center></h3>
            <textarea name="ta1" rows="10" cols="50" placeholder="paste your text here"></textarea>
            <br><br>
            <Button type="submit" class="btn" name="subs">Submit</button>
            <br><br>
        </form>
    </center>
</body>
</html>

Servlet:

if(request.getParameter("subs")!=null)
            {
               RequestDispatcher rd=request.getRequestDispatcher("/show.jsp");
               rd.forward(request, response);    
            }

Show.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Results Page</title>
    </head>
    <body>
        <% String textars=request.getParameter("ta1");
                 String textars1=textars.toLowerCase();
                 String string2[]=textars1.split("[(' ').-]"); 
                 HashMap<String, Integer> uniques = new HashMap<String, Integer>();
                    for (String word : string2)
                    {
            // ignore words 2 or less characters long
                    if (word.length() <= 2)
                    {
                        
                        continue;
                    }
           
                    Integer existingCount = uniques.get(word);
                    uniques.put(word, (existingCount == null ? 1 : (existingCount + 1)));
                    }
                    Set<Map.Entry<String, Integer>> uniqueSet = uniques.entrySet();
                    boolean first = true; %>
                   <% double alen=string2.length; %>
                   <table border="3">
                        <tr class="td">
                            <td width="150px"><center> Word</center> </td>
                            <td width="150px"><center> Repeated Times</center> </td>
                            <td width="150px"><center> Density</center> </td>
                        </tr>
                        <tr>
                    <%
                    for (Map.Entry<String, Integer> entry : uniqueSet)
                    {
                        double d;
                        if (entry.getValue() > 1)
                        { %>
                            <% d=(entry.getValue()/alen)*100; %>
                                <td> <center><% out.println(entry.getKey());%> </center></td>
                                <td> <center><% out.println(entry.getValue()); %> </center></td>
                                <td> <center><% out.println(d); %> </center></td>
                     </tr> 
                     <% } }%>
                      
                   </table> 
                      <table>
                          <tr>
                              <td> Total Words: <%=alen%></td>
                          </tr>
                      </table>
    </body>
</html>

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *