<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="et">
	<id>http://www.lambda.ee/w/index.php?action=history&amp;feed=atom&amp;title=KR2020lab3ex</id>
	<title>KR2020lab3ex - Redigeerimiste ajalugu</title>
	<link rel="self" type="application/atom+xml" href="http://www.lambda.ee/w/index.php?action=history&amp;feed=atom&amp;title=KR2020lab3ex"/>
	<link rel="alternate" type="text/html" href="http://www.lambda.ee/w/index.php?title=KR2020lab3ex&amp;action=history"/>
	<updated>2026-10-28T20:37:48Z</updated>
	<subtitle>Selle lehekülje redigeerimiste ajalugu</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>http://www.lambda.ee/w/index.php?title=KR2020lab3ex&amp;diff=17264&amp;oldid=prev</id>
		<title>PriitJ: Uus lehekülg: &#039;Some more code examples to get started with lab 3.  == Question Parsing with SpaCy ==  Loading SpaCy:  &lt;pre&gt; import spacy # do this once from command line: # python -m spacy down...&#039;</title>
		<link rel="alternate" type="text/html" href="http://www.lambda.ee/w/index.php?title=KR2020lab3ex&amp;diff=17264&amp;oldid=prev"/>
		<updated>2020-04-18T18:08:54Z</updated>

		<summary type="html">&lt;p&gt;Uus lehekülg: &amp;#039;Some more code examples to get started with lab 3.  == Question Parsing with SpaCy ==  Loading SpaCy:  &amp;lt;pre&amp;gt; import spacy # do this once from command line: # python -m spacy down...&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Uus lehekülg&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Some more code examples to get started with lab 3.&lt;br /&gt;
