项目作者: fdelgados

项目描述 :
Tools for cleaning and preprocessing text
高级语言: Python
项目地址: git://github.com/fdelgados/Textools.git
创建时间: 2019-12-21T11:39:31Z
项目社区:https://github.com/fdelgados/Textools

开源协议:MIT License

下载


Text Clean and Preprocess Tools (texcptulz)

made-with-python
GitHub release (latest by date)

textcptulz is a text preprocessing framework to transform raw ingested text into a form that is ready for computation and modeling.

Table of Contents

Installations

Dependencies

textcptulz requires:

  • Python (>=3.5)
  • NumPy
  • scikit-learn
  • NLTK
  • spaCy
  • spacy-langdetect
  • gensim

Installation

You can install texcptulz using pip

  1. pip install texcptulz

That easy.

Project Motivation

texcptulz aims to be a wrapper for all that processes involved in the wrangling part of an ETL pipeline for text analysis.
In addition, it includes other useful tools such as the detection of the language of a document. These are some of the
supported languages:

  • English
  • Spanish
  • French
  • German
  • Italian

Modules

The modules included in texcptulz are:

normalizer

The normalizer module includes TextNormalizer class that can perform the text normalization process, taking a text
as input and returning a list of lemmatized tokens.

Special characters, HTML tags, urls, etc. will be removed, using the clean_text function, then, the text will be
converted to lowercase and will be splitted into tokens and lemmatized.

clusterer

The clusterer module has only one class KMeansClusters that performs k-means clustering. The distance measure used
is cosine distance. The k parameter is the number of clusters, the default value is 7.

similarity

The similarity module has only one class, Similarity that creates a mxm similarity matrix for a group of documents.
In this matrix, columns and rows represents the index of each document the elements of the matrix are similarity between
documents.

The range of values will be between 0 and 1, where 0 means totally different and 1 means that the content is identical.

vectorizer

The vectorizer module has only one class, OneHotVectorizer that performs the text conversion into a vector
representations of categorical variables as binary vectors (one hot encoding)

utils

This module includes the LangDetector class to detect the language of a document

Instructions

Normalize text

  1. from txtools.normalizer import TextNormalizer
  2. text = 'Python is a programming language that lets you work quickly' \
  3. ' and integrate systems more effectively'
  4. normalizer = TextNormalizer()
  5. tokens = normalizer.normalize(text, clean=True)
  6. print(tokens)
  7. # output
  8. # ['python', 'programming', 'language', 'let', 'work', 'quickly', 'integrate', 'system', 'effectively']

TextNormalizer class implements the Transformer interface, this allows us to add this class into a scikit-learn pipeline.

Language detection

utils module includes the LangDetector class intended to detect the language in which a document was written. The
detection is done with a certain confidence. You can provide a minimum percentage of confidence for a detection process:

  1. from txtools.utils import LangDetector
  2. documents = [
  3. 'El aprendizaje automático tiene una amplia gama de aplicaciones, incluyendo motores de búsqueda, diagnósticos médicos, detección de fraude en el uso de tarjetas de crédito',
  4. 'Machine learning tasks are classified into several broad categories. In supervised learning, the algorithm builds a mathematical model from a set of data',
  5. 'L\'apprendimento automatico viene impiegato in quei campi dell\'informatica nei quali progettare e programmare algoritmi espliciti è impraticabile',
  6. 'Selon les informations disponibles durant la phase d\'apprentissage, l\'apprentissage est qualifié de différentes manières.'
  7. ]
  8. lang_detector = LangDetector()
  9. for idx, document in enumerate(documents):
  10. print('Document {} is written in {}'.format(idx, lang_detector.lang(document)))
  11. # output
  12. # Document 0 is written in Spanish
  13. # Document 1 is written in English
  14. # Document 2 is written in Italian
  15. # Document 3 is written in French

Also, you can get the ISO 639-1 code:

  1. for idx, document in enumerate(documents):
  2. print('ISO 639-1 code for document {} is {}'.format(idx, lang_detector.iso_639_1_code(document)))
  3. # output
  4. # ISO 639-1 lang code for document 0 is es
  5. # ISO 639-1 lang code for document 1 is en
  6. # ISO 639-1 lang code for document 2 is it
  7. # ISO 639-1 lang code for document 3 is fr

Clustering documents

  1. from txtools.clusterer import KMeansClusters
  2. documents = [] # this must contains a lot of documents
  3. k_means = KMeansClusters(k=10)
  4. clusters = k_means.transform(documents)

Compute document similarity

