# Word Sense Disambiguation Using Cosine Similarity Collaborates with Word2 vec and WordNet
## 3 Proposed Method
### Putting It Together  
Algorithm 1 shows the pseudocode of the proposed method, wsdw2v. First, a sentence \(S\), a target word \(w\), a context size \(c\), a similarity threshold \(\alpha\) and a number of best synsets \(n\_best\) must be given to a system. Line 1, the given sentence \(S\) is tokenized into a word list \(W\). Each word is annotated with its part-of-speech tag. The stop words are removed from the word list \(W\), and the remaining words are then lemmatized (i.e., a process of returning to the root word, e.g., eats \(\rightarrow\) eat, ate \(\rightarrow\) eat, eaten \(\rightarrow\) eat). Line 2, scanning through the word list \(W\) to find the target word \(w\) and return a target word index \(w_{i}\). Line 3, the context size \(c\) defines a number of words surrounding \(w_{i}\). Therefore, context \(C\) consisted of a list of the following words {\(w_{i\_02}\),...,\(w_{i\_2}\),\(w_{i\_1}\),\(w_{i\_1}\),\(w_{i\_2}\),...,\(w_{i\_02}\)} and then vectorized using Word2 vec to \(V_{C}\) with Line 4. Line 5, a word senses inventory \(s_{i}\) (a set of synsets) is retrieved corresponding to \(w_{i}\) and its part-of-speech tag from WordNet. Line 6, set a variable _Result_ to an empty dictionary. Line 8, set a variable _Score_ to 0. Line 9, each word sense \(s_{ij}\) (a synset) makes a signature sentence _Sig_ by concatenating its definition and relations from WordNet. Line 10, the signature sentence _Sig_ is also processed through tokenization, removing stop words, lemmatization and then vectorized using Word2 vec to \(V_{Sig}\) with Line 11. Line 12, the context sentence vector \(V_{C}\) and the signature sentence vector \(V_{sig}\) are then used to compute the variable _Score_ from \(cosim(V_{C}\), \(V_{sig})\). Line 13, if the variable _Score_ is not higher than the similarity threshold \(\alpha\), it will be combined with the probability of sense distribution \(P(s_{i,j}|w_{i})\) with Line 14 and the variable _Result_ is appended with a dictionary entry [\(s_{i,j}\) : _Score_] with Line 15. After all word senses are processed, \(n\_best\) senses (the synsets) are returned according to their scores as an output of the system with Line 16. The context size \(c\) and the similarity threshold \(\alpha\) are a system parameter which is to further investigated in the next section. Figure 2 shows some examples of wsdw2v (Jupyter notebook for wsdw2v is available at [[https://anaconda.org/korawit/wsdw2v/notebook](https://anaconda.org/korawit/wsdw2v/notebook)]([https://anaconda.org/korawit/wsdw2v/notebook](https://anaconda.org/korawit/wsdw2v/notebook))) to disambiguate word \"bank\" and \"plant\" in different contexts. The answer senses for each example are ranked by their scores. ```
1:a sentence \(S\), a target word \(w\), a context size \(c\), a similarity threshold \(\alpha\), a number of best synsets \(n\_best\)
2:\(n\_best\) disambiguate senses (synsets) of the target word \(w\)
3:\(W\)\(\leftarrow\)Lemmatized(RemoveStopwords(POStag(Tokenized(S))))
4:\(w_{i}\)\(\leftarrow\)GetTargetWordIndex(\(W\), \(w\))
5:\(C\)\(\leftarrow\)GetContext(\(W\), \(w_{i}\), \(c\))
6:\(V_{C}\)\(\leftarrow\)Word2 Vectorizer(C)
7:\(s_{i}\)\(\leftarrow\)GetSynsetsFromWordNet(\(w_{i}\), GetPOStag(\(w_{i}\)))
8:Result\(\leftarrow\)EmptyDict()
9:For each synset \(s_{ij}\) in \(s_{i}\) :
10: Score\(\leftarrow\)0
11:\(\textit{Sig}\)\(\leftarrow\)GetDefinition(\(s_{i,j}\))+GetDefinitionFromRelations(\(s_{i,j}\), \"hypernym, hyponym, meronym, holonym, entailment\")
12:\(\textit{Sig}\)\(\leftarrow\)Lemmatized(RemoveStopwords(Tokenized(\(Sig\))))
13:\(V_{Sig}\)\(\leftarrow\)Word2 Vectorizer(\(Sig\))
14: Score\(\leftarrow\)\(Cosim(V_{C}\), \(V_{Sig})\)
15: If Score \(<\alpha\) :
16: Score\(\leftarrow\)\(P(s_{ij}|w_{i})\)
17: Result\(\leftarrow\)Result + [\(s_{ij}\) : Score]
18:Return\(n\_best\) synsets from Result
```  
**Algorithm 1** Pseudocode of proposed method, wsdw2v
In order to enhance the scalability of the method when analyzing a bunch of documents, it is possible that some word senses have been retrieved several times from WordNet.