Search in sources :

Example 6 with LearningData

use of org.liberty.android.fantastischmemo.entity.LearningData in project AnyMemo by helloworld1.

the class MnemosyneXMLExporter method convert.

public void convert(String src, String dest) throws Exception {
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(src);
    String dbName = FilenameUtils.getName(dest);
    PrintWriter outxml = null;
    try {
        final CardDao cardDao = helper.getCardDao();
        final LearningDataDao learningDataDao = helper.getLearningDataDao();
        final CategoryDao categoryDao = helper.getCategoryDao();
        // Populate all category field in a transaction.
        List<Card> cardList = cardDao.callBatchTasks(new Callable<List<Card>>() {

            public List<Card> call() throws Exception {
                List<Card> cards = cardDao.queryForAll();
                for (Card c : cards) {
                    categoryDao.refresh(c.getCategory());
                    learningDataDao.refresh(c.getLearningData());
                }
                return cards;
            }
        });
        if (cardList == null || cardList.size() == 0) {
            throw new IOException("Read empty or corrupted file");
        }
        outxml = new PrintWriter(new BufferedWriter(new FileWriter(dest)));
        if (outxml.checkError()) {
            throw new IOException("Can't open: " + dest);
        }
        int count = 0;
        long timeOfStart = 0L;
        String id, u, gr, e, ac_rp, rt_rp, lps, ac_rp_l, rt_rp_l, l_rp, n_rp, question, answer, category;
        // Now write the xml to the file
        for (Card card : cardList) {
            // At the first item, we write all metadata
            if (count == 0) {
                // timeOfStart = item.getDatelearnUnix();
                // 2000-01-01 12:00
                timeOfStart = 946728000L;
                outxml.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
                outxml.print("<mnemosyne core_version=\"1\" time_of_start=\"" + timeOfStart + "\" >\n");
                outxml.print("<category active=\"1\">\n");
                outxml.print("<name>" + dbName + "</name>\n");
                outxml.print("</category>\n");
            }
            // Convert the learning data
            LearningData ld = card.getLearningData();
            id = "" + card.getOrdinal();
            gr = "" + ld.getGrade();
            e = "" + ld.getEasiness();
            ac_rp = "" + ld.getAcqReps();
            rt_rp = "" + ld.getRetReps();
            lps = "" + ld.getLapses();
            ac_rp_l = "" + ld.getAcqRepsSinceLapse();
            rt_rp_l = "" + ld.getRetRepsSinceLapse();
            ;
            if (ac_rp.equals("0")) {
                u = "1";
            } else {
                u = "0";
            }
            // Add 1 here to avoid rounding problem
            l_rp = Long.toString((ld.getLastLearnDate().getTime() / 1000 - timeOfStart) / 86400);
            n_rp = Long.toString((ld.getNextLearnDate().getTime() / 1000 - timeOfStart) / 86400 + 1);
            // Replace the illegal symbols from the question and answer
            question = card.getQuestion();
            answer = card.getAnswer();
            category = card.getCategory().getName();
            if (question == null) {
                question = "";
            }
            if (answer == null) {
                answer = "";
            }
            if (category == null) {
                category = "";
            }
            question = question.replaceAll("&", "&amp;");
            question = question.replaceAll("<", "&lt;");
            question = question.replaceAll(">", "&gt;");
            question = question.replaceAll("'", "&apos;");
            question = question.replaceAll("\"", "&quot;");
            answer = answer.replaceAll("&", "&amp;");
            answer = answer.replaceAll("<", "&lt;");
            answer = answer.replaceAll(">", "&gt;");
            answer = answer.replaceAll("'", "&apos;");
            answer = answer.replaceAll("\"", "&quot;");
            category = category.replaceAll("&", "&amp;");
            category = category.replaceAll("<", "&lt;");
            category = category.replaceAll(">", "&gt;");
            category = category.replaceAll("'", "&apos;");
            category = category.replaceAll("\"", "&quot;");
            outxml.print("<item id=\"" + id + "\" u=\"" + u + "\" gr=\"" + gr + "\" e=\"" + e + "\" ac_rp=\"" + ac_rp + "\" rt_rp=\"" + rt_rp + "\" lps=\"" + lps + "\" ac_rp_l=\"" + ac_rp_l + "\" rt_rp_l=\"" + rt_rp_l + "\" l_rp=\"" + l_rp + "\" n_rp=\"" + n_rp + "\">\n");
            if (category.equals("")) {
                outxml.print("<cat>" + dbName + "</cat>\n");
            } else {
                outxml.print("<cat>" + category + "</cat>\n");
            }
            outxml.print("<Q>" + question + "</Q>\n");
            outxml.print("<A>" + answer + "</A>\n");
            outxml.print("</item>\n");
            if (outxml.checkError()) {
                throw new IOException("Error writing xml on id: " + id);
            }
            count += 1;
        }
        outxml.print("</mnemosyne>");
        outxml.close();
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
        if (outxml != null) {
            outxml.close();
        }
    }
}
Also used : CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) FileWriter(java.io.FileWriter) IOException(java.io.IOException) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) IOException(java.io.IOException) Card(org.liberty.android.fantastischmemo.entity.Card) BufferedWriter(java.io.BufferedWriter) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) List(java.util.List) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) PrintWriter(java.io.PrintWriter)

