#!/usr/bin/env python
#coding=utf-8

# @file match.py
# @brief 将paper表中的published_in与publication表中的name匹配
# @author x565178035,x565178035@126.com
# @version 1.0
# @date 2017-12-15 15:55

import sys

reload(sys)
sys.setdefaultencoding("utf-8")

# Django specific settings
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django
django.setup()
# Import your models for use in your script
from db.models import *

def match():
    #  abbv能匹配上

    publications=Publication.objects.filter(abbv__isnull=False)
    for each_pub in publications:
        print "abbv:",each_pub.abbv
        papers=Paper.objects.filter(published_in__contains=each_pub.abbv)
        for each_paper in filter(
                lambda e:each_pub.abbv in e.published_in\
                        .replace('(',' ')\
                        .replace(')',' ')\
                        .replace('/',' ')\
                        .replace('\'',' ')\
                        .split(' '),
                papers):
            print each_paper.published_in.decode('utf8')
            each_paper.published_in=each_pub.name
            each_paper.save()

    #  三个词以上组成的全称能匹配上
    publications=Publication.objects.all()
    for each_pub in publications:
        if len(each_pub.name.split(' '))>2:
            print "name:",each_pub.name
            papers=Paper.objects.filter(published_in__contains=each_pub.name).exclude(published_in=each_pub.name)
            for each_paper in papers:
                print each_paper.published_in.decode('utf8')
                each_paper.published_in=each_pub.name
                each_paper.save()

    #三个词以下的文献
    #  publications=Publication.objects.all()
    #  for each_pub in publications:
        #  if len(each_pub.name.split(' '))<=2:
            #  print "name:",each_pub.name
            #  papers=Paper.objects.filter(published_in__contains=each_pub.name).exclude(published_in=each_pub.name)
            #  for each_paper in papers:
                #  print each_paper.published_in.decode('utf8')
                #  each_paper.published_in=each_pub.name
                #  res=raw_input("Y/N:")
                #  if res!='n' and res!='N':
                    #  each_paper.save()


if __name__ == '__main__':
    match()

