def test_seg(lexical_data): res = [['结婚', '的', '和', '尚未', '结婚', '的', '都', '成', '了', '和尚'], ['我', '喜欢', '《', '瓦尔登', '湖', '》', '这', '本书', ',', '如果', '你', '也', '喜欢', ',', '欢迎', '联系', '我', 'xmlee97', '@', 'gmail', '.', 'com', '一起', '交流'], # NOQA ['<', 'h1', '>', '谷歌', '<', '/', 'h1', '>', '的', '网址', '是', 'https', ':', '/', '/', 'google', '.', 'com'], ['现在', '时间', '是', '2021年2月'], ['现任', '美国', '总统', '是', '拜登']] preds = [xmnlp.seg(data) for data in lexical_data] lexical_equal(preds, res)
def test_seg(postag_data): res = [['结婚', '的', '和', '尚未', '结婚', '的', '都', '成', '了', '和尚'], [ '工信处', '女干事', '每月', '经过', '下属', '科室', '都', '要', '亲口', '交代', '24', '口', '交换机', '等', '技术性', '器件', '的', '安装', '工作' ], ['他', '正在', '量', '和服', '尺寸']] preds = [xmnlp.seg(data) for data in postag_data] postag_equal(preds, res)
def test_seg(postag_data): res = [['结婚', '的', '和', '尚未', '结婚', '的', '都', '成', '了', '和尚'], [ '我', '喜欢', '《瓦尔登湖》', '这', '本书', ',', '如果', '你', '也', '喜欢', '欢迎', '联系', '我', '*****@*****.**' ], ['<h1>', '谷歌', '</h1>', '的', '网址', '是', 'https://google.com'], ['现在', '时间', '是', '2019年10月']] preds = [xmnlp.seg(data) for data in postag_data] postag_equal(preds, res)
import xmnlp doc = """小明 NLP 是一款开源的轻量级中文自然语言处理工具🔧,当前版本发布时间为2019年9月,改版本修复了一些 bug 也增加了一些特性,主要新增特性如下:分词/词性标注支持日期、email、url、html标签、书📖的识别。如果您有什么建议/疑问欢迎联系我 [email protected]""" xmnlp.seg(doc, hmm=True) pass
# sentiments = sentiment['sentiment'] # print(f"正向:{positive},负向:{negative},可信度:{confidence},情感:{sentiments}") # return sentiment # # def get_text(path): # with open(path, 'r') as f: # data = f.readlines() # s = [] # a = 0 # for key, i in enumerate(data): # if i[0].isdigit(): # text = ''.join(data[a:key]).replace('\n', '') # a = key # s.append(text) # return s # if __name__ == '__main__': # path = 'juben.txt' # text = get_text(path) # flag = 1 # for i in text: # get_emotion(i) # flag+=1 # if flag%5==0: # time.sleep(5) import xmnlp doc = """自然语言处理: 是人工智能和语言学领域的分支学科。 在这此领域中探讨如何处理及运用自然语言;自然语言认知则是指让电脑“懂”人类的语言。 自然语言生成系统把计算机数据转化为自然语言。自然语言理解系统把自然语言转化为计算机程序更易于处理的形式。""" print(xmnlp.seg(doc, hmm=True))
def search(keyword, page): db = SQL('datadb') page = int(page) # 根据页码返回指定记录 offset = int((page - 1) * num_per_page) # 时间 timeid = db.session.query(times).order_by(desc(times.id)).first().id # timeid = 60 # 关键词拆分 keywords = [] ch = re.findall('[\u4e00-\u9fa5]+', keyword) tp = '' for c in ch: tp = tp + c cnk = xm.seg(tp) enk = wordninja.split(keyword) keywords = enk[0:3] + cnk[0:3] words = keywords for i in range(0, len(keywords)): keywords[i] = '%' + str(keywords[i]) + '%' print(words) rule = or_(*[products.name.like(w) for w in keywords]) rs = db.session.query(evalresult, products).join( products, evalresult.productid == products.id).filter(rule).filter( evalresult.current_timeid == timeid, evalresult.comm_eval > -1).offset(offset).all() datalist = [] for result in rs: print(result[1].name) # 模糊匹配 筛选命中2个以上的 print(len(words)) if len(words) >= 3: i = 0 for key in words: check = re.search(key, result[1].name) if check: i += 1 print('+++') if i < 3: continue result = result[0] data = {} tmp = result.imgid.split('||') data[ 'imgid'] = 'https://www.buptdachuang2019.top/api/img?imgid=' + tmp[ 0] data['productid'] = result.productid data['platform'] = db.session.query(platforms).filter( platforms.id == result.platformid).one().platform data['name'] = db.session.query(products).filter( products.id == result.productid).one().name data['price'] = result.price data['num'] = result.num datalist.append(data) db.close() return datalist
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.""" import sys sys.path.append("..") if sys.version_info[0] == 2: reload(sys) sys.setdefaultencoding('utf8') import xmnlp xmnlp.set_userdict('./userdict.txt') doc = """自然语言处理: 是人工智能和语言学领域的分支学科。 在这此领域中探讨如何处理及运用自然语言;自然语言认知则是指让电脑“懂”人类的语言。 自然语言生成系统把计算机数据转化为自然语言。自然语言理解系统把自然语言转化为计算机程序更易于处理的形式。""" seg_list = xmnlp.seg(doc, hmm=True) print(' / '.join(seg_list))