Skip to main content
Create new
Introduction
Reactivity
Props
Logic
Events
Bindings
Lifecycle
Stores
Motion
Transitions
Animations
Easing
SVG
Actions
Classes
Component composition
Context API
Special elements
Module context
Debugging
7GUIs
Miscellaneous
App.svelte
<script>
let text1 = "I";
let text2 = "love dogs";
let text3 = "dogs dogs dogs";

$: tokens = (text1 + " " + text2 + " " + text3)
.replace(/[^\w\s]/gi, "")
.toLowerCase();
$: vocab = Array.from(new Set(tokens.split(" ")))
.filter(el => el !== "")
let mode = "count";


function countWords(vocab, text, mode) {
let count = {};
let cleanedText = text.replace(/[^\w\s]/gi, "").toLowerCase()

for (const word of vocab) {
if (mode === "count") {
count[word] = cleanedText.split(" ").filter((token) => token === word).length;
} else {
count[word] = cleanedText.split(" ").filter((token) => token === word).length
? 1
: 0;
}
}

return count;
}
$: text1Count = countWords(vocab, text1, mode);
$: text2Count = countWords(vocab, text2, mode);
$: text3Count = countWords(vocab, text3, mode);
</script>
loading Svelte compiler...
/* Select a component to see its compiled code */
result = svelte.compile(source, {
generate: ,
});
/* Select a component to see its compiled code */