Example 7 with LearningData

use of org.liberty.android.fantastischmemo.entity.LearningData in project AnyMemo by helloworld1.

the class MnemosyneXMLImporter method startElement.

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if (localName.equals("mnemosyne")) {
        try {
            timeOfStart = Long.parseLong(atts.getValue("time_of_start"));
            /* Convert to local time */
            /*
                Calendar nc = Calendar.getInstance();
                TimeZone tz = nc.getTimeZone();
                int offset = tz.getOffset(timeOfStart);
                timeOfStart -= offset;
                */
            Log.v(TAG, "Time of start: " + timeOfStart);
        } catch (Exception e) {
            Log.e(TAG, "parse time_of_start error", e);
        }
    }
    if (localName.equals("item")) {
        card = new Card();
        LearningData learningData = new LearningData();
        card.setLearningData(learningData);
        card.setCategory(new Category());
        card.setId(count);
        card.setOrdinal(count);
        count += 1;
        // Id field is not used here. We use "count" variable instead
        // String idAttr = atts.getValue("id");
        String grAttr = atts.getValue("gr");
        if (grAttr != null) {
            learningData.setGrade(Integer.parseInt(grAttr));
        }
        String eAttr = atts.getValue("e");
        if (eAttr != null) {
            learningData.setEasiness(Float.parseFloat(eAttr));
        }
        String acrpAttr = atts.getValue("ac_rp");
        String uAttr = atts.getValue("u");
        String rtrpAttr = atts.getValue("rt_rp");
        if (rtrpAttr != null) {
            learningData.setRetReps(Integer.parseInt(rtrpAttr));
        }
        if (acrpAttr != null) {
            int acrp = Integer.parseInt(acrpAttr);
            if (uAttr != null) {
                if (Integer.parseInt(uAttr) == 1) {
                /* Commented out for testing */
                // acrp = 0;
                }
            }
            if (Integer.valueOf(rtrpAttr) != 0 && acrp == 0) {
                /* This is a workaround for the malformed
                     * XML file.
                     */
                acrp = Integer.valueOf(rtrpAttr) / 2 + 1;
            }
            learningData.setAcqReps(acrp);
        }
        String lpsAttr = atts.getValue("lps");
        if (lpsAttr != null) {
            learningData.setLapses(Integer.parseInt(lpsAttr));
        }
        String acqrplAttr = atts.getValue("ac_rp_l");
        if (acqrplAttr != null) {
            learningData.setAcqRepsSinceLapse(Integer.parseInt(acqrplAttr));
        }
        String rtrplAttr = atts.getValue("rt_rp_l");
        if (rtrplAttr != null) {
            learningData.setRetRepsSinceLapse(Integer.parseInt(rtrplAttr));
        }
        String lrpAttr = atts.getValue("l_rp");
        if (lrpAttr != null && timeOfStart != 0L) {
        // Do nothing now
        }
        String nrpAttr = atts.getValue("n_rp");
        if (nrpAttr != null && lrpAttr != null) {
            long lrp = timeOfStart * 1000 + Math.round(Double.parseDouble(lrpAttr)) * MILLSECS_PER_DAY;
            long nrp = timeOfStart * 1000 + Math.round(Double.parseDouble(nrpAttr)) * MILLSECS_PER_DAY;
            learningData.setLastLearnDate(new Date(lrp));
            learningData.setNextLearnDate(new Date(nrp));
        }
    }
    characterBuf = new StringBuffer();
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) SAXException(org.xml.sax.SAXException) Date(java.util.Date) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 8 with LearningData

use of org.liberty.android.fantastischmemo.entity.LearningData in project AnyMemo by helloworld1.

the class QATxtImporter method convert.

