Search in sources :

Example 21 with LearningData

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

the class GoogleDriveUploadHelper method createSpreadsheet.

public Spreadsheet createSpreadsheet(String title, String dbPath) throws Exception {
    // First read card because if it failed we don't even bother uploading.
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(mContext, dbPath);
    List<Card> cardList = null;
    try {
        final CardDao cardDao = helper.getCardDao();
        final CategoryDao categoryDao = helper.getCategoryDao();
        final LearningDataDao learningDataDao = helper.getLearningDataDao();
        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;
            }
        });
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
    // Find the spreadsheets to delete after the process is done
    List<Document> spreadsheetsToDelete = DocumentFactory.findDocuments(title, authToken);
    // Create the AnyMemo folder if needed
    Folder folder = FolderFactory.createOrReturnFolder("AnyMemo", authToken);
    // Create new spreadsheet
    Document newSpreadsheetDocument = DocumentFactory.createSpreadsheet(title, authToken);
    List<Spreadsheet> spreadsheetList = SpreadsheetFactory.getSpreadsheets(authToken);
    Spreadsheet newSpreadsheet = spreadsheetList.get(0);
    // Create worksheets
    List<Worksheet> worksheetsToDelete = WorksheetFactory.getWorksheets(newSpreadsheet, authToken);
    // Delete the worksheets with the duplicated name
    // First create a dummy worksheet so we have at least one worksheet
    boolean needsToCreateDummy = true;
    for (Worksheet ws : worksheetsToDelete) {
        if (ws.getTitle().equals("dummy")) {
            needsToCreateDummy = false;
            break;
        }
    }
    // Dummy worksheet will be deleted at the last step
    if (needsToCreateDummy) {
        Worksheet dummyWorksheet = WorksheetFactory.createWorksheet(newSpreadsheet, "dummy", 1, 1, authToken);
        worksheetsToDelete.add(dummyWorksheet);
    }
    Iterator<Worksheet> worksheetToDeleteIterator = worksheetsToDelete.iterator();
    while (worksheetToDeleteIterator.hasNext()) {
        Worksheet ws = worksheetToDeleteIterator.next();
        if (ws.getTitle().equals("cards")) {
            WorksheetFactory.deleteWorksheet(newSpreadsheet, ws, authToken);
            worksheetToDeleteIterator.remove();
        }
        if (ws.getTitle().equals("learning_data")) {
            WorksheetFactory.deleteWorksheet(newSpreadsheet, ws, authToken);
            worksheetToDeleteIterator.remove();
        }
    }
    // setting up the worksheet size is critical.
    Worksheet cardsWorksheet = WorksheetFactory.createWorksheet(newSpreadsheet, "cards", cardList.size() + 1, 4, authToken);
    Cells cardCells = new Cells();
    // Add the header for cards first
    cardCells.addCell(1, 1, "question");
    cardCells.addCell(1, 2, "answer");
    cardCells.addCell(1, 3, "category");
    cardCells.addCell(1, 4, "note");
    for (int i = 0; i < cardList.size(); i++) {
        Card c = cardList.get(i);
        // THe first row is the header.
        cardCells.addCell(i + 2, 1, c.getQuestion());
        cardCells.addCell(i + 2, 2, c.getAnswer());
        cardCells.addCell(i + 2, 3, c.getCategory().getName());
        cardCells.addCell(i + 2, 4, c.getNote());
    }
    // upload card's rows into worksheet
    CellsFactory.uploadCells(newSpreadsheet, cardsWorksheet, cardCells, authToken);
    // Let GC free up memory
    cardCells = null;
    // Now deal with learning data
    Worksheet learningDataWorksheet = WorksheetFactory.createWorksheet(newSpreadsheet, "learning_data", cardList.size() + 1, 9, authToken);
    Cells learningDataCells = new Cells();
    // The first row is the header.
    learningDataCells.addCell(1, 1, "acqReps");
    learningDataCells.addCell(1, 2, "acqRepsSinceLapse");
    learningDataCells.addCell(1, 3, "easiness");
    learningDataCells.addCell(1, 4, "grade");
    learningDataCells.addCell(1, 5, "lapses");
    learningDataCells.addCell(1, 6, "lastLearnDate");
    learningDataCells.addCell(1, 7, "nextLearnDate");
    learningDataCells.addCell(1, 8, "retReps");
    learningDataCells.addCell(1, 9, "retRepsSinceLapse");
    for (int i = 0; i < cardList.size(); i++) {
        LearningData ld = cardList.get(i).getLearningData();
        learningDataCells.addCell(i + 2, 1, Integer.toString(ld.getAcqReps()));
        learningDataCells.addCell(i + 2, 2, Integer.toString(ld.getAcqRepsSinceLapse()));
        learningDataCells.addCell(i + 2, 3, Float.toString(ld.getEasiness()));
        learningDataCells.addCell(i + 2, 4, Integer.toString(ld.getGrade()));
        learningDataCells.addCell(i + 2, 5, Integer.toString(ld.getLapses()));
        learningDataCells.addCell(i + 2, 6, ISO8601_FORMATTER.format(ld.getLastLearnDate()));
        learningDataCells.addCell(i + 2, 7, ISO8601_FORMATTER.format(ld.getNextLearnDate()));
        learningDataCells.addCell(i + 2, 8, Integer.toString(ld.getRetReps()));
        learningDataCells.addCell(i + 2, 9, Integer.toString(ld.getRetRepsSinceLapse()));
    }
    // upload learning data rows into worksheet
    CellsFactory.uploadCells(newSpreadsheet, learningDataWorksheet, learningDataCells, authToken);
    learningDataCells = null;
    // Put new spreadsheet into the folder
    FolderFactory.addDocumentToFolder(newSpreadsheetDocument, folder, authToken);
    // Finally delete the unneeded worksheets ...
    for (Worksheet ws : worksheetsToDelete) {
        WorksheetFactory.deleteWorksheet(newSpreadsheet, ws, authToken);
    }
    // ... And spreadsheets with duplicated names.
    for (Document ss : spreadsheetsToDelete) {
        DocumentFactory.deleteDocument(ss, authToken);
    }
    return null;
}
Also used : CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) Callable(java.util.concurrent.Callable) Card(org.liberty.android.fantastischmemo.entity.Card) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao)

