BrightSide Workbench Full Report + Source Code
Synonyms.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.entities.db;
20 
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import javax.persistence.Column;
26 import javax.persistence.ElementCollection;
27 import javax.persistence.Entity;
28 import javax.persistence.FetchType;
29 import javax.persistence.GeneratedValue;
30 import javax.persistence.GenerationType;
31 import javax.persistence.Id;
32 import org.turro.string.Strings;
33 import org.turro.elephant.db.ElephantPU;
34 import org.turro.elephant.db.SQLHelper;
35 import org.turro.elephant.db.WhereClause;
36 import org.turro.jpa.Dao;
37 import org.turro.sql.SQLUtil;
38 
43 @Entity
44 public class Synonyms implements java.io.Serializable {
45 
46  @Id
47  @GeneratedValue(strategy=GenerationType.IDENTITY)
48  @Column(name="IDENTIFIER")
49  private Long id;
50 
51  @ElementCollection(fetch=FetchType.EAGER)
52  private Set<String> words = new HashSet<>();
53 
54  public Long getId() {
55  return id;
56  }
57 
58  public void setId(Long id) {
59  this.id = id;
60  }
61 
62  public Set<String> getWords() {
63  return words;
64  }
65 
66  public void setWords(Set<String> words) {
67  this.words = words;
68  }
69 
70  /* Helpers */
71 
72  public boolean isEmpty() {
73  return words.isEmpty();
74  }
75 
76  public String getWordsString() {
77  return String.join(",", words);
78  }
79 
80  /* Statics */
81 
82  public static List<Synonyms> getSynonymsByWords(String phrase) {
83  ArrayList<Synonyms> synonyms = new ArrayList<>();
84  if(!Strings.isBlank(phrase)) {
85  for(String word : phrase.split("\\s+")) {
86  synonyms.addAll(getSynonyms(new ElephantPU(), word));
87  }
88  }
89  return synonyms;
90  }
91 
92  public static List<Synonyms> getSynonyms(String word) {
93  return getSynonyms(new ElephantPU(), word);
94  }
95 
96  public static List<Synonyms> getSynonyms(String word, int max) {
97  return getSynonyms(new ElephantPU(), word, max);
98  }
99 
100  public static List<Synonyms> getSynonyms(Dao dao, String word) {
101  return getSynonyms(dao, word, SQLUtil.MAX_SYNONIMS + 1);
102  }
103 
104  public static List<Synonyms> getSynonyms(Dao dao, String word, int max) {
105  WhereClause wc = new WhereClause();
106  wc.addClause("select s from Synonyms s");
107  wc.addClause("left join s.words w");
108  wc.addClause("where w like :word");
109  wc.addNamedValue("word", SQLHelper.convertToLike(word));
110  return max == 0 ? dao.getResultList(wc) : dao.getResultList(wc, max);
111  }
112 
113 }
static String convertToLike(String value)
Definition: SQLHelper.java:33
void addNamedValue(String name, Object value)
static List< Synonyms > getSynonyms(Dao dao, String word, int max)
Definition: Synonyms.java:104
static List< Synonyms > getSynonyms(String word)
Definition: Synonyms.java:92
static List< Synonyms > getSynonyms(String word, int max)
Definition: Synonyms.java:96
void setWords(Set< String > words)
Definition: Synonyms.java:66
static List< Synonyms > getSynonymsByWords(String phrase)
Definition: Synonyms.java:82
static List< Synonyms > getSynonyms(Dao dao, String word)
Definition: Synonyms.java:100
static final int MAX_SYNONIMS
Definition: SQLUtil.java:34