feature: base64 detect and auto-decode
This commit is contained in:
parent
136450f93d
commit
1d7aaf6a8a
22
components/search/onesearch/plainText.tsx
Normal file
22
components/search/onesearch/plainText.tsx
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
export default function (props: { children: React.ReactNode; selected: boolean }) {
|
||||||
|
if (props.selected) {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={`w-full h-auto leading-6 break-all py-[0.6rem] bg-zinc-300 dark:bg-zinc-700
|
||||||
|
px-5 z-10 cursor-pointer duration-100`}
|
||||||
|
>
|
||||||
|
{props.children}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={`w-full h-auto leading-6 break-all py-[0.6rem] bg-zinc-100 hover:bg-zinc-300
|
||||||
|
dark:bg-zinc-800 hover:dark:bg-zinc-700 px-5 z-10 cursor-pointer duration-100`}
|
||||||
|
>
|
||||||
|
{props.children}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
8
lib/nlp/stopwords.ts
Normal file
8
lib/nlp/stopwords.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
const stopwords = ["a","about","above","after","again","against","all","am","an","and","any","are","aren't","as","at","be","because","been","before","being","below","between","both","but","by","can't","cannot","could","couldn't","did","didn't","do","does","doesn't","doing","don't","down","during","each","few","for","from","further","had","hadn't","has","hasn't","have","haven't","having","he","he'd","he'll","he's","her","here","here's","hers","herself","him","himself","his","how","how's","i","i'd","i'll","i'm","i've","if","in","into","is","isn't","it","it's","its","itself","let's","me","more","most","mustn't","my","myself","no","nor","not","of","off","on","once","only","or","other","ought","our","ours ourselves","out","over","own","same","shan't","she","she'd","she'll","she's","should","shouldn't","so","some","such","than","that","that's","the","their","theirs","them","themselves","then","there","there's","these","they","they'd","they'll","they're","they've","this","those","through","to","too","under","until","up","very","was","wasn't","we","we'd","we'll","we're","we've","were","weren't","what","what's","when","when's","where","where's","which","while","who","who's","whom","why","why's","with","won't","would","wouldn't","you","you'd","you'll","you're","you've","your","yours","yourself","yourselves"];
|
||||||
|
|
||||||
|
export default function removeStopwords(str: string, extraStopwords: string[] = []){
|
||||||
|
for (let word of stopwords.concat(extraStopwords)){
|
||||||
|
str = str.replace(new RegExp(`\\b${word}\\b`, 'gi'), '');
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
16
lib/onesearch/NLPResult.ts
Normal file
16
lib/onesearch/NLPResult.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
export class NLPResult {
|
||||||
|
constructor(
|
||||||
|
public suggestion: string | null,
|
||||||
|
public intention: string | null,
|
||||||
|
public probability: number,
|
||||||
|
public confidence: number,
|
||||||
|
public relevanceBase: number = 2000,
|
||||||
|
public confidenceWeight: number = 0.2,
|
||||||
|
public type: string = "text",
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
get relevance(): number {
|
||||||
|
return this.relevanceBase * this.probability + this.confidence * this.relevanceBase * this.confidenceWeight;
|
||||||
|
}
|
||||||
|
}
|
74
lib/onesearch/baseCheck.ts
Normal file
74
lib/onesearch/baseCheck.ts
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import removeStopwords from "../nlp/stopwords";
|
||||||
|
import { NLPResult } from "./NLPResult";
|
||||||
|
|
||||||
|
interface KeywordsDict {
|
||||||
|
[key: string]: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IntentionsDict {
|
||||||
|
[key: string]: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validBase64(str: string) {
|
||||||
|
return str.length % 4 == 0 && /^[A-Za-z0-9+/]+[=]{0,2}$/.test(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function base64NLP(str: string) {
|
||||||
|
const keywords: KeywordsDict = {
|
||||||
|
base64: 1,
|
||||||
|
b64: 0.95,
|
||||||
|
base: 0.5
|
||||||
|
};
|
||||||
|
let result = new NLPResult(null, null, 0.0, 0.0);
|
||||||
|
for (let keyword of Object.keys(keywords)) {
|
||||||
|
const pos = str.trim().indexOf(keyword);
|
||||||
|
const l = str.length;
|
||||||
|
const w = str.split(" ").length;
|
||||||
|
if (w > 1 && (pos === 0 || pos == l)) {
|
||||||
|
result.probability += keywords[keyword];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const intentions: IntentionsDict = {
|
||||||
|
decode: 0.1,
|
||||||
|
encode: 1
|
||||||
|
};
|
||||||
|
for (let intention of Object.keys(intentions)) {
|
||||||
|
const pos = str.trim().indexOf(intention);
|
||||||
|
const l = str.length;
|
||||||
|
const w = str.split(" ").length;
|
||||||
|
if (w > 1 && pos !== -1) {
|
||||||
|
result.confidence += intentions[intention];
|
||||||
|
result.intention = `base64.${intention}`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let processedQuery = removeStopwords(str, Object.keys(keywords).concat(Object.keys(intentions))).trim();
|
||||||
|
if (result.intention === "base64.decode"){
|
||||||
|
if (validBase64(processedQuery)) {
|
||||||
|
result.confidence = 1;
|
||||||
|
} else {
|
||||||
|
result.confidence = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (validBase64(processedQuery)) {
|
||||||
|
result.intention = "base64.decode";
|
||||||
|
result.confidence += Math.max(1 / Math.log10(1 / processedQuery.length) + 1, 0);
|
||||||
|
result.probability += Math.max(1 / Math.log10(1 / processedQuery.length) + 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (result.intention) {
|
||||||
|
case "base64.encode":
|
||||||
|
result.suggestion = btoa(processedQuery);
|
||||||
|
break;
|
||||||
|
case "base64.decode":
|
||||||
|
result.suggestion = atob(processedQuery);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user