1515 */
1616package com .graphaware .nlp .domain ;
1717
18+ import static com .graphaware .nlp .domain .Labels .PhraseOccurrence ;
1819import static com .graphaware .nlp .domain .SentimentLabels .*;
1920import static com .graphaware .nlp .domain .Labels .Sentence ;
21+ import static com .graphaware .nlp .domain .Labels .TagOccurrence ;
22+ import static com .graphaware .nlp .domain .Properties .END_POSITION ;
2023import static com .graphaware .nlp .domain .Properties .HASH ;
2124import static com .graphaware .nlp .domain .Properties .PROPERTY_ID ;
25+ import static com .graphaware .nlp .domain .Properties .SENTENCE_NUMBER ;
26+ import static com .graphaware .nlp .domain .Properties .START_POSITION ;
2227import static com .graphaware .nlp .domain .Properties .TEXT ;
28+ import static com .graphaware .nlp .domain .Relationships .HAS_PHRASE ;
2329import static com .graphaware .nlp .domain .Relationships .HAS_TAG ;
30+ import static com .graphaware .nlp .domain .Relationships .PHRASE_OCCURRENCE_PHRASE ;
31+ import static com .graphaware .nlp .domain .Relationships .SENTENCE_PHRASE_OCCURRENCE ;
32+ import static com .graphaware .nlp .domain .Relationships .SENTENCE_TAG_OCCURRENCE ;
33+ import static com .graphaware .nlp .domain .Relationships .TAG_OCCURRENCE_TAG ;
2434import static com .graphaware .nlp .util .HashFunctions .MD5 ;
2535import java .util .ArrayList ;
2636import java .util .Collection ;
3141import org .neo4j .graphdb .Node ;
3242import org .neo4j .graphdb .Relationship ;
3343import org .neo4j .graphdb .ResourceIterator ;
44+ import org .neo4j .graphdb .Transaction ;
3445
3546public class Sentence implements Persistable {
3647
3748 public static final int NO_SENTIMENT = -1 ;
3849
3950 private final Map <String , Tag > tags ;
40- private Map <Integer , PartOfTextOccurrence <Tag >> tagOccurrences ;
41- private Map <Integer , Map <Integer , PartOfTextOccurrence <Phrase >>> phraseOccurrences ;
51+ private Map <Integer , PartOfTextOccurrence <Tag >> tagOccurrences = new HashMap <>() ;
52+ private Map <Integer , Map <Integer , PartOfTextOccurrence <Phrase >>> phraseOccurrences = new HashMap <>() ;
4253
4354 private final String sentence ;
4455 private int sentiment = NO_SENTIMENT ;
4556
4657 private boolean store = false ;
4758 private String id ;
59+ private int sentenceNumber ;
4860
49- public Sentence (String sentence , boolean store , String id ) {
61+ public Sentence (String sentence , boolean store , String id , int sentenceNumber ) {
5062 this (sentence , id );
5163 this .store = store ;
64+ this .sentenceNumber = sentenceNumber ;
5265 }
5366
5467 public Sentence (String sentence , String id ) {
5568 this .tags = new HashMap <>();
69+ this .tagOccurrences = new HashMap <>();
5670 this .sentence = sentence ;
5771 this .id = id ;
5872 }
@@ -61,11 +75,14 @@ public Collection<Tag> getTags() {
6175 return tags .values ();
6276 }
6377
64- public void addTag (Tag tag ) {
78+ public Tag addTag (Tag tag ) {
6579 if (tags .containsKey (tag .getLemma ())) {
66- tags .get (tag .getLemma ()).incMultiplicity ();
80+ Tag result = tags .get (tag .getLemma ());
81+ result .incMultiplicity ();
82+ return result ;
6783 } else {
6884 tags .put (tag .getLemma (), tag );
85+ return tag ;
6986 }
7087 }
7188
@@ -85,9 +102,6 @@ public void addTagOccurrence(int begin, int end, Tag tag) {
85102 if (begin < 0 ) {
86103 throw new RuntimeException ("Begin cannot be negative (for tag: " + tag .getLemma () + ")" );
87104 }
88- if (tagOccurrences == null ) {
89- tagOccurrences = new HashMap <>();
90- }
91105 //Will update end if already exist
92106 tagOccurrences .put (begin , new PartOfTextOccurrence <>(tag , begin , end ));
93107 }
@@ -144,30 +158,78 @@ public Phrase getPhraseOccurrence(int begin, int end) {
144158 Map <Integer , PartOfTextOccurrence <Phrase >> occurrences = phraseOccurrences .get (begin );
145159
146160 if (occurrences != null && occurrences .containsKey (end )) {
147- return occurrences .get (end ).getElement ();
161+ return occurrences .get (end ).getElement ();
148162 }
149163 return null ;
150164 }
151165
166+ public List <Phrase > getPhraseOccurrence () {
167+ List <Phrase > result = new ArrayList <>();
168+ phraseOccurrences .values ().stream ().forEach ((phraseList ) -> {
169+ phraseList .values ().stream ().forEach ((item ) -> {
170+ result .add (item .getElement ());
171+ });
172+ });
173+
174+ return result ;
175+
176+ }
177+
152178 @ Override
153179 public Node storeOnGraph (GraphDatabaseService database ) {
154- Node sequenceNode = checkIfExist (database , id );
155- if (sequenceNode == null ) {
156- Node newSentenceNode = database .createNode (Sentence );
157- newSentenceNode .setProperty (HASH , MD5 (sentence ));
158- newSentenceNode .setProperty (PROPERTY_ID , id );
159- if (store ) {
160- newSentenceNode .setProperty (TEXT , sentence );
180+ Node sentenceNode = checkIfExist (database , id );
181+ if (sentenceNode == null ) {
182+ try (Transaction tx = database .beginTx ();) {
183+ Node newSentenceNode = database .createNode (Sentence );
184+ newSentenceNode .setProperty (HASH , MD5 (sentence ));
185+ newSentenceNode .setProperty (PROPERTY_ID , id );
186+ newSentenceNode .setProperty (SENTENCE_NUMBER , sentenceNumber );
187+ if (store ) {
188+ newSentenceNode .setProperty (TEXT , sentence );
189+ }
190+ storeTags (database , newSentenceNode );
191+ storePhrases (database , newSentenceNode );
192+ sentenceNode = newSentenceNode ;
193+ assignSentimentLabel (sentenceNode );
194+ tx .success ();
161195 }
162- tags .values ().stream ().forEach ((tag ) -> {
163- Node tagNode = tag .storeOnGraph (database );
164- Relationship hasTagRel = newSentenceNode .createRelationshipTo (tagNode , HAS_TAG );
165- hasTagRel .setProperty ("tf" , tag .getMultiplicity ());
196+ } else {
197+ assignSentimentLabel (sentenceNode );
198+ }
199+ return sentenceNode ;
200+ }
201+
202+ private void storeTags (GraphDatabaseService database , Node newSentenceNode ) {
203+ tags .values ().stream ().forEach ((tag ) -> {
204+ Node tagNode = tag .storeOnGraph (database );
205+ Relationship hasTagRel = newSentenceNode .createRelationshipTo (tagNode , HAS_TAG );
206+ hasTagRel .setProperty ("tf" , tag .getMultiplicity ());
207+ });
208+ tagOccurrences .values ().stream ().forEach ((tagOccurrenceAtPosition ) -> {
209+ Node tagNode = tagOccurrenceAtPosition .getElement ().getOrCreate (database );
210+ Node tagOccurrenceNode = database .createNode (TagOccurrence );
211+ tagOccurrenceNode .setProperty (START_POSITION , tagOccurrenceAtPosition .getSpan ().first ());
212+ tagOccurrenceNode .setProperty (END_POSITION , tagOccurrenceAtPosition .getSpan ().second ());
213+ newSentenceNode .createRelationshipTo (tagOccurrenceNode , SENTENCE_TAG_OCCURRENCE );
214+ tagOccurrenceNode .createRelationshipTo (tagNode , TAG_OCCURRENCE_TAG );
215+ });
216+ }
217+
218+ private void storePhrases (GraphDatabaseService database , Node newSentenceNode ) {
219+ if (phraseOccurrences != null ) {
220+ phraseOccurrences .values ().stream ().forEach ((phraseOccurrencesAtPosition ) -> {
221+ phraseOccurrencesAtPosition .values ().stream ().forEach ((phraseOccurrence ) -> {
222+ Node phraseNode = phraseOccurrence .getElement ().storeOnGraph (database );
223+ newSentenceNode .createRelationshipTo (phraseNode , HAS_PHRASE );
224+ Node phraseOccurrenceNode = database .createNode (PhraseOccurrence );
225+ phraseOccurrenceNode .setProperty (START_POSITION , phraseOccurrence .getSpan ().first ());
226+ phraseOccurrenceNode .setProperty (END_POSITION , phraseOccurrence .getSpan ().second ());
227+ newSentenceNode .createRelationshipTo (phraseOccurrenceNode , SENTENCE_PHRASE_OCCURRENCE );
228+ phraseOccurrenceNode .createRelationshipTo (phraseNode , PHRASE_OCCURRENCE_PHRASE );
229+ //TODO: Add relationship with tags
230+ });
166231 });
167- sequenceNode = newSentenceNode ;
168232 }
169- assignSentimentLabel (sequenceNode );
170- return sequenceNode ;
171233 }
172234
173235 private void assignSentimentLabel (Node sentenceNode ) {
@@ -202,7 +264,8 @@ public static Sentence load(Node sentenceNode) {
202264 }
203265 String text = (String ) sentenceNode .getProperty (TEXT );
204266 String id = (String ) sentenceNode .getProperty (PROPERTY_ID );
205- return new Sentence (text , true , id );
267+ Integer sentenceNumber = (Integer ) sentenceNode .getProperty (SENTENCE_NUMBER );
268+ return new Sentence (text , true , id , sentenceNumber );
206269 }
207270
208271 private Node checkIfExist (GraphDatabaseService database , Object id ) {
0 commit comments