Example 22 with LearningData

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

the class DefaultScheduler method schedule.

/*
     * Return the interval of the after schedule the new card
     */
@Override
public LearningData schedule(LearningData oldData, int newGrade, boolean includeNoise) {
    Date currentDate = new Date();
    double actualInterval = AMDateUtil.diffDate(oldData.getLastLearnDate(), currentDate);
    double scheduleInterval = oldData.getInterval();
    double newInterval = 0.0;
    int oldGrade = oldData.getGrade();
    float oldEasiness = oldData.getEasiness();
    int newLapses = oldData.getLapses();
    int newAcqReps = oldData.getAcqReps();
    int newRetReps = oldData.getRetReps();
    int newAcqRepsSinceLapse = oldData.getAcqRepsSinceLapse();
    int newRetRepsSinceLapse = oldData.getRetRepsSinceLapse();
    float newEasiness = oldData.getEasiness();
    Date newFirstLearnDate = oldData.getFirstLearnDate();
    if (actualInterval <= parameters.getMinimalInterval()) {
        actualInterval = parameters.getMinimalInterval();
    }
    // new item (unseen = 1 in mnemosyne)
    if (newAcqReps == 0) {
        newAcqReps = 1;
        newEasiness = parameters.getInitialEasiness();
        newAcqRepsSinceLapse = 1;
        newInterval = calculateInitialInterval(newGrade);
    } else if (!isGradeSuccessful(oldGrade, false) && !isGradeSuccessful(newGrade, false)) {
        newAcqReps += 1;
        newAcqRepsSinceLapse += 1;
        newInterval = 0;
    } else if (!isGradeSuccessful(oldGrade, false) && isGradeSuccessful(newGrade, false)) {
        newAcqReps += 1;
        newAcqRepsSinceLapse += 1;
        newInterval = parameters.getFailedGradingInterval(newGrade);
    } else if (isGradeSuccessful(oldGrade, false) && !isGradeSuccessful(newGrade, false)) {
        newRetReps += 1;
        newLapses += 1;
        newAcqRepsSinceLapse = 0;
        newRetRepsSinceLapse = 0;
        newInterval = 0;
    } else if (isGradeSuccessful(oldGrade, false) && isGradeSuccessful(newGrade, false)) {
        newRetReps += 1;
        newRetRepsSinceLapse += 1;
        newInterval = 0;
        if (actualInterval >= scheduleInterval) {
            newEasiness = oldEasiness + parameters.getEasinessIncremental(newGrade);
            if (newEasiness < parameters.getMinimalEasiness()) {
                newEasiness = parameters.getMinimalEasiness();
            }
            if (actualInterval <= scheduleInterval) {
                newInterval = actualInterval * newEasiness;
                // Fix the cram review scheduling problem by using the larger of scheduled interval
                newInterval = actualInterval * newEasiness > scheduleInterval ? actualInterval * newEasiness : scheduleInterval;
            } else {
                newInterval = scheduleInterval * newEasiness;
            }
        } else {
            // If learning a card before it is cheduled
            // The old interval is used.
            newInterval = scheduleInterval;
        }
        if (newInterval <= parameters.getMinimalInterval()) {
            Log.w(TAG, "Interval " + newInterval + " is less than " + parameters.getMinimalInterval() + " for old data: " + oldData);
            newInterval = parameters.getMinimalInterval();
        }
    }
    /*
         * By default the noise is included. However,
         * the estimation of days should not include noise
         * If the noise is disabled in the algorithm customization.
         */
    if (includeNoise && parameters.getEnableNoise()) {
        newInterval = newInterval + calculateIntervalNoise(newInterval);
    }
    if (this.isCardNew(oldData)) {
        newFirstLearnDate = new Date();
    }
    LearningData newData = new LearningData();
    newData.setId(oldData.getId());
    newData.setAcqReps(newAcqReps);
    newData.setAcqRepsSinceLapse(newAcqRepsSinceLapse);
    newData.setEasiness(newEasiness);
    newData.setGrade(newGrade);
    newData.setLapses(newLapses);
    newData.setLastLearnDate(currentDate);
    newData.setNextLearnDate(afterDays(currentDate, newInterval));
    newData.setRetReps(newRetReps);
    newData.setRetRepsSinceLapse(newRetRepsSinceLapse);
    newData.setFirstLearnDate(newFirstLearnDate);
    return newData;
}
Also used : LearningData(org.liberty.android.fantastischmemo.entity.LearningData) Date(java.util.Date)

