diff --git a/components/search/onesearch/plainText.tsx b/components/search/onesearch/plainText.tsx new file mode 100644 index 0000000..c48f7ba --- /dev/null +++ b/components/search/onesearch/plainText.tsx @@ -0,0 +1,22 @@ +export default function (props: { children: React.ReactNode; selected: boolean }) { + if (props.selected) { + return ( +
+ {props.children} +
+ ); + } + else { + return ( +
+ {props.children} +
+ ); + } +} diff --git a/lib/nlp/stopwords.ts b/lib/nlp/stopwords.ts new file mode 100644 index 0000000..55dd3e5 --- /dev/null +++ b/lib/nlp/stopwords.ts @@ -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; +} \ No newline at end of file diff --git a/lib/onesearch/NLPResult.ts b/lib/onesearch/NLPResult.ts new file mode 100644 index 0000000..b37c207 --- /dev/null +++ b/lib/onesearch/NLPResult.ts @@ -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; + } +} diff --git a/lib/onesearch/baseCheck.ts b/lib/onesearch/baseCheck.ts new file mode 100644 index 0000000..7f17033 --- /dev/null +++ b/lib/onesearch/baseCheck.ts @@ -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; +}