&lt;br /&gt;
== Question Parsing with SpaCy ==&lt;br /&gt;
&lt;br /&gt;
Loading SpaCy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import spacy&lt;br /&gt;
# do this once from command line:&lt;br /&gt;
# python -m spacy download en&lt;br /&gt;
nlp = spacy.load(&amp;quot;en&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A ridiculously simple parser for questions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import spacy.symbols as sym                                                     &lt;br /&gt;
&lt;br /&gt;
def parse(txt):&lt;br /&gt;
    doc = nlp(txt)&lt;br /&gt;
    verbs = []&lt;br /&gt;
    for word in doc:&lt;br /&gt;
        if word.dep in [sym.nsubj, sym.nsubjpass, sym.pobj]:&lt;br /&gt;
            chunk, verb = find_upward_verb(word)&lt;br /&gt;
            if verb != None:&lt;br /&gt;
                verbs.append((verb, chunk))&lt;br /&gt;
        # uncomment this to see what the &amp;quot;parser&amp;quot; sees&lt;br /&gt;
        #print(word, word.dep_, word.pos_, word.head)&lt;br /&gt;
    return verbs&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code above assumes that the input is a single sentence. It matches verbs with their subjects and objects. This function collects the relevant parts from the tree of the sentence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def find_upward_verb(word):                                                     &lt;br /&gt;
    words = []                                                                  &lt;br /&gt;
    verb = None                                                                 &lt;br /&gt;
    while word.dep_ != &amp;quot;ROOT&amp;quot;:                                                  &lt;br /&gt;
        words.append(word)                                                      &lt;br /&gt;
        word = word.head                                                        &lt;br /&gt;
        if word.pos in [sym.VERB, sym.AUX]:                                     &lt;br /&gt;
            verb = word                                                         &lt;br /&gt;
            break                                                               &lt;br /&gt;
    words.reverse() # from top to down                                          &lt;br /&gt;
    return words, verb                                                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; parse(&amp;quot;what is estonia?&amp;quot;)&lt;br /&gt;
[(is, [estonia])]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; parse(&amp;quot;where is estonia?&amp;quot;)&lt;br /&gt;
[(is, [estonia])]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; parse(&amp;quot;where is estonia located?&amp;quot;)&lt;br /&gt;
[(located, [estonia])]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; parse(&amp;quot;what is located in estonia?&amp;quot;)&lt;br /&gt;
[(located, [what]), (located, [in, estonia])]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; parse(&amp;quot;who is the mayor of tallinn?&amp;quot;)&lt;br /&gt;
[(is, [who]), (is, [mayor, of, tallinn])]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This structure should be relatively easy to handle. For example, the third question is about the verb &amp;quot;located&amp;quot; and it&amp;#039;s subject or object is &amp;quot;estonia&amp;quot;. The returned objects are still SpaCy [https://spacy.io/api/token Tokens] so you can access their POS tags and other information. However, you probably need to additionally detect at least the &amp;quot;question&amp;quot; words, such as &amp;quot;where&amp;quot;, &amp;quot;what&amp;quot;, &amp;quot;who&amp;quot; etc. to properly interpret the question.&lt;br /&gt;
&lt;br /&gt;
Obviously, a lot more work is required to parse longer questions or handle finesse like the word &amp;quot;not&amp;quot; appearing somewhere.&lt;br /&gt;
&lt;br /&gt;
== Semantic Similarity ==&lt;br /&gt;
&lt;br /&gt;
Semantic similarity with word vectors is a hot topic and you will find hundreds of articles and guides online. Recent versions of SpaCy have a way to [https://spacy.io/api/vectors/#most_similar find words with similar meaning].&lt;br /&gt;
&lt;br /&gt;
This example uses pre-trained vectors that ship with SpaCy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy as np&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; # python -m spacy download en_core_web_lg&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; nlp = spacy.load(&amp;quot;en_core_web_lg&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; bla=nlp(&amp;quot;hill&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; keys, _, sim = nlp.vocab.vectors.most_similar(np.array([bla[0].vector]), n=10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; for k, s in zip(keys[0], sim[0]): print((nlp.vocab.strings[k], s))&lt;br /&gt;
... &lt;br /&gt;
(&amp;#039;HIll&amp;#039;, 1.0)&lt;br /&gt;
(&amp;#039;hill&amp;#039;, 1.0)&lt;br /&gt;
(&amp;#039;HILL&amp;#039;, 1.0)&lt;br /&gt;
(&amp;#039;Hill&amp;#039;, 1.0)&lt;br /&gt;
(&amp;#039;hills&amp;#039;, 0.795)&lt;br /&gt;
(&amp;#039;Hills&amp;#039;, 0.795)&lt;br /&gt;
(&amp;#039;HILLS&amp;#039;, 0.795)&lt;br /&gt;
(&amp;#039;mountain&amp;#039;, 0.6898)&lt;br /&gt;
(&amp;#039;MOUNTAIN&amp;#039;, 0.6898)&lt;br /&gt;
(&amp;#039;Mountain&amp;#039;, 0.6898)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to search within a limited set of terms then you need to make your own [https://spacy.io/api/vectors/ Vectors] object. This does not necessarily mean training your own vectors. If all your words are in SpaCy&amp;#039;s vocabulary, you can simply copy vectors from there.&lt;br /&gt;
&lt;br /&gt;
Note that vectors are not limited to just words in SpaCy. You can match against longer text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; nlp(&amp;quot;banana&amp;quot;).similarity(nlp(&amp;quot;yellow fruit&amp;quot;))&lt;br /&gt;
0.655803422700308&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; nlp(&amp;quot;banana&amp;quot;).similarity(nlp(&amp;quot;red fruit&amp;quot;))&lt;br /&gt;
0.6324075748339544&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Fuzzy Search ==&lt;br /&gt;
&lt;br /&gt;
Instead of words with similar meaning, we might want to find words that &amp;#039;&amp;#039;look&amp;#039;&amp;#039; similar. This is an annoying problem to solve efficiently. Using the right type of index can help a bit.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import editdistance&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import pybktree&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; # assuming labels contain the strings that we want to match against&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; fuzzy_index = pybktree.BKTree(editdistance.eval, labels)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; fuzzy_index.find(&amp;quot;Reikjavik&amp;quot;, 3)&lt;br /&gt;
[(1, &amp;#039;Reykjavik&amp;#039;), (3, &amp;#039;Miklavik&amp;#039;)]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unfortunately these tools do not scale too well beyond ~1M strings. If you have relatively few strings to match against, you can do a simple linear search and find the ones with lowest distance using &amp;lt;code&amp;gt;editdistance.eval()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== More Parsing ==&lt;br /&gt;
&lt;br /&gt;
The [https://chartbeat-labs.github.io/textacy/build/html/index.html textacy] package contains some wrappers for SpaCy functionality, for example matching with expressions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import textacy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; doc = nlp(&amp;quot;who is the mayor of tallinn?&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; print(list(textacy.extract.matches(doc,&lt;br /&gt;
...         [{&amp;quot;POS&amp;quot;:&amp;quot;ADP&amp;quot;},{&amp;quot;OP&amp;quot;:&amp;quot;*&amp;quot;},{&amp;quot;POS&amp;quot;:&amp;quot;PROPN&amp;quot;}])))&lt;br /&gt;
[of tallinn]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It also adds some new algorithms, such as finding the most important words:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import textacy.ke&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; textacy.ke.textrank(doc)&lt;br /&gt;
[(&amp;#039;tallinn&amp;#039;, 0.16844059871436393), (&amp;#039;mayor&amp;#039;, 0.1573258553341743)]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>PriitJ</name></author>
	</entry>
</feed>