Search in sources :

Example 26 with Category

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

the class QuizQueuingManagerTest method testFilterCategory.

@SmallTest
@Test
public void testFilterCategory() throws Exception {
    CardDao cardDao = helper.getCardDao();
    Card c10 = cardDao.queryForId(10);
    assertNotNull(c10);
    Category cat = new Category();
    cat.setName("tt");
    c10.setCategory(cat);
    cardDao.update(c10);
    QueueManager queueManager = new QuizQueueManager.Builder().setDbOpenHelper(helper).setFilterCategory(cat).build();
    Card cqueue = queueManager.dequeue();
    assertEquals(10, (int) cqueue.getId());
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) QuizQueueManager(org.liberty.android.fantastischmemo.queue.QuizQueueManager) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card) QuizQueueManager(org.liberty.android.fantastischmemo.queue.QuizQueueManager) QueueManager(org.liberty.android.fantastischmemo.queue.QueueManager) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) AbstractExistingDBTest(org.liberty.android.fantastischmemo.test.AbstractExistingDBTest) SmallTest(android.support.test.filters.SmallTest)

Example 27 with Category

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

the class CSVImporter 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);
    CSVReader reader;
    if (separator == null) {
        reader = new CSVReader(new FileReader(src));
    } else {
        reader = new CSVReader(new FileReader(src), separator);
    }
    try {
        final CardDao cardDao = helper.getCardDao();
        String[] nextLine;
        final List<Card> cardList = new LinkedList<Card>();
        while ((nextLine = reader.readNext()) != null) {
            if (nextLine.length < 2) {
                throw new Exception("Malformed CSV file. Please make sure the CSV's first column is question, second one is answer and the optinal third one is category");
            }
            String note = "";
            String category = "";
            if (nextLine.length >= 3) {
                category = nextLine[2];
            }
            if (nextLine.length >= 4) {
                note = nextLine[3];
            }
            Card card = new Card();
            Category cat = new Category();
            LearningData ld = new LearningData();
            cat.setName(category);
            card.setCategory(cat);
            card.setLearningData(ld);
            card.setQuestion(nextLine[0]);
            card.setAnswer(nextLine[1]);
            card.setNote(note);
            cardList.add(card);
        }
        cardDao.createCards(cardList);
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
        reader.close();
    }
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) CSVReader(com.opencsv.CSVReader) LinkedList(java.util.LinkedList) Card(org.liberty.android.fantastischmemo.entity.Card) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) FileReader(java.io.FileReader) File(java.io.File) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao)

Example 28 with Category

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

the class Mnemosyne2CardsImporter method xmlToCards.

/*
       Sample format to parse:
       <openSM2sync number_of_entries="10"><log type="10" o_id="epbt47DXNulRU8DsEtwtRp"><name>Category1</name></log>
       <log type="10" o_id="5SfWDFGwqrlnGLDQxHHyG0"><name>Category2</name></log>
       <log type="16" o_id="4pblLFgzF7vONXhjIQv5Rm"><b>Answer1</b><f>Qustion1</f></log>
       <log type="16" o_id="QiQz84nC2tvMF5OKdPepaf"><b>Answer2</b><f>Question2</f></log>
       <log type="16" o_id="hfoNwlcf0quuxpcWntyCZl"><b>Answer3</b><f>Question3</f></log>
       <log type="16" o_id="7xmRCBH0WP0DZaxeFn5NLw"><b>Answer4</b><f>Question4</f></log>
       <log card_t="1" fact_v="1.1" e="2.5" gr="-1" tags="epbt47DXNulRU8DsEtwtRp" rt_rp_l="0" lps="0" l_rp="-1" n_rp="-1" ac_rp_l="0" rt_rp="0" ac_rp="0" type="6" o_id="MDcIMVQJUGEjqR6DJoAqVm" fact="4pblLFgzF7vONXhjIQv5Rm"></log>
       <log card_t="1" fact_v="1.1" e="2.5" gr="-1" tags="epbt47DXNulRU8DsEtwtRp" rt_rp_l="0" lps="0" l_rp="-1" n_rp="-1" ac_rp_l="0" rt_rp="0" ac_rp="0" type="6" o_id="iVwbE7q2QLPY4SJRS1AtqK" fact="QiQz84nC2tvMF5OKdPepaf"></log>
       <log card_t="1" fact_v="1.1" e="2.5" gr="-1" tags="5SfWDFGwqrlnGLDQxHHyG0" rt_rp_l="0" lps="0" l_rp="-1" n_rp="-1" ac_rp_l="0" rt_rp="0" ac_rp="0" type="6" o_id="hW3JxWbfBOWoKDVQZ3jVQ1" fact="hfoNwlcf0quuxpcWntyCZl"></log>
       <log card_t="1" fact_v="1.1" e="2.5" gr="-1" tags="5SfWDFGwqrlnGLDQxHHyG0" rt_rp_l="0" lps="0" l_rp="-1" n_rp="-1" ac_rp_l="0" rt_rp="0" ac_rp="0" type="6" o_id="7IXjCysHuCDtXo8hlFrK55" fact="7xmRCBH0WP0DZaxeFn5NLw"></log>
       </openSM2sync>
   */
