class PinyinTests(unittest.TestCase): def setUp(self): import cpinyin cpinyin.install() from xpinyin import Pinyin self.p = Pinyin() def test_get_pinyin_with_default_splitter(self): self.assertEqual(self.p.get_pinyin(u'上海'), u'shang-hai') def test_get_pinyin_with_splitter(self): self.assertEqual(self.p.get_pinyin(u'上海', splitter=u''), u'shanghai') def test_get_pinyin_mixed_words(self): self.assertEqual(self.p.get_pinyin(u'Apple发布iOS7', splitter=u'-'), u'Apple-fa-bu-iOS7') def test_get_pinyin_with_tone_marks(self): self.assertEqual(self.p.get_pinyin(u'上海', tone_marks=u'marks'), u'sh\xe0ng-h\u01cei') def test_get_pinyin_with_tone_marks(self): self.assertEqual(self.p.get_pinyin(u'秋', tone_marks=u'marks'), u'qiū') def test_get_initial(self): self.assertEqual(self.p.get_initial(u'你'), u'N') def test_get_initials(self): self.assertEqual(self.p.get_initials(u'你好'), u'N-H') def test_get_initials_with_splitter(self): self.assertEqual(self.p.get_initials(u'你好', u' '), u'N H') self.assertEqual(self.p.get_initials(u'你好', u''), u'NH')
def ab(self): try: # 拼音 p = Pinyin() if self.args['EditType'] == 'add': sql = "INSERT INTO Carder(Carder_MarkName, Carder_Mobile, " \ "Carder_Address, Carder_User, Carder_BankUserName, " \ "Carder_BankNumber, Carder_BankLive, Carder_Type, " \ "Carder_CarNumber, Carder_BakInfo, Carder_CarType, Carder_PY) " \ "values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')" \ .format(safe(self.args.get('Carder_MarkName', '')), safe(self.args.get('Carder_Mobile', '')), safe(self.args.get('Carder_Address', '')), safe(self.args.get('Carder_User', '')), safe(self.args.get('Carder_BankUserName', '')), safe(self.args.get('Carder_BankNumber', '')), safe(self.args.get('Carder_BankLive', '')), safe(self.args.get('Carder_Type', '')), safe(self.args.get('Carder_CarNumber', '')), safe(self.args.get('Carder_BakInfo', '')), safe(self.args.get('Carder_CarType', '')), p.get_initials(self.args.get('Carder_MarkName', ''), '')) conn.eDB(sql) res_id = conn.sDB("select cast(IDENT_CURRENT('Carder') as nvarchar(20)) as ID")[0] self.args['ID'] = int(res_id['ID']) else: sql = "UPDATE Carder Set Carder_MarkName = '{0}', Carder_Mobile = '{1}', " \ "Carder_Address = '{2}', Carder_User = '******', Carder_BankUserName = '******', " \ "Carder_BankNumber = '{5}', Carder_BankLive = '{6}', Carder_Type = '{7}', " \ "Carder_CarNumber = '{8}', Carder_BakInfo = '{9}', Carder_CarType = '{10}', " \ "Carder_PY = '{11}' where ID='{12}'" \ .format(safe(self.args.get('Carder_MarkName', '')), safe(self.args.get('Carder_Mobile', '')), safe(self.args.get('Carder_Address', '')), safe(self.args.get('Carder_User', '')), safe(self.args.get('Carder_BankUserName', '')), safe(self.args.get('Carder_BankNumber', '')), safe(self.args.get('Carder_BankLive', '')), safe(self.args.get('Carder_Type', '')), safe(self.args.get('Carder_CarNumber', '')), safe(self.args.get('Carder_BakInfo', '')), safe(self.args.get('Carder_CarType', '')), p.get_initials(self.args.get('Carder_MarkName', ''), ''), safe(self.args.get('ID', ''))) conn.eDB(sql) return str(json.dumps({"err": False, "data": self.args})) except Exception as err: return str(json.dumps({"err": str(err)}))
def addCatagory(name, parent_name, meta_title, meta_keyword, meta_description, hot_tag, hot_brand, author, cover_image): ''' 增加一个目录,一级目录的parent_id=0 ''' db = DBAccess() db.dbName = "zixun" p = Pinyin() if not name: raise MyDefineError("名称不能为空") names_all = getAllObjectUrlsOrNames('catagory', 'name') if name in names_all: raise MyDefineError("栏目已存在") if parent_name: parent_id = getObjectIdWithName('catagory', parent_name) if not parent_id: raise MyDefineError("父栏目不存在") else: parent_id = 0 url = str(p.get_initials(name.decode('utf-8'), '')).lower() urls_all = getAllObjectUrlsOrNames('catagory', 'url') while url in urls_all: url += str(random.randint(0, 30)) sql = "insert into catagory (name, parent_id,url, meta_title,meta_keyword,meta_description,cover_image) values ('%s','%s','%s','%s','%s','%s','%s')" \ %(name,parent_id,url,meta_title,meta_keyword,meta_description,cover_image) catagory_id = db.execNonQuery(sql) if hot_tag: updateHotTag(catagory_id, hot_tag) if hot_brand: updateHotBrand(catagory_id, hot_brand) recordFlow(author, "add", "catagory", sql) return
def parse_village(response): print('==> %s' % response.url) pid = response.meta['pid'] p = Pinyin() try: for tr in response.xpath('//tr[@class="villagetr"]'): item = RegionItem() item['adCode'] = tr.xpath('td[1]/text()')[0].extract() item['name'] = tr.xpath('td[3]/text()')[0].extract() if '居民委员会' in item['name']: item['name'] = item['name'].replace('居民委员会', '') if '村民委员会' in item['name']: item['name'] = item['name'].replace('村民委员会', '村') if '村委会委会' in item['name']: item['name'] = item['name'].replace('村委会委会', '村') if '居委会' in item['name']: item['name'] = item['name'].replace('居委会', '') if '村村村委会' in item['name']: item['name'] = item['name'].replace('村村村委会', '村') if '村村委会' in item['name']: item['name'] = item['name'].replace('村村委会', '村') if '村村' in item['name']: item['name'] = item['name'].replace('村村', '村') item['py'] = p.get_pinyin(item['name'], ' ') item['initial'] = p.get_initials(item['name'], '')[0:1] item['level'] = 5 item['parent'] = pid yield item except IndexError: pass
def parse_town(self, response): print('==> %s' % response.url) pid = response.meta['pid'] p = Pinyin() for tr in response.xpath('//tr[@class="towntr"]'): item = RegionItem() item['adCode'] = tr.xpath('td[1]/a/text()')[0].extract()[0:9] item['name'] = tr.xpath('td[2]/a/text()')[0].extract() if '办事处' in item['name']: item['name'] = item['name'].replace('办事处', '') item['py'] = p.get_pinyin(item['name'], ' ') item['initial'] = p.get_initials(item['name'], '')[0:1] item['level'] = 4 item['parent'] = pid yield item url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/%s/%s/%s/%s.html' % ( pid[0:2], pid[2:4], pid[4:6], item['adCode']) request = scrapy.Request(url, callback=self.parse_village, dont_filter=True) request.meta['pid'] = item['adCode'] yield request
def handle(self, *args, **options): hellspawns = Hellspawn.objects.all() p = Pinyin() for hell in hellspawns: hell.name_pinyin = p.get_pinyin(hell.name, '') hell.name_abbr = p.get_initials(hell.name, '') hell.save()
def parse_county(self, response): print('==> %s' % response.url) pid = response.meta['pid'] p = Pinyin() for tr in response.xpath('//tr[@class="countytr"]'): item = RegionItem() item['level'] = 3 item['parent'] = pid try: item['adCode'] = tr.xpath('td[1]/a/text()')[0].extract()[0:6] item['name'] = tr.xpath('td[2]/a/text()')[0].extract() url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/%s/%s/%s.html' % ( pid[0:2], pid[2:4], item['adCode']) request = scrapy.Request(url, callback=self.parse_town, dont_filter=True) request.meta['pid'] = item['adCode'] yield request except IndexError: item['adCode'] = tr.xpath('td[1]/text()')[0].extract()[0:6] item['name'] = tr.xpath('td[2]/text()')[0].extract() item['py'] = p.get_pinyin(item['name'], ' ') item['initial'] = p.get_initials(item['name'], '')[0:1] yield item
def addBrand(name, description, company_name, company_website, brand_classify, company_address, meta_title, meta_keyword, meta_description, cover_image, author): ''' 增加品牌 ''' db = DBAccess() db.dbName = "zixun" p = Pinyin() names_all = getAllObjectUrlsOrNames("brand", 'name') if name in names_all: raise MyDefineError("品牌已存在") url = str(p.get_initials(name.decode('utf-8'), '')).lower() urls_all = getAllObjectUrlsOrNames("brand", 'url') while url in urls_all: url += str(random.randint(0, 30)) create_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sql = "insert into brand (name,description,url,company_name,company_website,brand_classify,company_address,meta_title,meta_keyword,meta_description,cover_image,author,create_time,update_time) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"\ %(name,description,url,company_name,company_website,brand_classify,company_address,meta_title,meta_keyword,meta_description,cover_image,author,create_time,create_time) brand_id = db.execNonQuery(sql) recordFlow(author, "add", "brand", sql) return brand_id
def addTag(parent_name, name, meta_title, meta_keyword, meta_description, author): ''' 增加标签 ''' db = DBAccess() db.dbName = "zixun" r = getRedisObj() p = Pinyin() if parent_name: parent_id = getObjectIdWithName('tag', parent_name) if not parent_id: raise MyDefineError("一级标签不存在") else: parent_id = 0 names_all = getAllObjectUrlsOrNames("tag", 'name') if name in names_all: raise MyDefineError("标签已存在") url = str(p.get_initials(name.decode('utf-8'), '')).lower() urls_all = getAllObjectUrlsOrNames('tag', 'url') while url in urls_all: url += str(random.randint(0, 30)) update_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sql = "insert into tag (name,url,update_time,parent_id,meta_title,meta_keyword,meta_description) values ('%s','%s','%s',%s,'%s','%s','%s')"\ %(name,url,update_time,parent_id,meta_title,meta_keyword,meta_description) tag_id = db.execNonQuery(sql) recordFlow(author, "add", "tag", sql) return tag_id
def read_stock_code(dirs): p=Pinyin() for mydir in dirs: with open(mydir) as f: data=f.readlines() ix=0 while ix<len(data): s=data[ix].strip('\n').split('=') if not s[0]: ix+=1 continue if len(s)<2: ix+=2 continue code=s[0] cnames=s[1].split('|') if len(cnames)>1: chineseName=cnames[0].replace(' ','') initial='' for i in p.get_initials(cnames[0].replace(' ','')).replace('-',''): cod=ord(i) if 65312<cod<65339: i=chr(cod-65248) initial=initial+i spareCode=cnames[1][:-2] else: chineseName=cnames[0].replace(' ','') initial='' for i in p.get_initials(cnames[0].replace(' ','')).replace('-',''): cod=ord(i) if 65312<cod<65339: i=chr(cod-65248) initial=initial+i spareCode=None bazaar=None if mydir[-9:-7]=='16': bazaar='sh' elif mydir[-9:-7]=='32': bazaar='sz' elif '176' in mydir: bazaar='hk' yield [code,chineseName,initial,spareCode,bazaar] ix+=1 print(mydir)
def getShortPin(): print u'[-]姓名转缩写' pinyin = Pinyin() for name in cnnames: py = pinyin.get_initials(name.strip(), '').lower() print '{}---{}'.format(name.strip().decode('utf-8').encode('gbk'), py) username.add(py) print u'[-]姓名转姓名全拼' for name in cnnames: xing = name.strip()[0] ming = name.strip()[1:] pyxing = pinyin.get_pinyin(xing, '') pyming = pinyin.get_initials(ming, '').lower() py = '{}{}'.format(pyxing, pyming) print '{}---{}'.format(name.strip().decode('utf-8').encode('gbk'), py) username.add(py) info['ennames'] = list(username)
def save(self, without_valid=False, *args, **kwargs): p = Pinyin() full_pinyin = p.get_pinyin(smart_str(self.title), '') first_pinyin = p.get_initials(smart_str(self.title), '').lower() self.search_word = " ".join([full_pinyin, first_pinyin]) update_logger.info('video path: ' + str(self.video)) if self.description is None or self.description == "": self.description = default_description(self) if self.local_video != '' and self.local_video is not None: basename = Path(self.local_video).relative_to( Path(settings.LOCAL_MEDIA_ROOT)) self.video.name = str(Path(settings.LOCAL_MEDIA_URL) / basename) update_logger.debug("save local_video to filefield done") if without_valid: ret = super(Vod, self).save(*args, **kwargs) return ret super(Vod, self).save(*args, **kwargs) if os.path.splitext(str(self.video))[1] not in ['.mp4', '.m3u8']: p = threading.Thread(target=ff, args=(self, )) p.start() try: if self.video != None and self.video != '': relative_path = Path(self.video.name).relative_to( settings.MEDIA_URL) # Djan%20go.mp4 rel_name = uri_to_iri(relative_path) # Djan go.mp4 # Make sure the self.video.name is not in the LOCAL_FOLDER if not self.video.name.startswith(settings.LOCAL_FOLDER_NAME) and \ not self.video.name.startswith(settings.RECORD_MEDIA_FOLDER): self.video.name = str(rel_name) update_logger.info('save path: ' + self.save_path) update_logger.info('video name: ' + str(self.video.name)) update_logger.info('size: ' + self.video.file.size) self.file_size = humanfriendly.format_size( self.video.file.size) # duration = VideoFileClip(self.video.path).duration # self.duration = time_formate(duration) else: print("video file is None") except: pass try: if self.image: self.image.name = str( uri_to_iri( Path(self.image.name).relative_to(settings.MEDIA_URL))) except: pass return super(Vod, self).save(*args, **kwargs)
def SH(wordlists, char=""): #获取拼音首拼大写 p = Pinyin() pwd = os.getcwd() + "\\dic" if (os.path.exists(pwd) == False): os.makedirs(pwd) else: with open("./dic/SH.txt", "w", encoding="utf-8") as f: for word in wordlist: str0 = p.get_initials(word, char) f.write(str0) print("拼音首拼大写--->>>" + str0)
def parse_bus_list(self, req, result): soup = BeautifulSoup(result, "html.parser") for line in soup.find_all("script")[2]: if "var data = " in line: bus_list = [str(eval(x)) for x in line.split("[")[1].split("];")[0].split(",")] else: bus_list = [] p = Pinyin() bus_dict = {p.get_initials(x, ""): x for x in bus_list} with open("buseslist.txt", "w", encoding="utf-8") as f: f.write(str(bus_dict))
def __init__(self): self.city = input("请输入城市名:\n") p = Pinyin() city_name = p.get_initials(self.city, '').lower() self.url = 'http://{0}.ganji.com/v/zhaopinxinxi/p1/'.format(city_name) self.save_path = r'G:\data\ganji.csv' file_dir = os.path.split(self.save_path)[0] if not os.path.isdir(file_dir): os.makedirs(file_dir) if not os.path.exists(self.save_path): os.system(r'echo > %s' % self.save_path)
def welcome(request): if request.user.is_authenticated(): return redirect('/') appname = u"延伸" pagename ='welcome' if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] action = request.POST['action'] if username == '' or password =='' or action == '': message = '抱歉,服务器开小差了,注册失败。' return locals() if action == 'login': user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) return redirect('/') else: message = '账户已被注销。' return locals() else: message = 'Email 或密码错误。' return locals() elif action == 'reg': if Profile.objects.filter(email=username).exists(): message = '您已经注册过了。' return locals() else: try: user = Profile.objects.create_user(email=username, password=password) user.first_name = request.POST['firstname'] user.last_name = request.POST['lastname'] p = Pinyin() user.pinyin = p.get_pinyin(user.last_name + user.first_name, ' ') for char in user.last_name + user.first_name: user.pinyin += p.get_initials(char) user.contact_info = json.JSONEncoder().encode({"next_id":2, "data":[{"info_id":1, "type":"Email", "key": u"电子邮箱", "value": user.email}]}) user.save() category = Category.objects.create(name=u'未分组', owner=user, privilege='{"1":false}') category.save() except ValidationError: message = '请输入正确的 Email 地址。' return locals() if user: user = authenticate(username=username, password=password) login(request, user) return redirect('/me/?first=1') else: message = '抱歉,服务器开小差了,注册失败。' return locals() else: return locals()
def get_letter(name): """ get first letter :author: lishanZheng :date: 2020/01/08 """ pinyin = Pinyin() if name == '': return 'Z' letter = pinyin.get_initials(name, u'')[0] return letter
def get_pinyin(): p = Pinyin() for results in query_by_pagination(session, GoodsInventory): for itm in results: itm.py = p.get_initials(itm.name, '').replace(' ', '') try: session.commit() except Exception as e: logging.exception( 'ERROR in commit session site {0} reason {1}'.format( 'null', e)) session.rollback()
def draw_bar(): data = '''北 京 7355291 天 津 3963604 河 北 20813492 山 西 10654162 内 蒙 古 8470472 辽 宁 15334912 吉 林 9162183 黑 龙 江 13192935 上 海 8893483 江 苏 25635291 浙 江 20060115 安 徽 19322432 福 建 11971873 江 西 11847841 山 东 30794664 河 南 26404973 湖 北 17253385 湖 南 19029894 广 东 32222752 广 西 13467663 海 南 2451819 重 庆 10272559 四 川 26383458 贵 州 10745630 云 南 12695396 西 藏 689521 陕 西 11084516 甘 肃 7113833 青 海 1586635 宁 夏 1945064 新 疆 6902850''' data = data.split('\n') d = {} pinyin = Pinyin() for i in data: k = pinyin.get_initials(i[:6].strip().replace(' ',''),'') v = int(i[6:].strip()) d[k] = v pt.figure(figsize=(9,6)) ind = np.arange(len(d)) pt.bar(ind,d.values(),alpha=0.9,width=0.5,facecolor = 'lightskyblue',edgecolor = 'white',label = '人口',lw=1) pt.xticks(ind,d.keys()) pt.plot(ind,d.values(),color='red',label='plot',lw=1,alpha=0.6) pt.title('china population') pt.legend(loc = "upper left") pt.savefig(f'{random.randint(1,100)}.png') pt.show()
def get_citypair(filename): pinyin = Pinyin() f = open(filename, encoding='utf-8') citys_list = [i.strip().lstrip() for i in f.readlines()] f.close() citys_pinyin = [ pinyin.get_initials( i, splitter='', ).lower() for i in citys_list ] return dict( zip(citys_pinyin, [pinyin.get_pinyin(i, splitter='') for i in citys_list])).items()
def query_fulltext_code(code): pinyin = Pinyin() initials = [] for letter in pinyin.get_initials(code, splitter=' ').lower().split(' '): if letter.isalpha(): initials.append(letter * 4) logging.debug(initials) analysed_code = pinyin.get_pinyin(code, splitter=u' ') + ' ' + ' '.join(initials) logging.debug(analysed_code) clause = "MATCH(`codepinyin`, `codepinyininitials`) AGAINST (%s)" query = yield dbutil.do(Event.select(SQL('*, ' + clause + ' AS similarity', analysed_code)).where( SQL(clause, analysed_code)).limit(4).dicts) events = [event for event in query] logging.debug(events) return events
def insert_shortname(self): # 一次性产生全部用户的拼音首字母大写 conn = self.init_db() cur = conn.cursor() row = cur.execute("select * from 35txl") # 显示这个数据表有多少行 x = cur.fetchall() p = Pinyin() sqli = "update 35txl set shortname=%s where id=%s" # 35txl为表名 i = 0 while (i < row): shortname = p.get_initials(x[i][1], u'') i = i + 1 cur.execute(sqli, (shortname, i)) cur.close() conn.commit() conn.close()
def save(self, without_valid=False, *args, **kwargs): print("------- save vod -------") p = Pinyin() full_pinyin = p.get_pinyin(smart_str(self.title), '') first_pinyin = p.get_initials(smart_str(self.title), '').lower() self.search_word = " ".join([full_pinyin, first_pinyin]) print("video path:", self.video) if self.description is None or self.description == "": self.description = default_description(self) if self.local_video != '' and self.local_video is not None: basename = Path(self.local_video).relative_to(Path(settings.LOCAL_MEDIA_ROOT)) self.video.name = str(Path(settings.LOCAL_MEDIA_URL) / basename) print("save local_video to filefield done") if without_valid: ret = super(Vod, self).save(*args, **kwargs) return ret super(Vod, self).save(*args, **kwargs) try: if self.video != None and self.video != '': basename = Path(self.video.name).name # Djan%20go.mp4 rel_name = uri_to_iri(basename) # Djan go.mp4 # Make sure the self.video.name is not in the LOCAL_FOLDER if not self.video.name.startswith(settings.LOCAL_FOLDER_NAME) and \ not self.video.name.startswith(settings.RECORD_MEDIA_FOLDER): self.video.name = str(Path(self.save_path) / rel_name) print("save_path:", self.save_path) print(self.video.name) print('size:', self.video.file.size) self.file_size = humanfriendly.format_size(self.video.file.size) # duration = VideoFileClip(self.video.path).duration # self.duration = time_formate(duration) # print(self.duration) else: print("video file is None") except: pass try: if self.image: # self.image.name = os.path.join(self.save_path, os.path.basename(uri_to_iri(self.image.name))) self.image.name = Path(self.save_path) / Path(uri_to_iri(self.image.name)).name except: pass return super(Vod, self).save(*args, **kwargs)
def nameSpy(fo): p = Pinyin() name_dict = dict() name_dict_2 = dict() '''建两个字典, 第一个保存例如:zsw:张三五,张三无;zs:张三,张伞 第二个保存:sw:张三无,司三五 也就是两个字以上的名字的字的后两个字的简拼 ''' for line in fo: # print(line.strip(),end="") s = p.get_initials(line.strip(), u'').lower() # print(s) if s.__len__() > 2: s_2 = s[-2:] name_dict_2.setdefault(s_2, []).append(line.strip()) # 获取名字的简拼,"王五六"->"wwl" name_dict.setdefault(s, []).append(line.strip()) # 更新字典,这里字典的值为列表 return name_dict, name_dict_2
def new_topic(request): """创建新的主题""" pinyin = Pinyin() if request.method != 'POST': #当操作为非提交数据时,则创建一个新的表单 form = TopicForm() else: #当操作为提交数据时,则对数据进行处理 form = TopicForm(request.POST) if form.is_valid(): #获取post数据 new_topic = form.save(commit=False) new_topic.name = pinyin.get_initials(new_topic.text, splitter='') new_topic.save() return HttpResponseRedirect(reverse('learning_logs:topics')) context = {'form':form} return render(request, 'learning_logs/new_topic.html', context)
def post(self): try: data = json.loads(self.request.body.decode()) logging.debug(data) event = dict_to_model(Event, data) event.createtime = datetime.datetime.now() pinyin = Pinyin() event.codepinyin = pinyin.get_pinyin(event.code, splitter=' ') initials = [] for letter in pinyin.get_initials(event.code, splitter=' ').lower().split(' '): if letter.isalpha(): initials.append(letter * 4) event.codepinyininitials = ' '.join(initials) yield dbutil.do(event.save) self.write(Response(status=1, msg='恭喜你,活动发布成功!', result={}).json()) ioloop.IOLoop.current().spawn_callback(event_service.create_qrcode, event) except Exception as e: self.write(Response(msg='sorry,亲,活动发布失败').json()) logging.exception('CreateEventHandler error: {0}'.format(str(e)))
def post(self): try: data = json.loads(self.request.body.decode()) logging.debug(data) event = dict_to_model(Event, data) event.createtime = datetime.datetime.now() pinyin = Pinyin() event.codepinyin = pinyin.get_pinyin(event.code, splitter=' ') initials = [] for letter in pinyin.get_initials(event.code, splitter=' ').lower().split(' '): if letter.isalpha(): initials.append(letter * 4) event.codepinyininitials = ' '.join(initials) yield dbutil.do(event.save) self.write(Response( status=1, msg='恭喜你,活动发布成功!', result={} ).json()) ioloop.IOLoop.current().spawn_callback(event_service.create_qrcode, event) except Exception as e: self.write(Response(msg='sorry,亲,活动发布失败').json()) logging.exception('CreateEventHandler error: {0}'.format(str(e)))
def new_entry(request, topic_name): """在特定的主题中创建新的科目""" pinyin = Pinyin() topic = Topic.objects.get(name=topic_name) if request.method != 'POST': #当操作为非提交数据时,则创建一个新的表单 form = EntryForm() else: #当操作为提交数据时,则对数据进行处理 form = EntryForm(data=request.POST) if form.is_valid(): new_entry = form.save(commit=False) new_entry.topic = topic new_entry.name = pinyin.get_initials(new_entry.text, splitter='') new_entry.save() return HttpResponseRedirect(reverse('learning_logs:entries', args=[topic.name])) context = {'topic':topic, 'form':form} return render(request, 'learning_logs/new_entry.html', context)
def get_site_from_csv(): p = Pinyin() now = get_now_time_with_timezone() py_list = [] with codecs.open('ip.csv', encoding='utf-8') as f1: for line in f1: name, bos, fuel = line.split(',') py = p.get_initials(name, '').replace(' ', '') if py in py_list: py = '{0}{1}'.format(py, random.randint(0, 100)) py_list.append(py) site = Site() site.name = name site.fuel_server = fuel.strip() site.bos_server = bos.strip() site.slug = py site.create_time = now site.modify_time = now site.original_create_time = now site.check = False site.lock = 1000 session.add(site) session.commit()
def run(self): try: p = Pinyin() for stock in self.stocks: no = stock.get('no') request = urllib2.Request('http://hq.sinajs.cn/list=' + self.get_stock_with_prefix(no)) response = urllib2.urlopen(request) result = response.read().decode('gbk') text = re.sub('var.+=[^"]*"', '', result) infos = text.split(',') stockName = p.get_initials(self.strQ2B(infos[0]), u'') yesterdayPrice = float(infos[2]) curPrice = float(infos[3]) percent = (curPrice / yesterdayPrice - 1) * 100, percentStr = str('%0.2f'%percent) + "%" self.priceList.append({'no':no, 'name':stockName, 'percent': percentStr}) return except (urllib2.HTTPError) as (e): err = '%s: HTTP error %s contacting API' % (__name__, str(e.code)) except (urllib2.URLError) as (e): err = '%s: URL error %s contacting API' % (__name__, str(e.reason)) sublime.error_message(err)
def new_blog(request, topic_name, entry_name): """在特定主题中的某个科目下创建新的条目""" pinyin = Pinyin() topic = Topic.objects.get(name=topic_name) entry = Entry.objects.get(name=entry_name) if request.method != 'POST': #当操作为非提交数据时,则创建一个新的表单 form = BlogForm() else: #当操作为提交数据时,则对数据进行处理 form = BlogForm(data=request.POST) if form.is_valid(): new_blog = form.save(commit=False) new_blog.entry = entry new_blog.owner = request.user new_blog.name = pinyin.get_initials(new_blog.title, splitter='') new_blog.save() return HttpResponseRedirect(reverse('learning_logs:blogs', args=[topic_name, entry_name])) context = {'topic':topic,'entry':entry, 'form':form} return render(request, 'learning_logs/new_blog.html', context)
def parse(self, response): print('==> %s' % response.url) p = Pinyin() try: for tr in response.xpath('//tr[@class="provincetr"]'): for td in tr.xpath('td'): item = RegionItem() item['adCode'] = td.xpath('a/@href')[0].extract()[0:-5] item['name'] = td.xpath('a/text()')[0].extract() item['py'] = p.get_pinyin(item['name'], ' ') item['initial'] = p.get_initials(item['name'], '')[0:1] item['level'] = 1 item['parent'] = '' yield item url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/%s.html' % item[ 'adCode'] request = scrapy.Request(url, callback=self.parse_city, dont_filter=True) request.meta['pid'] = item['adCode'] yield request except IndexError: pass
def lev(): arith = arithmetic() pinyindisdic = {} initialdisdic = {} disdic = {} sym_pinyin_dic = {} sym_initial_dic = {} drug_pinyin_dic = {} drug_initial_dic = {} with open("sym_pinyin.txt", "r") as f: data = f.readlines() for i in data: sym_pinyin_dic[i.split(':')[1].split()[0]] = i.split(':')[0] with open("sym_initial.txt", "r") as f: data = f.readlines() for i in data: sym_initial_dic[i.split(':')[1].split()[0]] = i.split(':')[0] with open("drug_pinyin.txt", "r") as f: data = f.readlines() for i in data: drug_pinyin_dic[i.split(':')[1].split()[0]] = i.split(':')[0] with open("drug_initial.txt", "r") as f: data = f.readlines() for i in data: drug_initial_dic[i.split(':')[1].split()[0]] = i.split(':')[0] with open("trie.txt", "r") as f: data = f.readlines() data = [codeutil(i.split()[0]) for i in data] p = Pinyin() input_word = data[0] pinyin = p.get_pinyin(input_word, '') initial = p.get_initials(input_word, '') #print input_word,pinyin,initial for i in data[1:]: disdic[i] = arith.levenshtein(data[0], i) f = zip(disdic.values(), disdic.keys()) f = sorted(f) minf = f[0][0] #print sym_pinyin_dic.keys()[:5] for i in f: if pinyin in sym_pinyin_dic and i[0] == minf: #print pinyin if pinyin in drug_pinyin_dic and i[0] == minf: #print drug_pinyin_dic[pinyin], pinyin, i[0] pass #print sym_pinyin_dic[pinyin],pinyin,i[0] return sym_pinyin_dic[pinyin] elif initial in sym_initial_dic and i[0] == minf: if initial in drug_initial_dic and i[0] == minf: #print drug_initial_dic[initial], initial, i[0] pass print sym_initial_dic[initial], initial, i[0] return sym_initial_dic[initial] else: #print i[0],i[1] return i[1]
#!/usr/bin/env python #-*- coding:UTF-8 -*- import sys; from xpinyin import Pinyin p = Pinyin() # default splitter is `-` print p.get_pinyin(u"上海") # show tone marks print p.get_pinyin(u"上海", show_tone_marks=True) # remove splitter print p.get_pinyin(u"上海", '') # set splitter as whitespace print p.get_pinyin(u"上海", ' ') print p.get_initial(u"上") print p.get_initials(u"上海") print p.get_initials(u"上海", u'') print p.get_initials(u"上海", u' ') #如果方法中传入变量,那么直接加前缀是不可以了。而是要将变量转为utf-8编码: wordvalue = '中国' wordvalue= unicode(wordvalue,'utf-8') s = p.get_initials(wordvalue, u'').lower() print s