Issue
I have for loop that after block close , cull logger to show some log but don't know what happening here after this it's call every time for loop have length , It's weird! in this case the
**NOTE : IF use System.out.println(); its work fine and show one time.
log.log(Level.INFO , "Translated Setences from "+countOfTranslated+" / "+sentenses.size()+" successfully");
is after the for loop block close and it doesn't make any sense. if some one know something share. look at my full code :
package mehritco.ir.megnatis.institute.reflex.nlp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import org.json.JSONObject;
import edu.stanford.nlp.coref.data.CorefChain;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.ie.util.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.semgraph.*;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.util.CoreMap;
import mehritco.ir.megnatis.institute.reflex.instagram.Application;
import mehritco.ir.megnatis.institute.reflex.instagram.repository.RepositoryTranslate;
import mehritco.ir.megnatis.tools.file.BasicLocation;
import java.util.*;
/**
* https://stanfordnlp.github.io/CoreNLP/human-languages.html
* https://stanfordnlp.github.io/stanfordnlp/models.html
* @author Megnatis
*
*/
public class TextAnalyzer {
public static String unTranslatedText = """
درما در اینجا هستیم . سلام خوبی؟ وقت بخیر چیکار میکنی
تسلیت
راستی یادت نره بیای . دوست دارم عزیزم
چه میشه کرد؟
اونجایی
""";
/**
* @link https://stanfordnlp.github.io/CoreNLP/ssplit.html#sentence-splitting-from-java
* @param paragraph Max-length is 1024 byte
* @return
*/
public static ArrayList<String> breakParagraphToSentence(String paragraph){
ArrayList<String> sentenses = new ArrayList<String>();
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit");
/**
* @Link https://stanfordnlp.github.io/CoreNLP/ssplit.html#options
* Whether to treat newlines as sentence breaks. This property has 3 legal values.
* “always” means that a newline is always a sentence break
* (but there still may be multiple sentences per line).
*/
props.setProperty("ssplit.newlineIsSentenceBreak", "always");
/**
* @link https://stanfordnlp.github.io/CoreNLP/tokenize.html
* Java character offsets (stored in the CharacterOffset{Begin,End}Annotation)
* are in terms of 16-bit char’s, with Unicode characters outside the basic
* multilingual prime encoded as two chars via a surrogate pair.
* if need emoji use true .
*/
props.setProperty("tokenize.codepoint", "true");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
CoreDocument document = new CoreDocument(paragraph);
pipeline.annotate(document);
for (CoreSentence sentence : document.sentences()) {
sentenses.add(sentence.text());//split sentence with newLine char
}
return sentenses;
}
/**
* For create Paragraph from TranslatedText
* @param sentenses
* @param log
* @return String translated Paragraph
*/
@Nullable
public static String getParagraphInTranslated(ArrayList<String> sentenses , Logger log) {
//Check null sentence or not
if(sentenses == null || sentenses.size() < 0 || sentenses.isEmpty()) {
return null ;
}
String allSentenceToGetter = "";
int countOfTranslated = 0;
for (String sentense : sentenses) {
RepositoryTranslate repositoryTranslate = new RepositoryTranslate();
Logger logger = Application.setupLog(BasicLocation.getBaseFileDir()+"logs");
JSONObject translateJson = repositoryTranslate.translate(sentense, "fa", "en", logger);
boolean isTranslated = translateJson.optBoolean(RepositoryTranslate.IS_TRANSLATE);
if(isTranslated) {
String textFromTranslate = translateJson.optString(RepositoryTranslate.TRANSLATE_TEXT);
allSentenceToGetter += (textFromTranslate+"\n") ;
countOfTranslated++;
}
}
log.log(Level.INFO , "Translated Setences from "+countOfTranslated+" / "+sentenses.size()+" successfully");
return allSentenceToGetter;
}
/**
* @param sentenses Use breakParagraphToSentence() to get input
*/
public static void analyzer(ArrayList<String> sens , Logger log) {
String paragraphTranslated = null;
//Check null sentence or not
if(sens == null || sens.size() < 0 || sens.isEmpty()) {
return ;
}else {
paragraphTranslated = getParagraphInTranslated(sens, log);
}
if(paragraphTranslated == null) {
return;
}
Properties properties = new Properties();
properties.setProperty("annotators", "tokenize, ssplit, pos, lemma, parse, sentiment");
properties.setProperty("ssplit.newlineIsSentenceBreak", "always");
properties.setProperty("tokenize.codepoint", "true");
StanfordCoreNLP pipeline = new StanfordCoreNLP(properties);
Annotation annotation = pipeline.process(paragraphTranslated);
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
// sentences
for (CoreMap sentence : sentences) {
String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
System.out.println(sentiment + "\t" + sentence);
}
//Tokenizing process https://stanfordnlp.github.io/CoreNLP/tokenize.html
CoreDocument doc = new CoreDocument(unTranslatedText);
pipeline.annotate(doc);
// for (CoreMap sentence : sentences) {
// // Get the parse tree for each sentence
// Tree parseTree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
// // Do something interesting with the parse tree!
// System.out.println(parseTree);
// }
for (CoreLabel tok : doc.tokens()) {
System.out.println(String.format("%s\t%d\t%d", tok.word(), tok.beginPosition(), tok.endPosition()));
}
}
public static void main(String[] args) {
Logger logger = Application.setupLog(BasicLocation.getBaseFileDir()+"logs");
analyzer(breakParagraphToSentence(unTranslatedText), logger);
}
}
the output is :
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator tokenize
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator ssplit
-> 2022/05/04 07:40:47.140 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :0) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 07:40:47.150 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :0) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 07:40:47.155 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :0) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 07:40:47.162 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :0) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 07:40:47.167 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :0) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 07:40:47.172 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :0) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 07:40:47.177 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :0) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 07:40:47.182 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :0) ] : Translated Setences from 7 / 7 successfully
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator tokenize
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator ssplit
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator pos
[main] INFO edu.stanford.nlp.tagger.maxent.MaxentTagger - Loading POS tagger from edu/stanford/nlp/models/pos-tagger/english-left3words-distsim.tagger ... done [0.7 sec].
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator lemma
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator parse
[main] INFO edu.stanford.nlp.parser.common.ParserGrammar - Loading parser from serialized file edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ... done [0.7 sec].
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator sentiment
[main] INFO edu.stanford.nlp.sentiment.SentimentModel - Loading sentiment model edu/stanford/nlp/models/sentiment/sentiment.ser.gz ... done [0.1 sec].
Neutral We are here.
Neutral Hi how are you?
Neutral Good morning, what are you doing?
Neutral condolences
Negative I really do not remember.
Positive I love you baby
Neutral What can be done?
Neutral There
درما 1 5
در 6 8
اینجا 9 14
هستیم 15 20
. 21 22
سلام 23 27
خوبی 28 32
؟ 32 33
وقت 34 37
بخیر 38 42
چیکار 43 48
میکنی 49 54
تسلیت 55 60
راستی 61 66
یادت 67 71
نره 72 75
بیای 76 80
. 81 82
دوست 83 87
دارم 88 92
عزیزم 93 98
چه 99 101
میشه 102 106
کرد 107 110
؟ 110 111
اونجایی 112 119
The Output I Except must show like this :
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator tokenize
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator ssplit
-> 2022/05/04 07:40:47.140 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :0) ] : Translated Setences from 7 / 7 successfully
successfully
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator tokenize
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator ssplit
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator pos
[main] INFO edu.stanford.nlp.tagger.maxent.MaxentTagger - Loading POS tagger from edu/stanford/nlp/models/pos-tagger/english-left3words-distsim.tagger ... done [0.7 sec].
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator lemma
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator parse
[main] INFO edu.stanford.nlp.parser.common.ParserGrammar - Loading parser from serialized file edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ... done [0.7 sec].
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator sentiment
[main] INFO edu.stanford.nlp.sentiment.SentimentModel - Loading sentiment model edu/stanford/nlp/models/sentiment/sentiment.ser.gz ... done [0.1 sec].
Neutral We are here.
Neutral Hi how are you?
Neutral Good morning, what are you doing?
Neutral condolences
Negative I really do not remember.
Positive I love you baby
Neutral What can be done?
Neutral There
درما 1 5
در 6 8
اینجا 9 14
هستیم 15 20
. 21 22
سلام 23 27
خوبی 28 32
؟ 32 33
وقت 34 37
بخیر 38 42
چیکار 43 48
میکنی 49 54
تسلیت 55 60
راستی 61 66
یادت 67 71
نره 72 75
بیای 76 80
. 81 82
دوست 83 87
دارم 88 92
عزیزم 93 98
چه 99 101
میشه 102 106
کرد 107 110
؟ 110 111
اونجایی 112 119
As if you need setupLog method:
public static Logger setupLog(String pathForSqlite) {
Logger logger;
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
// Addressing your comment:
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
String prefix = sdf.format(date);
File logPath = new File(pathForSqlite);
if (!logPath.exists()) {
logPath.mkdir();
}
String dataBaseName = "Reflex_log_" + prefix + "_.db";
logger = Logger.getLogger(APP_NAME);
Connection connection = new SqliteConnection(pathForSqlite, dataBaseName).getConnection();
logger.setUseParentHandlers(false);
logger.setLevel(defaultLogLevel);
LogFormatterSqlite formatter = new LogFormatterSqlite(connection);
ConsoleHandler handler = new ConsoleHandler();
handler.setFormatter(formatter);
logger.addHandler(handler);
return logger;
}
After Read Comments and some investigation , Added some lines and here the result :
Code :
@Nullable
public static String getParagraphInTranslated(ArrayList<String> sentenses , Logger log) {
//Check null sentence or not
if(sentenses == null || sentenses.size() < 0 || sentenses.isEmpty()) {
return null ;
}
String allSentenceToGetter = "";
int countOfTranslated = 0;
log.log(Level.INFO , "JUST FOR CHECK");
for (String sentense : sentenses) {
RepositoryTranslate repositoryTranslate = new RepositoryTranslate();
Logger logger = Application.setupLog(BasicLocation.getBaseFileDir()+"logs");
JSONObject translateJson = repositoryTranslate.translate(sentense, "fa", "en", logger);
boolean isTranslated = translateJson.optBoolean(RepositoryTranslate.IS_TRANSLATE);
if(isTranslated) {
String textFromTranslate = translateJson.optString(RepositoryTranslate.TRANSLATE_TEXT);
textFromTranslate = textFromTranslate.trim();
allSentenceToGetter += (textFromTranslate+"\n") ;
countOfTranslated++;
}
}
log.log(Level.INFO , "JUST FOR CHECK Before Issue");
log.log(Level.INFO , "Translated Setences from "+countOfTranslated+" / "+sentenses.size()+" successfully");
log.log(Level.INFO , "JUST FOR CHECK After Issue");
return allSentenceToGetter;
}
Result :
-> 2022/05/04 09:25:34.767 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :0) ] : JUST FOR CHECK
-> 2022/05/04 09:25:34.833 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :1) ] : JUST FOR CHECK Before Issue
-> 2022/05/04 09:25:34.840 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :1) ] : JUST FOR CHECK Before Issue
-> 2022/05/04 09:25:34.845 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :1) ] : JUST FOR CHECK Before Issue
-> 2022/05/04 09:25:34.851 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :1) ] : JUST FOR CHECK Before Issue
-> 2022/05/04 09:25:34.857 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :1) ] : JUST FOR CHECK Before Issue
-> 2022/05/04 09:25:34.864 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :1) ] : JUST FOR CHECK Before Issue
-> 2022/05/04 09:25:34.869 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :1) ] : JUST FOR CHECK Before Issue
-> 2022/05/04 09:25:34.875 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :1) ] : JUST FOR CHECK Before Issue
-> 2022/05/04 09:25:34.880 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :2) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 09:25:34.885 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :2) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 09:25:34.891 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :2) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 09:25:34.896 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :2) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 09:25:34.901 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :2) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 09:25:34.907 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :2) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 09:25:34.912 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :2) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 09:25:34.917 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :2) ] : Translated Setences from 7 / 7 successfully
-> 2022/05/04 09:25:34.922 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :3) ] : JUST FOR CHECK After Issue
-> 2022/05/04 09:25:34.927 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :3) ] : JUST FOR CHECK After Issue
-> 2022/05/04 09:25:34.932 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :3) ] : JUST FOR CHECK After Issue
-> 2022/05/04 09:25:34.937 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :3) ] : JUST FOR CHECK After Issue
-> 2022/05/04 09:25:34.958 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :3) ] : JUST FOR CHECK After Issue
-> 2022/05/04 09:25:34.963 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :3) ] : JUST FOR CHECK After Issue
-> 2022/05/04 09:25:34.968 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :3) ] : JUST FOR CHECK After Issue
-> 2022/05/04 09:25:34.973 {INFO} [mehritco.ir.megnatis.institute.reflex.nlp.TextAnalyzer At Method :getParagraphInTranslated (Line :3) ] : JUST FOR CHECK After Issue
Solution
You've called setupLog
7 times. Each time you get the logger
logger = Logger.getLogger(APP_NAME);
then add a new handler
logger.addHandler(handler);
By the time you log something, the logger has 7 handlers all doing the same thing, so naturally all 7 handlers print out whatever message you ask the logger to output.
The solution is to move setupLog
to the start of the program, or at least outside of any routine which might loop like this. It's pretty bad separation of concerns to set up the logging in the same code path that does machine translation, anyway...
Answered By - John
Answer Checked By - Gilberto Lyons (JavaFixing Admin)