public void convert(String src, String dest) throws Exception {
    if (!new File(dest).exists()) {
        amFileUtil.createDbFileWithDefaultSettings(new File(dest));
    }
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(dest);
    try {
        final CardDao cardDao = helper.getCardDao();
        BufferedReader txtfile = new BufferedReader(new FileReader(src));
        String line;
        int count = 0;
        List<Card> cardList = new LinkedList<Card>();
        boolean isQ = false;
        StringBuffer qBuf = null;
        StringBuffer aBuf = null;
        while ((line = txtfile.readLine()) != null) {
            /* remove BOM */
            line = line.replace("\uFEFF", "");
            String head = "";
            if (line.length() >= 2) {
                head = line.substring(0, 2);
            }
            if (line.equals("")) {
                continue;
            } else if (head.equals("Q:")) {
                if (isQ == true) {
                    /* next line */
                    qBuf.append("<br />" + line.replaceAll("Q:\\s*", ""));
                } else {
                    isQ = true;
                    /* Save item when the Q is after A
                         * because it is a new item */
                    if (count != 0) {
                        Card card = new Card();
                        card.setQuestion(qBuf.toString());
                        card.setAnswer(aBuf.toString());
                        card.setCategory(new Category());
                        card.setLearningData(new LearningData());
                        cardList.add(card);
                    }
                    count += 1;
                    qBuf = new StringBuffer();
                    qBuf.append(line.replaceAll("Q:\\s*", ""));
                }
            } else if (head.equals("A:")) {
                if (isQ == true) {
                    isQ = false;
                    aBuf = new StringBuffer();
                    aBuf.append(line.replaceAll("A:\\s*", ""));
                } else {
                    aBuf.append("<br />" + line.replaceAll("A:\\s*", ""));
                }
            } else {
                if (isQ) {
                    qBuf.append("<br />" + line);
                } else {
                    aBuf.append("<br />" + line);
                }
            }
        }
        /* Last item need to be added manually */
        count += 1;
        Card card = new Card();
        card.setQuestion(qBuf.toString());
        card.setAnswer(aBuf.toString());
        card.setCategory(new Category());
        card.setLearningData(new LearningData());
        cardList.add(card);
        txtfile.close();
        cardDao.createCards(cardList);
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) LinkedList(java.util.LinkedList) Card(org.liberty.android.fantastischmemo.entity.Card) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao)

Example 9 with LearningData

use of org.liberty.android.fantastischmemo.entity.LearningData in project AnyMemo by helloworld1.

the class Supermemo2008XMLImporter method startElement.

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if (localName.equals("Question")) {
        characterBuf = new StringBuffer();
        card = new Card();
        card.setLearningData(new LearningData());
        card.setCategory(new Category());
    }
    if (localName.equals("Answer")) {
        characterBuf = new StringBuffer();
    }
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 10 with LearningData

use of org.liberty.android.fantastischmemo.entity.LearningData in project AnyMemo by helloworld1.

the class DefaultSchedulerTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    defaultScheduler = new DefaultScheduler(new SchedulingAlgorithmParameters(getContext()));
    newCardLearningData = new LearningData();
    failedCardLearningData = new LearningData();
    failedCardLearningData.setAcqReps(1);
    failedCardLearningData.setNextLearnDate(new Date(new Date().getTime() - 1000000));
    failedCardLearningData.setGrade(0);
    learnedCardLearningData = new LearningData();
    learnedCardLearningData.setAcqReps(1);
    learnedCardLearningData.setNextLearnDate(new Date(new Date().getTime() + 1000000));
    learnedCardLearningData.setGrade(5);
    forReviewCardLearningData = new LearningData();
    forReviewCardLearningData.setAcqReps(2);
    forReviewCardLearningData.setNextLearnDate(new Date(new Date().getTime() - 1000000));
    forReviewCardLearningData.setGrade(5);
}
Also used : SchedulingAlgorithmParameters(org.liberty.android.fantastischmemo.entity.SchedulingAlgorithmParameters) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) DefaultScheduler(org.liberty.android.fantastischmemo.scheduler.DefaultScheduler) Date(java.util.Date)

Aggregations

LearningData (org.liberty.android.fantastischmemo.entity.LearningData)42 Card (org.liberty.android.fantastischmemo.entity.Card)25 SmallTest (android.support.test.filters.SmallTest)15 Test (org.junit.Test)15 CardDao (org.liberty.android.fantastischmemo.dao.CardDao)15 Category (org.liberty.android.fantastischmemo.entity.Category)15 Date (java.util.Date)11 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)9 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)8 AbstractPreferencesTest (org.liberty.android.fantastischmemo.test.AbstractPreferencesTest)8 ArrayList (java.util.ArrayList)7 AbstractExistingDBTest (org.liberty.android.fantastischmemo.test.AbstractExistingDBTest)7 SQLException (java.sql.SQLException)5 CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)5 File (java.io.File)4 IOException (java.io.IOException)3 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 FileReader (java.io.FileReader)2 FileWriter (java.io.FileWriter)2