Similarity creates creates a mxm similarity matrix for a corpus where m is the number of documents.

  1. from txtools.similarity import Similarity
  2. documents = [
  3. 'Psycho is a 1960 American psychological horror film directed and produced by Alfred Hitchcock, and written by Joseph Stefano.',
  4. 'North by Northwest is a 1959 American thriller film directed by Alfred Hitchcock, starring Cary Grant, Eva Marie Saint and James Mason.',
  5. 'The Birds is a 1963 American horror-thriller film directed and produced by Alfred Hitchcock.',
  6. 'Rear Window is a 1954 American Technicolor mystery thriller film directed by Alfred Hitchcock and written by John Michael Hayes based on Cornell Woolrich\'s 1942 short story "It Had to Be Murder".'
  7. ]
  8. similarity = Similarity()
  9. sims = similarity.transform(documents)
  10. print(sims)
  11. # output
  12. # [[0.99999994 0. 0.13075474 0.02665992]
  13. # [0. 1. 0.00812627 0.00331377]
  14. # [0.13075474 0.00812627 1. 0.0069054 ]
  15. # [0.02665992 0.00331377 0.0069054 1. ]]

Using a scikit-learn pipeline

TextNormalizer, OneHotVectorizer, KMeansClusters and Similarity implement the Transformer interface, so we can
add them to a scikit-learn pipeline.

  1. from sklearn.pipeline import Pipeline
  2. from txtools.normalizer import TextNormalizer
  3. from txtools.similarity import Similarity
  4. documents = [
  5. 'Psycho is a 1960 American psychological horror film directed and produced by Alfred Hitchcock, and written by Joseph Stefano.',
  6. 'North by Northwest is a 1959 American thriller film directed by Alfred Hitchcock, starring Cary Grant, Eva Marie Saint and James Mason.',
  7. 'The Birds is a 1963 American horror-thriller film directed and produced by Alfred Hitchcock.',
  8. 'Rear Window is a 1954 American Technicolor mystery thriller film directed by Alfred Hitchcock and written by John Michael Hayes based on Cornell Woolrich\'s 1942 short story "It Had to Be Murder".'
  9. ]
  10. model = Pipeline([
  11. ('norm', TextNormalizer()),
  12. ('sim', Similarity())
  13. ])
  14. sims = model.fit_transform(documents)

Cleaning text

You can clean text with the clean_text function included in the normalizer module

  1. from txtools.normalizer import clean_text
  2. text = '<p><i><b>2001: A Space Odyssey</b></i> \n\nis a 1968 <a href="/wiki/Epic_film" title="Epic ' \
  3. 'film">epic</a> <a href="/wiki/Science_fiction_film" title="Science fiction film">science fiction film</a> ' \
  4. 'produced and directed by\t <a href="/wiki/Stanley_Kubrick" title="Stanley Kubrick">Stanley Kubrick</a>. ' \
  5. 'The screenplay was written by Kubrick and <a href="/wiki/Arthur_C._Clarke" title="Arthur C. ' \
  6. 'Clarke">Arthur C. Clarke</a>, and was inspired by Clarke\'s short story ""<a href="/wiki/The_Sentinel_(' \
  7. 'short_story)" title="The Sentinel (short story)">The Sentinel</a>" and other short stories by Clarke. A ' \
  8. '<a href="/wiki/2001:_A_Space_Odyssey_(novel)" title="2001: A Space Odyssey (novel)">novelisation of the ' \
  9. 'film</a> released after the film\'s premiere was in part written concurrently with the screenplay. The ' \
  10. 'film, which follows a voyage to <a href="/wiki/Jupiter" title="Jupiter">Jupiter</a> with the <a ' \
  11. 'href="/wiki/Sentience" title="Sentience">sentient</a> computer <a href="/wiki/HAL_9000" title="HAL ' \
  12. '9000">HAL</a> after the discovery of a <a href="/wiki/Monolith_(Space_Odyssey)" title="Monolith (Space ' \
  13. 'Odyssey)">featureless alien monolith</a> affecting human evolution, deals with themes of <a ' \
  14. 'href="/wiki/Existentialism" title="Existentialism">existentialism</a>, <a href="/wiki/Human_evolution" ' \
  15. 'title="Human evolution">human evolution</a>, technology, <a href="/wiki/Artificial_intelligence" ' \
  16. 'title="Artificial intelligence">artificial intelligence</a>, and the possibility of <a ' \
  17. 'href="/wiki/Extraterrestrial_life" title="Extraterrestrial life">extraterrestrial life</a>.</p> '
  18. print(clean_text(text))
  19. # output
  20. # 2001: A Space Odyssey is a 1968 epic science fiction film produced and directed by Stanley Kubrick. The screenplay
  21. # was written by Kubrick and Arthur C. Clarke, and was inspired by Clarke's short story "The Sentinel" and other
  22. # short stories by Clarke. A novelisation of the film released after the film's premiere was in part written
  23. # concurrently with the screenplay. The film, which follows a voyage to Jupiter with the sentient computer HAL after
  24. # the discovery of a featureless alien monolith affecting human evolution, deals with themes of existentialism,
  25. # human evolution, technology, artificial intelligence, and the possibility of extraterrestrial life.

License

Copyright (c) 2019 Cisco Delgado

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
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.