Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
119 lines (75 sloc) 5.05 KB
This is an effort to implement Explicit Semantic Analysis (ESA) as described in this paper:
"Wikipedia-based semantic interpretation for natural language processing"
2009, Gabrilovich, E. and Markovitch, S.
You can find this paper at:
Create a MySQL database 'wiki':
mysql -u root -p
This implementation consists of:
* : that reads Wikiprep output into a MySQL database.
It creates "article","text" and "pagelinks" tables.
* : that adds anchor text to target articles.
* : that adds redirect text to target articles.
The scripts above are able to work on both Wikiprep legacy formats and modern format (as in Zemanta fork).
Evgeniy Gabrilovich provides a preprocessed dump for 5 November 2005 snapshot of Wikipedia English.
It is available at:
In its current settings, Python scripts of wikiprep-esa are ready to process this dump.
If you need to process dumps in formats of Zemanta, you need to set FORMAT in these scripts.
FORMAT can be following: "Gabrilovich", "Zemanta-legacy", "Zemanta-modern"
After reading preprocessed dump into the database and adding anchors and redirects, you need to use
"esa-lucene" to perform indexing.
* ESAWikipediaIndexer: performs indexing with Lucene by feeding it with article content from database.
* WikipediaNormalSearcher: at this step, you can use this class to perform a search in Lucene index.
keep in mind that at this point, the implementation won't be the same with Gabrilovich et al. (2009),
since cosine normalization is term-based in Gabrilovich et al. but document length based in Lucene.
Additionally, pruning is not yet applied in Lucene index as in Gabrilovich et al.
However, TF.IDF weighing scheme is the same (log-based) and is located in ESASimilarity class.
* IndexModifier: reads term frequency vectors from Lucene index and writes cosine-normalized TF.IDF values into
"tfidf" table in the database. This is done to apply the same normalization method used in Gabrilovich et al. (2009).
[DEPRECATED] * IndexPruner: prunes concept vectors for each term with a sliding window.
By default, window_size = 100 and threshold = 0.05 as in Gabrilovich et al. (2009). You can modify these values
in IndexPruner class.
* ESASearcher: performs search and computes vectors by using the resulting index in the database.
* TestESAVectors: produces and displays regular feature vector.
* TestGeneralESAVectors: produces and displays "Second Order Interpretation" vector filtered with "Concept Generality Filter" as in Gabrilovich et al. (2009).
Python scripts use MySQL-Python to access database.
Python scripts also use PyStemmer, which is the project encapsulating Python wrappers of Snowball:
You can find further info at:
"esa-lucene" Java project used for indexing, pruning etc. uses MySQL Connector/J to access database,
Lucene 3.0 for indexing and Trove and these libraries are included in project files.
MySQL Connector/J:
Lucene 3.0:
pyre2 [optional] will speed up all regular expression parsing.
hg clone re2.r56 && cd re2.r56 && hg update 56 && make -j4 && sudo make install
easy_install re2
NOTE: re2 does not correctly handle Unicode (see However, none of the regexes
use re.UNICODE, so I believe that its output will be identical.
WARNING: I should test re2 more. Sometimes the output is not what is
This creates the pagelinks table and records incoming and outgoing link counts.
python <hgw.xml file from Wikiprep dump>
As stop categories, a list "wiki_stop_categories.txt" is provided.
But if you want to descend down and include all subtrees of these categories, you can use:
python <hgw.xml file from Wikiprep dump> <cat_hier output path>
[The commands below are standard]
python <hgw.xml file from Wikiprep dump>
python <anchor_text file from Wikiprep dump> <a writeable folder>
java -cp esa-lucene.jar <Lucene index folder>
java -cp esa-lucene.jar <Lucene index folder>
... or, if you have a sufficient RAM (15 Gb was enough to process en-20090618 dump) try this instead:
java -cp esa-lucene.jar <Lucene index folder>
IndexModifier sorts TF-IDF vectors using sort utility of Unix, also using the disk.
MemIndexModifier handles sorting in memory instead.
Then perform a feature generation to test:
To generate regular features:
java -cp esa-lucene.jar
To generate features using only more general links:
java -cp esa-lucene.jar