StringManager.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<#
    Defining main class [StringManager]:
     
    // Using Classes to develop the word combinations :: NOTES ::
        What is a class? - A Class is a blueprint which is used to create the instance of an object at run time.
        That is, when a class is instantiated the object is the instance of your class with certain properties
        and methods.
        Why classes? - There are many advantages of using classes in the script. The main advantage of using classes
        is code reuse-ability and redundancy. All the instances can be encapsulated in a single class and the main
        advantage is that formatting output.
 
    // Use Case ::
        The StringManager class gives the flexibility of picking the random alphabets from alphabet library
        and it actually forms all possible words with it. And finally it finds the synonyms for the words formed
        which are meaningful and return as an object.
        Now, the result can be formatted to various outputs for better visualisation.
 
    // Class Functionality ::
        StringManager class have multiple functionalities. They are -
            1. Pick alphabets for given number/s randomly and forms a word from the randomly picked alphabets
                E.g. "abc"
            2. Find the factorial of formed word. Plainly, it can give the number of possible combinations
            that can be formed from the formed word.
                E.g. Possible combinations that can be made from "abc" is 6.
            3. Remove the repeated letters in the word.
                E.g. "aabbc" Will be formatted to "abc"
            4. Find the word combinations or permutations for the word.
                E.g. for "abc" the word combinations are ( "abc","acb","bac","bca","cab","cba")
            5. Find the synonyms for meaningful word.
                E.g. Synonym for "cab" is taxi.
 
    // Notes ::
        Author : B Harish Karthic
        Date : 03/01/2020
        Version : v1.1
        Comments : Initial Script
 
    // Examples ::
        Instantiating the class -
        E.g. Instantiating a new class without passing arguments -
        $MyString = [StringManager]::new()
 
        Instantiating a new class by passing one argument -
        $MyString = [StringManager]::new(3)
         
        Instantiating a new class by passing two arguments -
        $MyString = [StringManager]::new(3,"Fun")
 
        Getting random alphabets from alphabets library
        $Word = $MyString.GetRandomAlphabets(3)
 
        Assigning the property to get random alphabets
        $Word = $MyString.GetRandomAlphabets($MyString.Number)
         
        Getting number of combinations that can be made from the word
        $MyString.GetFactorial($Word.Length)
 
        Removing repeated letters from the word
        $MyString.RemoveDuplicateLetters($Word)
 
        Generating all possible permutations of the word
        $Permutations = $MyString.GetPermutations($Word)
 
        Getting synonyms for generated permutations
        $MyString.GetSynonyms($Word)
#>


## Defining main class StringManager
Class StringManager {

    ## Defining Properties
    [int]$Number = 0
    [string]$String = "StringManager"

    ## hidden property
    hidden [array]$AlphabetsLibrary = @(for($i=0;$i -lt ([Char[]](97..122)).Count;$i++) { ([Char[]](97..122))[$i] })

    ## Constructors
    StringManager() {
        ## pass
    }

    StringManager([int]$Number) {
        $this.Number = $Number
    }

    StringManager([int]$Number,[string]$String) {
        $this.Number = $Number
        $this.String = $String
    }

    ## getting factorial of given number
    [bigint] GetFactorial([bigint]$number) {

        if($number -le 1) {
            return $number
        }
        else {
            return $number * $this.GetFactorial($number-1)           
        }        
    }

    ## getting random letters from AlphabetsLibrary
    [String] GetRandomAlphabets([int]$Number) {

        if($Number -lt 1) {
            return ""
        }
        else {
            $counter = 1
            $Word = ""

            While($counter -le $Number) { 
                $counter = $counter+1
                $Word += Get-Random $this.AlphabetsLibrary
            }
            return $Word
        }
    }

    ## removing repeated letters in the string
    [string] RemoveDuplicateLetters([string]$Word) {

        if(-not $PSBoundParameters.ContainsKey("Word")) {
            return ""
        }
        else {
            $FinalWord = ""
            $Word = ($Word.ToCharArray() | Group-Object -NoElement).Name

            foreach($letter in $Word) {
                $FinalWord += $letter
            }
            $FinalWord = $FinalWord -replace " ",""
            return $FinalWord
        }
    }

    ## finding string permutations
    [array] GetPermutations([string]$PermutationWord) {

        if ($PermutationWord.Length -eq 0) {
            return ""
        }
    
        elseif ($PermutationWord.Length -eq 1) {
            return $PermutationWord
        }
    
        else {
            $PermWord = @()
            $counter = $PermutationWord.Length
        
            for($i=0;$i -lt $PermutationWord.Length;$i++) {
                $FirstLetter = $PermutationWord[$i]
                $RemainingLetters = $PermutationWord.Substring(0,$i) + $PermutationWord.Substring(($i+1),($counter-1))
                $counter -= 1
    
                foreach($letter in $this.GetPermutations($RemainingLetters)) {
                    $PermWord += $FirstLetter + $letter
                }          
            }
            return $PermWord
        }
    }

    ## finding synonyms of the word/s
    [array] GetSynonyms([String[]]$Words) {
        
        $result = @()
        $Uri = "https://www.synonym.com/synonyms"

        foreach($Word in $Words) {

            try {
                $WebRequest = Invoke-WebRequest -Uri "$($Uri)/$($Word)" -UseBasicParsing
        
                $startingIndex = $WebRequest.Content.IndexOf("synonyms:")
                $endingIndex = $WebRequest.Content.IndexOf("| ")
                $synonym = $WebRequest.Content.Substring($startingIndex,$endingIndex)
                $antonym = $synonym.IndexOf("antonyms")
                $synonyms_new = $synonym.Substring(0,$antonym) -split "," -replace "|",""
                $Synonyms = foreach($item in $synonyms_new) { if($item.Contains("|")) { $item.Replace("|","") } }
                $Hash = [PSCustomObject]@{
                    "Words" = $Word
                    "Synonyms" = $Synonyms
                }
                $result += $Hash
            }
            catch {
                $Hash = [PSCustomObject]@{
                    "Words" = $Word
                    "Synonyms" = "No synonym for the word!"
                }
                $result += $Hash
            }
        }
        return $result
    }

}