# -*- coding: UTF-8 -*-
import sys
sys.path.append("../")
from whoosh.index import create_in,open_dir
from whoosh.fields import *
from whoosh.qparser import QueryParser

from jieba.analyse import ChineseAnalyzer 

analyzer = ChineseAnalyzer()

schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT(stored=True, analyzer=analyzer))
ix = create_in("tmp", schema) # for create new index
#ix = open_dir("tmp") # for read only
writer = ix.writer()

writer.add_document(
    title="document1", 
    path="/a",
    content="This is the first document we’ve added!"
)

writer.add_document(
    title="document2", 
    path="/b",
    content="The second one 你 中文测试中文 is even more interesting! 吃水果"
)

writer.add_document(
    title="document3", 
    path="/c",
    content="买水果然后来世博园。"
)

writer.add_document(
    title="document4", 
    path="/c",
    content="工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作"
)

writer.add_document(
    title="document4", 
    path="/c",
    content="咱俩交换一下吧。"
)

writer.commit()
searcher = ix.searcher()
parser = QueryParser("content", schema=ix.schema)

for keyword in ("水果世博园","你","first","中文","交换机","交换"):
    print "result of ",keyword
    q = parser.parse(keyword)
    results = searcher.search(q)
    for hit in results:  
        print hit.highlights("content")
    print "="*10

for t in analyzer("我的好朋友是李明;我爱北京天安门;IBM和Microsoft; I have a dream."):
    print t.text

@@ -181,16 +181,25 @@
 .analyse
+.analyzer
  import 
@@ -213,17 +213,16 @@
 Analyzer
- 
 %0A%0Aanalyz
@@ -427,16 +427,31 @@
 ir(%22tmp%22
+, schema=schema
 ) # for 
@@ -1174,17 +1174,17 @@
    print
- 
+(
 %22result 
@@ -1195,16 +1195,17 @@
 ,keyword
+)
 %0A    q =
@@ -1299,17 +1299,17 @@
    print
- 
+(
 hit.high
@@ -1325,16 +1325,17 @@
 ontent%22)
+)
 %0A    pri
@@ -1340,15 +1340,16 @@
 rint
- 
+(
 %22=%22*10
+)
 %0A%0Afo
@@ -1429,12 +1429,13 @@
 rint
- 
+(
 t.text
+)
 %0A

fix invalid syntax for python3