First commit
This commit is contained in:
parent
652e8b1262
commit
e26d0175eb
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,55 @@
|
||||||
|
/*\
|
||||||
|
title: $:/plugins/tiddlywiki/words/filters.js
|
||||||
|
type: application/javascript
|
||||||
|
module-type: filteroperator
|
||||||
|
|
||||||
|
Filter operators
|
||||||
|
|
||||||
|
\*/
|
||||||
|
(function(){
|
||||||
|
|
||||||
|
/*jslint node: true, browser: true */
|
||||||
|
/*global $tw: false */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var WORD_LIST_TITLE = "$:/plugins/tiddlywiki/words/WordList";
|
||||||
|
|
||||||
|
/*
|
||||||
|
Export our filter function
|
||||||
|
*/
|
||||||
|
exports.anagrams = function(source,operator,options) {
|
||||||
|
// Helper to lowercase the letters of a string and sort the letters into alphabetical order
|
||||||
|
var normaliseLetters = function(str) {
|
||||||
|
return str.toLowerCase().split("").sort().join("");
|
||||||
|
};
|
||||||
|
// Create a hashmap of normalised strings to arrays of words
|
||||||
|
var anagramList = options.wiki.getCacheForTiddler(WORD_LIST_TITLE,"anagrams",function() {
|
||||||
|
var words = options.wiki.getTiddlerText(WORD_LIST_TITLE,"").split("\n"),
|
||||||
|
result = {};
|
||||||
|
console.log(words.length + " words in dictionary")
|
||||||
|
$tw.utils.each(words,function(word) {
|
||||||
|
var normalisedWord = normaliseLetters(word);
|
||||||
|
if(!(normalisedWord in result)) {
|
||||||
|
result[normalisedWord] = [];
|
||||||
|
}
|
||||||
|
result[normalisedWord].push(word);
|
||||||
|
});
|
||||||
|
console.log(Object.keys(result).length + " anagrams found")
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
// Take the anagram of each input word
|
||||||
|
var results = [];
|
||||||
|
source(function(tiddler,title) {
|
||||||
|
// Check if the title contains wildcards
|
||||||
|
if(title.indexOf("?") === -1) {
|
||||||
|
// If there are no wildcards we can just pull out the anagrams directly
|
||||||
|
var anagrams = anagramList[normaliseLetters(title)];
|
||||||
|
if(anagrams) {
|
||||||
|
Array.prototype.push.apply(results,anagrams);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return results;
|
||||||
|
};
|
||||||
|
|
||||||
|
})();
|
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"title": "$:/plugins/tiddlywiki/words",
|
||||||
|
"name": "Words",
|
||||||
|
"description": "Word lists for games and inspiration",
|
||||||
|
"list": "readme examples"
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
title: $:/plugins/tiddlywiki/words/readme
|
||||||
|
|
||||||
|
!! Dictionary
|
||||||
|
|
||||||
|
!! Filter Operators
|
||||||
|
|
||||||
|
!!! `anagrams` operator
|
||||||
|
|
||||||
|
The anagrams operator takes as input a list of words and returns a list of English anagrams of those words. Any input items that are not English words (eg because they contain whitespace or non-Latin characters) will not return any anagrams.
|
||||||
|
|
||||||
|
The following special characters are recognised:
|
||||||
|
|
||||||
|
* `?` stands for any letter
|
||||||
|
|
Loading…
Reference in New Issue