Question

Simple code using Java to create a program that uses TF-IDF algorithm. Please add step by...

Simple code using Java to create a program that uses TF-IDF algorithm. Please add step by step on how to run the code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Main.java

import java.util.Arrays;

import java.util.List;

public class Main {

public double frequency(List<String> lt, String str) {

double res = 0;

for (String str1 : lt) {

if (str.equalsIgnoreCase(str1))

res++;

}

return res / lt.size();

}

public double idf(List<List<String>> lt1, String str) {

double num = 0;

for (List<String> lt : lt1) {

for (String str1 : lt) {

if (str.equalsIgnoreCase(str1)) {

num++;

break;

}

}

}

return Math.log(lt1.size() / num);

}

public double frequencyDec(List<String> lt, List<List<String>> lt1, String str) {

return frequency(lt, str) * idf(lt1, str);

}

public static void main(String[] args) {

List<String> lt2 = Arrays.asList("Yo", "Hello", "Bro", "Sissy", "sit", "Nte");

List<String> lt3 = Arrays.asList("vit", "te", "at", "Sissy", "pro", "quo");

List<String> lt4 = Arrays.asList("Has", "persius", "diss", "dis", "simulation");

List<List<String>> lt5 = Arrays.asList(lt2, lt3, lt4);

Main m = new Main();

double freq1 = m.frequencyDec(lt2, lt5, "Sissy");

System.out.println("TF-IDF (Sissy) = " + freq1);

}

}

Output:

import java.util.Arrays; import java.util.List; Java (TM) SE Runtime Environment (build 1.8.0_31-b13) Java HotSpot (TM) 64-Bit Server VM (build 25.31-be7, mixed mode) public class Main TF-IDF (Sissy) 0.06757751801802739 public double frequency(List<string> lt, String str) [ double res 0; for (String stri : lt) f if (str.equalsIgnoreCase(str1)) res++; return res / lt.size(); public double idf (List<List<string>> 1t1, String str) double nume; for (List<string> lt : 1t1) I for (String str1 : lt) - if (str.equalsIgnoreCase (stri)) num++ break; return Math.log(1t1.size() / num); public double frequencyDec (List<String lt, List<list<string>> 1t1, String str) t return frequency(1t, str)idf(lt1, str); public static void main(String[] args) lt2 List<string» Arrays.asList(Yo, Hello,

Add a comment
Know the answer?
Add Answer to:
Simple code using Java to create a program that uses TF-IDF algorithm. Please add step by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT