Hi, I have the Exception in thread "main" com.cybozu.labs.langdetect.LangDetectException: duplicate the same language profile and I think this is because I am iterating through the excel cells which is calling the langdetect more than once. Can anyone tell me how I can fix this please
The code below is meant to read the three lines of excel rows and detect the languages and write them in the languages column. But, currently it is only detecting the language of row2 for row1. So I am pretty sure it has to do with my iterator.
import java.util.ArrayList;
import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.DetectorFactory;
import com.cybozu.labs.langdetect.Language;
import java.util.Scanner;
import com.cybozu.labs.langdetect.LangDetectException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.DataFormatter;
public class LangDetectSample
{
public static void main(String[] args) throws IOException, LangDetectException
{
String excelFilePath = "C:\\BL\\Books.xlsx";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0); // Assuming that the data is sheet in one
Iterator<Row> iterator = firstSheet.iterator();
DataFormatter formatter = new DataFormatter();
LangDetectSample lang = new LangDetectSample();
//creating variables
String language;
int rowNumber;
String title;
//Blank workbook
XSSFWorkbook wb = new XSSFWorkbook(); //new workbook //fixed
//Create a blank sheet
Sheet sheet1 = wb.createSheet("Predicted language"); //fixed
while (iterator.hasNext())
{
Row nextRow = iterator.next();
rowNumber = nextRow.getRowNum();
Cell cell = nextRow.getCell(1); // Assuming that the title is in column 2 //put a comma
title = formatter.formatCellValue(cell);
System.out.print(title);
//Title should now have the title.
// Call the language detector:
language = lang.detect(title);
System.out.print(language);
// Write the title, language
Row row = sheet1.createRow(rowNumber);
Cell cell2 = row.createCell(2); //changed variable name
cell2.setCellValue(title);
Cell cell3 = row.createCell(3);
cell3.setCellValue(language);
}
try
{
//Write the workbook in file system
FileOutputStream out = new FileOutputStream("C:\\BL\\Books.xlsx");
workbook.write(out);
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
workbook.close();
inputStream.close();
}
public void init(String profileDirectory) throws LangDetectException
{
DetectorFactory.loadProfile(profileDirectory);
}
public String detect(String text) throws LangDetectException
{
DetectorFactory.loadProfile("C:\\BL\\profiles");
Detector detector = DetectorFactory.create();
detector.append(text);
return detector.detect();
}
public ArrayList detectLangs(String text) throws LangDetectException
{
Detector detector = DetectorFactory.create();
detector.append(text);
return detector.getProbabilities();
}
}
Hi, I have the Exception in thread "main" com.cybozu.labs.langdetect.LangDetectException: duplicate the same language profile and I think this is because I am iterating through the excel cells which is calling the langdetect more than once. Can anyone tell me how I can fix this please
The code below is meant to read the three lines of excel rows and detect the languages and write them in the languages column. But, currently it is only detecting the language of row2 for row1. So I am pretty sure it has to do with my iterator.