Example 23 with LearningData

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

the class CellsDBConverter method getLearningDataFromRow.

private LearningData getLearningDataFromRow(List<String> row) {
    LearningData learningData = new LearningData();
    // Make sure it is valid learning data
    if (row.size() == 9) {
        learningData.setAcqReps(Integer.parseInt(row.get(0)));
        learningData.setAcqRepsSinceLapse(Integer.parseInt(row.get(1)));
        learningData.setEasiness(Float.parseFloat(row.get(2)));
        learningData.setGrade(Integer.parseInt(row.get(3)));
        learningData.setLapses(Integer.parseInt(row.get(4)));
        try {
            learningData.setLastLearnDate(ISO8601_FORMATTER.parse(row.get(5)));
        } catch (ParseException e) {
            Log.w(TAG, "Parset date error", e);
            // 2010-01-01 00:00:00
            learningData.setLastLearnDate(new Date(1262304000000L));
        }
        try {
            learningData.setNextLearnDate(ISO8601_FORMATTER.parse(row.get(6)));
        } catch (ParseException e) {
            Log.w(TAG, "Parset date error", e);
            // 2010-01-01 00:00:00
            learningData.setNextLearnDate(new Date(1262304000000L));
        }
        learningData.setRetReps(Integer.parseInt(row.get(7)));
        learningData.setRetRepsSinceLapse(Integer.parseInt(row.get(8)));
    }
    return learningData;
}
Also used : ParseException(java.text.ParseException) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) Date(java.util.Date)

Example 24 with LearningData

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

the class ImportMergingTest method setUp.

@Before
public void setUp() throws Exception {
    // Reflect out the test context
    testContext = getContext();
    amFileUtil = new AMFileUtil(testContext, new AMPrefUtil(getContext()));
    newDbCardList = new ArrayList<Card>();
    Card c1 = new Card();
    c1.setQuestion("old question 1");
    c1.setAnswer("old answer 1");
    c1.setLearningData(new LearningData());
    c1.setCategory(new Category());
    Card c2 = new Card();
    c2.setQuestion("old question 2");
    c2.setAnswer("old answer 2");
    c2.setLearningData(new LearningData());
    c2.setCategory(new Category());
    newDbCardList.add(c1);
    newDbCardList.add(c2);
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) AMFileUtil(org.liberty.android.fantastischmemo.utils.AMFileUtil) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) AMPrefUtil(org.liberty.android.fantastischmemo.utils.AMPrefUtil) Card(org.liberty.android.fantastischmemo.entity.Card) Before(org.junit.Before)

Example 25 with LearningData

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

the class CardDaoTest method testReviewCardsOrderOfOneDifferentEasiness.

@SmallTest
@Test
public void testReviewCardsOrderOfOneDifferentEasiness() throws SQLException {
    CardDao cardDao = helper.getCardDao();
    Card c13 = cardDao.queryForId(13);
    Card c14 = cardDao.queryForId(14);
    Card c15 = cardDao.queryForId(15);
    LearningDataDao learningDataDao = helper.getLearningDataDao();
    Date testDate = new Date((new Date().getTime() - 1));
    learningDataDao.refresh(c13.getLearningData());
    LearningData c13Ld = c13.getLearningData();
    c13Ld.setAcqReps(1);
    c13Ld.setNextLearnDate(testDate);
    c13Ld.setEasiness((float) 2.8);
    learningDataDao.update(c13Ld);
    learningDataDao.refresh(c14.getLearningData());
    LearningData c14Ld = c14.getLearningData();
    c14Ld.setAcqReps(1);
    c14Ld.setNextLearnDate(testDate);
    c14Ld.setEasiness((float) 2.6);
    learningDataDao.update(c14Ld);
    learningDataDao.refresh(c15.getLearningData());
    LearningData c15Ld = c15.getLearningData();
    c15Ld.setAcqReps(1);
    c15Ld.setNextLearnDate(testDate);
    c15Ld.setEasiness((float) 2.6);
    learningDataDao.update(c15Ld);
    List<Card> cards = cardDao.getCardsForReview(null, null, 50, ReviewOrdering.HardestFirst);
    assertEquals(3, cards.size());
    assertEquals(14, (int) cards.get(0).getOrdinal());
    assertEquals(15, (int) cards.get(1).getOrdinal());
    assertEquals(13, (int) cards.get(2).getOrdinal());
}
Also used : LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Date(java.util.Date) Card(org.liberty.android.fantastischmemo.entity.Card) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) AbstractExistingDBTest(org.liberty.android.fantastischmemo.test.AbstractExistingDBTest) SmallTest(android.support.test.filters.SmallTest)

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