There are different ways that we can define add to index,
here's one way that works.
We're going to create a procedure, we'll call it add to index--
and it takes our 3 inputs--the index, the keyword, and the URL--
What we need to do in add to index is first find if the keyword already appears.
To do that, we need to look through all of the entries in index.
The natural way to do that is to use a 4-loop.
We're going to define the procedure add to index--
and it takes our 3 inputs, index, keyword and URL--
to help keep track of what we are doing,
I am going to draw a reminder of what the data structure of index is.
Remember that it is a list of entries,
and each entry is itself a list,
where the 1st part is a keyword, and the 2nd part is a list of URLs.
That's our data structure,
that's going to help us figure out what to do to define add to index.
The first thing we need to do
is to check whether the keyword already exists in the index.
If we can find it, well, then we want to modify that entry, rather than creating a new one.
The natural way to do that is to use a 4-loop.
We are going to loop through the elements of index.
We will give each one the name entry to use in the block.
This is what entry will be.
The first time through the loop, the value of entry will be a reference to the list here--
which is the first element of the index list.
Now we need to find the keyword.
The keyword is right here--that's the element at position zero of entry.
We're going to test the value at position zero on entry
identical to the keyword that's passed in
we'll use the double-equal comparison to test that--
If it is equal, then we found a match--
this means we want to append the URL to the list of URLs associated with that entry.
To get that list of URLs
we want to find entry 1--that's the value at position 1 of entry--
and we want to append that the new URL.
Here we've found an entry that matches the keyword we were looking for--
this means the keyword is already in the index--
we've added the new URL to the URLs associated with that keyword--
so we're done, we have nothing else to do.
What we want to make sure, is that we don't continue and and do anything else.
One approach would be to use break---that would end the loop--
what we want to do instead is really end the whole procedure.
If we did break, well, then we'd still have the problem of how do we deal with the case
where the keyword wasn't found?
Here we're just going to return--
we're all done with add to index, we've added the URL it belongs.
Now we need to think about what to do in the case
where the keyword does not already exist in the index.
If that's the case, then we get to the end of the loop without ever finding that entry.
If we've got to the end of the loop,
that means we did not find any entry in the index that matches the keyword,
then what we want to do is add a new entry---
and that new entry is going to have, as its value,
a list containing 2 elements, it will be the keyword--
and as the 2nd element
we'll have a list containing the URLs that we found that have that keyword.
So far we only have 1--the URL that was passed in to add to index.
How do we do that?
To add a new element to add to index we use append.
We need something to pass in to append--that is the structure we want to add.
This whole thing is what we want to add--
so, that's a list containing the value keyword as its 1st element.
As the 2nd element, its a list containing just the single URL.
That's what we want to append.
in the case where we didn't already find the keyword in the index.
Ci sono diversi modi con i quali possiamo definire "addtoindex"
eccone uno che funziona.
Andremo a creare una procedura, che chiameremo "addtoindex"--
che riceve 3 parametri-- l'indice, la parola chiave, e l'URL--
Quello che dobbiamo fare è aggiungere ad "index" è per prima cosa vedere se la parola chiave è già inserita
Per fare questo dobbiamo cercare su tutte le voci di "index"
Normalmente si fa ciò usando un ciclo "for".
Definiremo la procedura "addtoindex"--
riceve tre parametri, "index", "keyword", "url"--
per aiutarci a tenere traccia di quello che stiamo facendo,
disegnerò un promemoria di quello che è la struttura dati di "index"
Ricordate che si tratta di una lista di voci
e ciascuna voce è essa stessa una lista
la cui prima parte è una parola chiave e la seconda parte è una lista di URL
Ecco la nostra struttura dati
che ci aiuterà a capire cosa fare per definire "addtoindex"
La prima cosa che dobbiamo fare
è verificare se la parola chiave esiste già nell'indice
Se la possiamo trovare, bene, allora vogliamo modificare quella voce, piuttosto che crearne una nuova.
Il modo naturale per farlo è usare un ciclo "for"
Itereremo attraverso gli elementi di "index"
Chiameremo ciascuno elemento "entry" per usarlo nel blocco
Questo è quello che sarà "entry"
La prima volta che attraversiamo il ciclo, il valore di "entry" sarà un riferimento alla lista qui--
che è il primo elemento della lista in "index"
Ora dobbiamo trovare la parola chiave
La parola chiave è proprio qui--è l'elemento a posizione zero di "entry"
Verificheremo se il valore a posizione zero su "entry"
è identico a quello della parola chiave ricevuta
useremo l'operatore di comparazione == per verifcare ciò--
Se è uguale, allora abbiamo una corrispondenza--
il che vuole dire che vogliamo aggiungere l'URL alla lista di URL associati con quella voce
Per ottenere quella lista di URL
vogliamo trovare "entry[1]"--che è il valore a posizione 1 di "entry"
e vogliamo accodare quel nuovo URL
Qui abbiamo trovato una voce che corrisponde alla parola chiave che stavamo cercando--
il che significa che la parola chiave è già nell'indice
abbiamo aggiunto il nuovo URL agli URL associati a quella parola chiave
abbiamo finito, non abbiamo altro da fare.
Quello di cui vogliamo essere sicuri, è che non continuiamo e non facciamo altro.
Un approccio sarebbe usare "break"--che terminerebbe il ciclo--
quello che invece vogliamo fare è terminare l'intera procedura.
Se interrompiamo con "break", avremmo ancora il problema di come affrontare il caso
nel quale la parola chiave non è stata trovata.
Quindi semplicemente useremo "return"--
abbiamo finito con "addtoindex", abbiamo aggiunto l'URL al posto giusto.
Ora dobbiamo pensare a cosa fare nel caso dove
la parola chiave non esiste già nell'indice
Se questo è il caso, allora ci troveremo alla fine del ciclo senza avere mai trovato quella voce
Se arriviamo alla fine del ciclo
significa che non abbiamo trovato alcuna voce nell'indice che corrisponde alla parola chiave
quello che vogliamo fare è aggiungere una nuova voce--
e detta nuova voce avrà, come suo valore,
una lista che contiene 2 elementi, la parola chiave--
e come secondo elemento
avrà una lista che contiene l'URL che abbiamo trovato che ha quella parola chiave
Fino ad ora ne abbiamo uno--l'URL ricevuto come parametro di "addtoindex"
Come facciamo?
Per aggiungere un nuovo elemento ad "addtoindex" utilizziamo "append"
Ci serve qualcosa da passare ad "append"-- vale a dire la struttura che vogliamo aggiungere
Tutto questo è quello che vogliamo aggiungere--
quindi, ecco una lista che contiene il valore di "keyword" come primo elemento
e come secondo, è una lista che contiene un singolo URL
Ecco quello che vogliamo aggiungere
Nel caso in cui non abbiamo trovato la parola chiave nell'indice
インデックスに追加することを
定義する様々な方法があります
これは機能する方法の1つです
add_to_indexという関数を作成します
3つの引数index、keyword、urlを使います
add_to_indexではまずkeywordが
すでに現れているかどうかを把握する必要があります
そのためにindexのすべてのentryを
見ていく必要があります
forループを使うことが一般的です
add_to_index関数を定義します
3つの引数index、keyword、urlを使用します
indexのデータ構造が
どんなものだったか忘れないように書いていきます
これがentryのリストであることや
それぞれのentry自体がリストであること
そのリストの1つ目がkeywordで
2つ目がurlのリストであることを忘れないでください
これが私たちのデータ構造です
これはadd_to_indexを定義するために
処理内容を解明するのに役立ちます
初めに行うのはkeywordがすでにindexにあるかどうか
チェックすることです
もし見つけられたら新しいものを作成するのではなく
そのentryを変更します
一般的にはforループを使って行います
indexの要素をループします
ブロックで使用するために
それぞれの要素をentryに代入します
そしてentryはこうなります
1回目のループが終了すると
entryの値はこのリストを参照します
それはindexのリストの1つ目の要素です
ここでkeywordを見つける必要があります
keywordはここにあります
entry[0]の位置にある要素です
keywordに一致するentry[0]にある値を
テストします
それをテストするために
ダブルイコールの比較を使用します
もし同等なら一致したということです
つまりurlをそのentryに関連するurlのリストに
appendするということです
urlのそのリストを得るために
entry[1]を見つける必要があります
それがエントリの位置1にある値です
その新しいurlをappendする必要があります
これが探していたkeywordに一致するentryです
これはkeywordが
すでにindexにあることを意味しています
新たなurlをそのkeywordに関連するurlに追加します
これで終了です 他に処理することはありません
継続しないかどうか他に行うことがないかを
確認しましょう
breakを使う方法もあります
これはループを終わらせるものです
関数全体を終了させたいと思います
もしbreakを行った場合
keywordが見つけられなかった場合に
どう対処するかという問題が残ります
ここで返します
add_to_indexで行うことは完了しました
urlをその関連がある先に追加しました
keywordがすでにindexに存在していなかった場合
何を行うか考える必要があります
それが事実ならそのentryを見つけられないまま
ループを終了させなければなりません
ループを終了させた場合
keywordに一致するentryがindexで
見つけられなかったことになります
そこで新たなentryを追加します
新たなentryには値として
2つの要素を含むリストがあります
それがkeywordです
そして2つ目の要素には
そのkeywordを見つけたurlを含んだリストが
あります
今のところadd_to_indexに渡したurlは
1つしかありません
それをどのように行うのでしょう?
新たな要素をadd_to_indexに加えるため
appendを使用します
何かをappendに渡す必要があります
それが追加したい構造になります
これら全部を追加しましょう
1つ目の要素としてkeywordを含んでいるリストです
2つ目の要素として1つのurlを含んでいるリストです
これがappendする必要があるものです
これはindexにkeywordが見つからなかった場合です
定义添加索引的方法很多
现在介绍其中一种
我们将创建一个程序,叫做添加索引
包含三次输入:索引、关键词、和URL
添加索引中,首先需要确认这个关键词是否已经出现过
那么,就需要对索引中的所有元素进行检索
正常做法是使用for循环
所以是先定义添加索引这一程序
这需要三次输入,索引、关键词、和URL
来帮助我们对现在所做工作进行跟踪
我先画下索引的数据结构作为提醒
请记得这是一个元素链表
每个元素本身也是一个链表
第一部分是关键词,第二部分是URL链表
这是我们的数据结构
这一概念会帮我们了解如何对添加索引进行定义
第一个任务
是检查关键词是否已经存在于索引中了
如果找到了该关键词,则需修改元素,而不要创建新元素
如前,通常做法是使用for循环
我们会对索引中的元素进行循环
将之称作元素,用在程序块中
这就是元素了
第一次循环,元素值将会作为这个链表的参考
这是索引链表的第一个元素
现在需要找出关键词
就在这里,条目零位置上的元素
我们会对该位置的值做出测试
是否与传入关键词吻合
所以将用双等于号来测试
如果相等,那么我们找到了匹配词
即需要将URL附加如URL链表中与该元素建立关联
要获得URL链表
需要找到元素1,就是位置1上的值
还要附加新URL
这里,我们找到一个元素与我们寻找的关键词相符
这说明关键词已经包含在索引里了
我们也往URL链表中添加了新URL,并与该关键词建立了关键
所以,大功告成
需要确认的是,我们停下来不再进行其它变动
一个步骤不对,就可能终止该循环
所以我们应该结束整个程序
如果真破坏了循环,那么就仍然有如何应对的问题
在哪里没找到这个关键词?
我们就回到这个位置
添加索引已经完成,也添加了属于该索引的URL
现在则要思考,在这个情况下
即索引中尚无该关键词时怎么做了
如果没有,那么不用找到该元素,也要结束循环
如果终止这一循环
就意味着我们在索引中未找到任何匹配关键词的元素
那么就需要添加新元素
这个新元素,作为其价值
需要一个包含两个元素的链表,这就是关键词了
第二个元素
则是包含URL的链表,这些URL包含该关键词
目前我们只有1,即为添加索引而传入的URL
该如何处理?
在添加索引中加入新元素,我们的做法是附加
我们需要传入来进行附加,这就是需要添加的结构
这一整套就是我们要添加的内容
这是一个链表,包含了关键词这一值作为第一个元素
第二个元素是包含了单个URL的链表
这是我们
在索引中未找到关键词时要附加的内容
有很多不同的方法可以定義 add-to-index
這是其中一種
我們將建立一個程序,稱為 add-to-index
需要三個輸入:index、keyword、和 url
在 add-to-index,首先確認這個關鍵字是否已經出現過
那麼,我們需要對 index 的所有項目進行瀏覽
最自然的做法是使用 for loop
我們定義程序 add-to-index
需要三個輸入,index、keyword、和 url
為了幫忙追蹤進行的過程
我先畫下 index 的資料結構作為提醒
請記得這是一個項目列表
每個項目本身也是一個列表
第一部分是 keyword,第二部分是 url 列表
這是我們的資料結構
這會幫助我們了解如何定義 add-to-index
我們需要做的第一件事
是檢查 keyword 是否存在於 index 中
如果找到 keyword,
我們要修改項目,而不是建一個新的項目
最自然的做法是使用 for loop
我們會走遍 index 的每個元素
在程序塊裡,我把它們稱為 entry
這就是 entry
第一次經過 loop,entry 指向這個列表
這是 index 列表的第一個元素
現在需要找出 keyword
keyword 就在這裡,entry 位置 0 的元素
我們會對 entry 位置 0 的值做測試
是否與傳入的 keyword 吻合
使用 '==' 比較式來測試
如果相等,那麼我們找到了符合的 keyword
也就是說,我們要將 url 加到 url 列表中,
與這個 entry 相關聯的 url 列表
為了得到 url 列表
要找到 entry[1],就是 entry 位置 1 的值
我們要加入新的 url
這裡,我們找到一個 entry 與我們尋找的 keyword 相符
這意謂著 keyword 已經在 index 裡了
我們加入新的 url 到與該關鍵字相關的 url 列表裡
我們完成了,沒有其他的事要做
要確認的是,我們停下來不再做其他的事
一種方法是使用 break,它可以終止 loop
所以我們要結束整個程序
寫了 break,仍然得面對如何處理
沒找到 keyword 的問題
我們走到 return
已經完成 add-to-index ,也將 url 加到它應該的位置
現在則要思考
在keyword 不存於 index 的情況下,該怎麼做?
在這情形下,我們走到 loop 的末尾,沒有找到任何項目
如果走到 loop 的末尾
就意謂著,我們在 index
沒有找到任何符合 keyword 的項目
那麼,我們要加入新的 entry
這個新的 entry,它的值是一個列表
包含兩個元素,有 keyword
作為第二個元素
有包含 url 的列表,這些 url 包含這個關鍵字
目前我們只有 1 個,傳入了 add-to-index 的 url
該如何處理呢?
在 add-to-index 加入新元素,我們使用 append
我們需要傳入某個值到 append ,
我們要加入的資料結構
這整個就是我們要加入的內容
這是一個列表,包含了 keyword 作為第一個元素
第二個元素是包含了單個 url 的列表
在 index 沒有找到 keyword 的情況下
這是我們要加入的內容