private List<Card> xmlToCards(File xmlFile) throws IOException, XmlPullParserException {
    FileInputStream inputStream = new FileInputStream(xmlFile);
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    XmlPullParser xpp = factory.newPullParser();
    xpp.setInput(new BufferedReader(new InputStreamReader(inputStream)));
    List<Card> cardList = null;
    // Map between xml oid to card
    Map<String, Card> cardOidMap = null;
    // Map between xml oid to category
    Map<String, Category> categoryOidMAP = null;
    int lastType = 0;
    int currentOrd = 1;
    String lastOid = "";
    ValueType lastValueType = null;
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_DOCUMENT) {
        // Nothing
        } else if (eventType == XmlPullParser.START_TAG) {
            // <openSM2sync number_of_entries="10">
            if (xpp.getName().equals("openSM2sync")) {
                int cardCount = Integer.parseInt(xpp.getAttributeValue(null, "number_of_entries"));
                cardList = new ArrayList<Card>(cardCount);
                // The hash map is 0.75 full for performance and space.
                // Use LinkedHashMap to maintain insertion order.
                cardOidMap = new LinkedHashMap<String, Card>(cardCount * 4 / 3);
                categoryOidMAP = new LinkedHashMap<String, Category>();
            }
            if (xpp.getName().equals("log")) {
                lastType = Integer.parseInt(xpp.getAttributeValue(null, "type"));
                lastOid = xpp.getAttributeValue(null, "o_id");
            }
            // <log type="10" o_id="5SfWDFGwqrlnGLDQxHHyG0"><name>Category2</name></log>
            if (lastType == TAG_FIELD_TYPE && xpp.getName().equals("name")) {
                lastValueType = ValueType.CARD_TAG;
            }
            // <log type="16" o_id="7xmRCBH0WP0DZaxeFn5NLw"><b>Answer4</b><f>Question4</f></log>
            if (lastType == QA_FIELD_TYPE && xpp.getName().equals("f")) {
                lastValueType = ValueType.CARD_FRONT;
            }
            if (lastType == QA_FIELD_TYPE && xpp.getName().equals("b")) {
                lastValueType = ValueType.CARD_BACK;
            }
            // <log card_t="1" fact_v="1.1" e="2.5" gr="-1" tags="epbt47DXNulRU8DsEtwtRp" rt_rp_l="0" lps="0" l_rp="-1" n_rp="-1" ac_rp_l="0" rt_rp="0" ac_rp="0" type="6" o_id="MDcIMVQJUGEjqR6DJoAqVm" fact=  "4pblLFgzF7vONXhjIQv5Rm"></log>
            if (lastType == LEARNING_DATA_FIELD_TYPE) {
                String tagOid = xpp.getAttributeValue(null, "tags");
                String factOid = xpp.getAttributeValue(null, "fact");
                LearningData ld = new LearningData();
                ld.setEasiness(Float.parseFloat(xpp.getAttributeValue(null, "e")));
                ld.setGrade(Integer.parseInt(xpp.getAttributeValue(null, "gr")));
                ld.setRetReps(Integer.parseInt(xpp.getAttributeValue(null, "rt_rp_l")));
                ld.setLapses(Integer.parseInt(xpp.getAttributeValue(null, "lps")));
                ld.setLapses(Integer.parseInt(xpp.getAttributeValue(null, "lps")));
                long nrp = Integer.parseInt(xpp.getAttributeValue(null, "n_rp"));
                long lrp = Integer.parseInt(xpp.getAttributeValue(null, "l_rp"));
                if (lrp != -1 && nrp != -1) {
                    ld.setLastLearnDate(new Date(lrp * 1000L));
                    ld.setNextLearnDate(new Date(nrp * 1000L));
                }
                ld.setAcqRepsSinceLapse(Integer.parseInt(xpp.getAttributeValue(null, "ac_rp_l")));
                ld.setRetReps(Integer.parseInt(xpp.getAttributeValue(null, "rt_rp")));
                ld.setAcqReps(Integer.parseInt(xpp.getAttributeValue(null, "ac_rp")));
                // Now find the card that need this learning data
                Card card = cardOidMap.get(factOid);
                // The learning data is using the same id as card
                ld.setId(card.getId());
                // and also find out the corresponding tag for card's category
                Category category = categoryOidMAP.get(tagOid);
                if (tagOid.contains(",")) {
                    // If the card has multiple category, select only the first category.
                    String firstOid = tagOid.split(",")[0];
                    category = categoryOidMAP.get(firstOid);
                } else {
                    category = categoryOidMAP.get(tagOid);
                }
                if (category == null) {
                    category = new Category();
                }
                card.setLearningData(ld);
                card.setCategory(category);
            }
        } else if (eventType == XmlPullParser.TEXT) {
            if (lastValueType == ValueType.CARD_TAG) {
                Category category = new Category();
                category.setName(xpp.getText());
                categoryOidMAP.put(lastOid, category);
            } else if (lastValueType == ValueType.CARD_FRONT) {
                Card card = cardOidMap.get(lastOid);
                if (card == null) {
                    card = new Card();
                    card.setId(currentOrd);
                    card.setOrdinal(currentOrd);
                    cardOidMap.put(lastOid, card);
                    cardList.add(card);
                    currentOrd++;
                }
                card.setQuestion(xpp.getText());
            } else if (lastValueType == ValueType.CARD_BACK) {
                Card card = cardOidMap.get(lastOid);
                if (card == null) {
                    card = new Card();
                    card.setId(currentOrd);
                    card.setOrdinal(currentOrd);
                    cardOidMap.put(lastOid, card);
                    cardList.add(card);
                    currentOrd++;
                }
                card.setAnswer(xpp.getText());
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if (xpp.getName().equals("log")) {
                lastType = 0;
                lastOid = "";
                lastValueType = null;
            }
        }
        eventType = xpp.next();
    }
    return cardList;
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) InputStreamReader(java.io.InputStreamReader) XmlPullParser(org.xmlpull.v1.XmlPullParser) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) Date(java.util.Date) Card(org.liberty.android.fantastischmemo.entity.Card) LinkedHashMap(java.util.LinkedHashMap) XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) BufferedReader(java.io.BufferedReader) LearningData(org.liberty.android.fantastischmemo.entity.LearningData)

