Advertisement
lameski

Word Vector

Aug 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. import java.util.stream.IntStream;
  4.  
  5. /**
  6. * Word vectors test
  7. */
  8. class Word
  9. {
  10. String word;
  11.  
  12. public Word(String word)
  13. {
  14. this.word = word;
  15. }
  16. }
  17. class Vector
  18. {
  19. static final Vector DEFAULT = new Vector(Arrays.asList(5,5,5,5,5));
  20. static final Vector IDENTITY = new Vector(Arrays.asList(0,0,0,0,0));
  21. List<Integer> numbers;
  22.  
  23. public Vector(List<Integer> numbers)
  24. {
  25. this.numbers = numbers;
  26. }
  27.  
  28. public int max()
  29. {
  30. return numbers.stream().mapToInt(Integer::intValue)
  31. .max().orElse(0);
  32. }
  33.  
  34. public int size()
  35. {
  36. return numbers.size();
  37. }
  38.  
  39. public int at(int i)
  40. {
  41. return numbers.get(i);
  42. }
  43.  
  44. public Vector sum(Vector other)
  45. {
  46. return new Vector(IntStream.range(0, other.size())
  47. .map(i -> at(i) + other.at(i))
  48. .boxed()
  49. .collect(Collectors.toList()));
  50. }
  51. }
  52. class WordVectors
  53. {
  54. Map<Word,Vector> wordVectorMap;
  55. List<Vector> vectors;
  56. Comparator<Word> comp = Comparator.comparing(i -> i.word);
  57. public WordVectors(String[] words, List<List<Integer>>vectors)
  58. {
  59. wordVectorMap = new TreeMap<>(comp);
  60. IntStream.range(0, words.length)
  61. .forEach(i -> wordVectorMap.put(new Word(words[i]), new Vector(vectors.get(i))));
  62. }
  63.  
  64. public void readWords(List<String> words)
  65. {
  66. vectors = words.stream()
  67. .map(word -> wordVectorMap.getOrDefault(new Word(word), Vector.DEFAULT))
  68. .collect(Collectors.toList());
  69. }
  70.  
  71. public List<Integer> slidingWindow(int n)
  72. {
  73. return IntStream.range(0, vectors.size() - n +1)
  74. .mapToObj(i ->
  75. vectors.subList(i, i+n).stream()
  76. .reduce(Vector.IDENTITY, Vector::sum))
  77. .map(Vector::max)
  78. .collect(Collectors.toList());
  79.  
  80. }
  81. }
  82.  
  83.  
  84. public class WordVectorsTest {
  85. public static void main(String[] args) {
  86. Scanner scanner = new Scanner(System.in);
  87. int n = scanner.nextInt();
  88. scanner.nextLine();
  89. String[] words = new String[n];
  90. List<List<Integer>> vectors = new ArrayList<>(n);
  91. for (int i = 0; i < n; ++i) {
  92. String line = scanner.nextLine();
  93. String[] parts = line.split("\\s+");
  94. words[i] = parts[0];
  95. List<Integer> vector = Arrays.stream(parts[1].split(":"))
  96. .map(Integer::parseInt)
  97. .collect(Collectors.toList());
  98. vectors.add(vector);
  99. }
  100. n = scanner.nextInt();
  101. scanner.nextLine();
  102. List<String> wordsList = new ArrayList<>(n);
  103. for (int i = 0; i < n; ++i) {
  104. wordsList.add(scanner.nextLine());
  105. }
  106. WordVectors wordVectors = new WordVectors(words, vectors);
  107. wordVectors.readWords(wordsList);
  108. n = scanner.nextInt();
  109. List<Integer> result = wordVectors.slidingWindow(n);
  110. System.out.println(result.stream()
  111. .map(Object::toString)
  112. .collect(Collectors.joining(",")));
  113. scanner.close();
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement