|
| 1 | +package us.narin.summarizer; |
| 2 | + |
| 3 | +import kr.bydelta.koala.data.Morpheme; |
| 4 | +import kr.bydelta.koala.data.Sentence; |
| 5 | +import kr.bydelta.koala.data.Word; |
| 6 | +import kr.bydelta.koala.twt.Tagger; |
| 7 | +import org.jgrapht.alg.interfaces.VertexScoringAlgorithm; |
| 8 | +import org.jgrapht.alg.scoring.PageRank; |
| 9 | +import org.jgrapht.graph.DefaultWeightedEdge; |
| 10 | +import org.jgrapht.graph.SimpleDirectedWeightedGraph; |
| 11 | +import scala.collection.Iterator; |
| 12 | +import us.narin.summarizer.utils.ListUtils; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.FileNotFoundException; |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +public class Summarizer { |
| 19 | + |
| 20 | + public static void main(String[] args) throws FileNotFoundException { |
| 21 | + |
| 22 | + Tagger tagger = new Tagger(); |
| 23 | + String content = new Scanner(new File("./test.txt")).useDelimiter("\\Z").next(); |
| 24 | + List<String> keywords = Arrays.asList("๋ถํ", "๋ฏธ์ฌ์ผ"); |
| 25 | + String[] splitSentences = content.split("[.?!\n]"); |
| 26 | + Map<String, List<String>> parsedSentence = new LinkedHashMap<>(); |
| 27 | + |
| 28 | + SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> graph = |
| 29 | + new SimpleDirectedWeightedGraph<> |
| 30 | + (DefaultWeightedEdge.class); |
| 31 | + |
| 32 | + System.out.println(Arrays.toString(splitSentences)); |
| 33 | + |
| 34 | + for (String sentence : splitSentences) { |
| 35 | + sentence = sentence.trim(); |
| 36 | + System.out.println(sentence); |
| 37 | + Sentence analyzedSentence = tagger.tagSentence(sentence); |
| 38 | + Iterator iterator = analyzedSentence.words().iterator(); |
| 39 | + List<String> detectedNouns = new ArrayList<>(); |
| 40 | + |
| 41 | + while (iterator.hasNext()) { |
| 42 | + Word word = (Word) iterator.next(); |
| 43 | + Iterator wordIterator = word.iterator(); |
| 44 | + |
| 45 | + while (wordIterator.hasNext()) { |
| 46 | + Morpheme morpheme = (Morpheme) wordIterator.next(); |
| 47 | +// if (morpheme.isNoun() || morpheme.isPredicate()) { |
| 48 | + if (morpheme.isNoun()) { |
| 49 | +// WTF - KoalaNLP์ Morpheme ํด๋์ค์์ ์์ํ ๋จ์ด๋ง ๊ฐ์ ธ์ค๋ ํจ์๊ฐ ์๋ค. |
| 50 | + String plainWord = morpheme.toString().split("/")[0]; |
| 51 | + System.out.println(morpheme); |
| 52 | + detectedNouns.add(plainWord); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + parsedSentence.put(sentence, detectedNouns); |
| 57 | + } |
| 58 | + |
| 59 | + System.out.println(parsedSentence); |
| 60 | + |
| 61 | + |
| 62 | + for (Map.Entry<String, List<String>> entry : parsedSentence.entrySet()) { |
| 63 | + String key = entry.getKey(); |
| 64 | + graph.addVertex(key); |
| 65 | + List<String> value = entry.getValue(); |
| 66 | + |
| 67 | + System.out.println(key); |
| 68 | + System.out.println(value); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + parsedSentence.entrySet().stream().sorted((o1, o2) -> { |
| 73 | + System.out.println("=== COMPARING START ==="); |
| 74 | + List<String> intersection = ListUtils.intersection(o1.getValue(), o2.getValue()); |
| 75 | + List<String> union = ListUtils.union(o1.getValue(), o2.getValue()); |
| 76 | + |
| 77 | + float similarity = (float) intersection.size() / (float) union.size(); |
| 78 | + System.out.println(String.format("'%s'์ '%s'์ ์ ์ฌ๋๋ %f", o1.getKey(), o2.getKey(), similarity)); |
| 79 | + System.out.println("=== COMPARING END ==="); |
| 80 | + |
| 81 | + if (similarity > 0) { |
| 82 | + DefaultWeightedEdge e = graph.addEdge(o1.getKey(), o2.getKey()); |
| 83 | + graph.setEdgeWeight(e, similarity); |
| 84 | + } |
| 85 | + |
| 86 | + return 0; |
| 87 | + }).forEach(System.out::println); |
| 88 | + System.out.println(graph); |
| 89 | + |
| 90 | + VertexScoringAlgorithm<String, Double> pr = new PageRank<>(graph); |
| 91 | + |
| 92 | + pr.getScores().entrySet().stream() |
| 93 | + .map(entity -> { |
| 94 | +// TODO ๊ฒ์ ํค์๋๊ฐ ํฌํจ๋ ๋ฌธ์ฅ์ ๊ฒฝ์ฐ ํ์ด์ง ๋ญํฌ์ ๊ฐ์ ๋ํด์ค์ผ ํจ. |
| 95 | + List<String> mutualKeywords = ListUtils.intersection(parsedSentence.get(entity.getKey()), keywords); |
| 96 | + if (!mutualKeywords.isEmpty()) { |
| 97 | + System.out.println((Double) (entity.getValue() + (entity.getValue() + mutualKeywords.size() / 100))); |
| 98 | + } |
| 99 | +// entity.setValue((Double) (entity.getValue() + (entity.getValue() + mutalKeywords.size() / 100))); |
| 100 | + return entity; |
| 101 | + }) |
| 102 | + .sorted((o1, o2) -> o1.getValue() < o2.getValue() ? 1 : -1) |
| 103 | + .forEach(System.out::println); |
| 104 | + |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | + |
0 commit comments