Example 29 with Category

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

the class SupermemoXMLImporter method startElement.

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if (localName.equals("SuperMemoElement")) {
        card = new Card();
        card.setCategory(new Category());
        ld = new LearningData();
        card.setLearningData(ld);
        // Set a default interval, in case of malformed the xml file
        interval = 0;
    }
    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 30 with Category

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

the class CellsDBConverter method convertCellsToDb.

/* cardCells contains the question, answer, category and note
     * category and note is optionally.
     * learningDataCells contains all necessary learning data.
     * If learningDataCells, new learning data is used.
     * dbPath is the place to store converted database
     */
public void convertCellsToDb(Cells cardCells, Cells learningDataCells, String dbPath) throws IOException {
    int numberOfRows = cardCells.getRowCounts();
    int numberOfLearningDataRows = 0;
    if (learningDataCells != null) {
        numberOfLearningDataRows = learningDataCells.getRowCounts();
    }
    // We ignore the header row
    List<Card> cardList = new ArrayList<Card>(numberOfRows + 1);
    for (int i = 1; i < numberOfRows; i++) {
        List<String> row = cardCells.getRow(i);
        Card card = new Card();
        Category category = new Category();
        if (row.size() == 0) {
            Log.w(TAG, "Each row in spreadsheet should have at least 2 column: question and answer. Row number: " + i);
        }
        if (row.size() >= 1) {
            card.setQuestion(row.get(0));
        }
        if (row.size() >= 2) {
            card.setAnswer(row.get(1));
        }
        if (row.size() >= 3) {
            category.setName(row.get(2));
        }
        if (row.size() >= 4) {
            card.setNote(row.get(3));
        }
        // This can't be null because numberOfLearningDataRows is 0
        // if learningDataCells is 0.
        LearningData learningData;
        if (i < numberOfLearningDataRows) {
            learningData = getLearningDataFromRow(learningDataCells.getRow(i));
        } else {
            learningData = new LearningData();
        }
        card.setCategory(category);
        card.setLearningData(learningData);
        cardList.add(card);
    }
    if (cardList.size() == 0) {
        throw new IOException("Wrong spreadsheet format. The spreadsheet should contain at least 1 worksheet with at least 2 columns of questions and answers.");
    }
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(mContext, dbPath);
    try {
        CardDao cardDao = helper.getCardDao();
        cardDao.createCards(cardList);
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) Category(org.liberty.android.fantastischmemo.entity.Category) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

Aggregations

Category (org.liberty.android.fantastischmemo.entity.Category)35 Card (org.liberty.android.fantastischmemo.entity.Card)30 CardDao (org.liberty.android.fantastischmemo.dao.CardDao)27 CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)19 SmallTest (android.support.test.filters.SmallTest)17 Test (org.junit.Test)17 AbstractExistingDBTest (org.liberty.android.fantastischmemo.test.AbstractExistingDBTest)17 LearningData (org.liberty.android.fantastischmemo.entity.LearningData)15 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)11 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)3 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 LinkedList (java.util.LinkedList)2 QueueManager (org.liberty.android.fantastischmemo.queue.QueueManager)2 Intent (android.content.Intent)1