import pathlib
from typing import Union
import attr
import paralleldots
import requests
from aylienapiclient import textapi
from srec_nlp.common import _ensure_utf, Query, Score, _parallel
class NLP:
@_ensure_utf
def sentiment(self, text) -> Query:
"""
Sample class.
:param text: Union[IOBase, str, Path, bytes] a file, pathtofile, bytes or string with the text.
Function guarantees the value will be converted to text
:return: Query
"""
raise NotImplemented()
@attr.s
class ParallelDots:
"""
with ParallelDots(api_key) as nlp:
nlp.sentiment(text)
nlp.sentiment(bytes)
nlp.sentiment(open(path/to/text.txt,'r'))
nlp.sentiment(pathlib.Path('path/to/text.txt'))
# ParallelDots is still usable at this point.
# So reusing it is still valid
nlp.sentiment(text)
nlp.sentiment(bytes)
nlp.sentiment(>>> from math import sqrt
>>> from joblib import Parallel, delayed
>>> Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]open(path/to/text.txt,'r'))
nlp.sentiment(pathlib.Path('path/to/text.txt'))
"""
api_key: str = attr.ib()
@api_key.validator
def setkey(self, _, value):
paralleldots.set_api_key(value)
def __enter__(self):
return self
@_parallel
@_ensure_utf
def sentiment(self, text) -> Query:
"""
Produces a sentiment score for query text using ParallelDots API
:param text: Union[IOBase, str, Path, bytes] a file, pathtofile, bytes or string with the text.
Function guarantees the value will be converted to text
:return: Query
"""
response = paralleldots.sentiment(text)
return Query(text, Score.fromtriplet(**response["sentiment"]))
def __exit__(self, *exc):
pass
class TextProcessing:
"""
with TextProcessing() as nlp:
nlp.sentiment(text)
nlp.sentiment(bytes)
nlp.sentiment(open(path/to/text.txt,'r'))
nlp.sentiment(pathlib.Path('path/to/text.txt'))
# TextProcessing is still usable at this point.
# So reusing it is still valid
nlp.sentiment(text)
nlp.sentiment(bytes)
nlp.sentiment(open(path/to/text.txt,'r'))
nlp.sentiment(pathlib.Path('path/to/text.txt'))
"""
_url = "http://text-processing.com/api/sentiment/"
def __enter__(self):
return self
@_parallel
@_ensure_utf
def sentiment(self, text) -> Query:
"""
Produces a sentiment score for query text using TextProcessing API
:param text: Union[IOBase, str, Path, bytes] a file, pathtofile, bytes or string with the text.
Function guarantees the value will be converted to text
:return: Query
"""
data = dict(text=text)
result = requests.post(self._url, data=data).json()
result = result["probability"]
result['positive'] = result['pos']
result['negative'] = result['neg']
del result['pos']
del result['neg']
q = Query(
content=text, score=Score.fromtriplet(**result)
)
return q
def __exit__(self, *exc):
pass
try:
import fastText
class FastText:
"""
model: str/pathlib.Path the path to the model.
# Usage example:
with FastText(path/to/model.bin) as nlp:
nlp.sentiment(text)
nlp.sentiment(bytes)
nlp.sentiment(open(path/to/text.txt,'r'))
nlp.sentiment(pathlib.Path('path/to/text.txt'))
# at this point, nlp.sentiment WILL RAISE AN ERROR because the model has been
# dealloc'd, to avoid this, use the following:
nlp = FastText(path/to/model.bin)
nlp.sentiment(text)
nlp.sentiment(bytes)
nlp.sentiment(open(path/to/text.txt,'r'))
nlp.sentiment(pathlib.Path('path/to/text.txt'))
"""
def __init__(self, path: Union[str, pathlib.Path]):
self.model = FastText._load_model(path)
def __enter__(self):
return self
def __exit__(self, *vargs, **kwargs):
del self.model
@_ensure_utf
def sentiment(self, text) -> Query:
"""
Produces a sentiment score for query text using FastText API
Note that FastText needs to be installed for this to work.
:param text: Union[IOBase, str, Path, bytes] a file, pathtofile, bytes or string with the text.
Function guarantees the value will be converted to text
:return: Query
"""
labels, scores = self.model.predict(text, 1)
label = labels[0]
score = scores[0]
if label == "__label__1":
score = 1 - score
score = Score.fromsingle(score)
return Query(text, score)
@staticmethod
def _load_model(path: Union[str, pathlib.Path]):
p = pathlib.Path(path).expanduser()
if not p.is_file():
raise ValueError(f"{p} is not a file.")
try:
return fastText.load_model(str(p))
except Exception as e:
print(e)
raise ValueError(f"{p} is not a valid FastText model")
except:
pass
class Aylien:
def __init__(self, appid, appkey):
"""
:param appid: string Aylien application id
:param appkey: string Aylien application key
with Aylien(id, key) as nlp:
nlp.sentiment(text)
nlp.sentiment(bytes)
nlp.sentiment(open(path/to/text.txt,'r'))
nlp.sentiment(pathlib.Path('path/to/text.txt'))
# Aylien is still usable at this point.
# So reusing it is still valid
nlp.sentiment(text)
nlp.sentiment(bytes)
nlp.sentiment(open(path/to/text.txt,'r'))
nlp.sentiment(pathlib.Path('path/to/text.txt'))
"""
self.client = textapi.Client(applicationId=appid, applicationKey=appkey)
def __enter__(self):
return self
@_parallel
@_ensure_utf
def sentiment(self, text: str) -> Query:
"""
Produces a sentiment score for query text using Aylien API
:param text: Union[IOBase, str, Path, bytes] a file, pathtofile, bytes or string with the text.
Function guarantees the value will be converted to text
:return: Query
"""
sentiment = self.client.Sentiment(dict(text=text))
confidence = sentiment["polarity_confidence"]
klass = sentiment["polarity"]
remaining = (1 - confidence)
vals = dict(positive=remaining, negative=remaining)
vals[klass] = confidence
return Query(content=text, score=Score(**vals))
def __exit__(self, *exc):
pass
Python库 | srec_nlp-0.2.1.tar.gz
版权申诉
162 浏览量
2022-04-15
17:16:01
上传
评论
收藏 4KB GZ 举报
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
最新资源
- 基于Python+Tensorflow实现声纹识别+源代码+文档说明.zip
- java-leetcode题解之第112题路径总和.zip
- java-leetcode题解之第111题二叉树的最小深度.zip
- java-leetcode题解之第110题平衡二叉树.zip
- java-leetcode题解之第109题有序链表转换二叉搜索树.zip
- java-leetcode题解之第108题将有序数组转换为二叉搜索树.zip
- java-leetcode题解之第107题二叉树的层序遍历II.zip
- java-leetcode题解之第102题二叉树的层序遍历.zip
- java-leetcode题解之第103题二叉树的锯齿形层序遍历.zip
- java-leetcode题解之第104题二叉树的最大深度.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