It is a record of the 80th "Corpus shaping" of Language processing 100 knock 2015. Finally, we have reached Chapter 9, "Vector Space Law (I)". This time, it's easy because it's just character replacement using regular expressions in the preprocessing system.
Link | Remarks |
---|---|
080.Corpus shaping.ipynb | Answer program GitHub link |
100 amateur language processing knocks:80 | I am always indebted to you by knocking 100 language processing |
100 language processing knock 2015 version(80~82) | Chapter 9 was helpful |
type | version | Contents |
---|---|---|
OS | Ubuntu18.04.01 LTS | It is running virtually |
pyenv | 1.2.15 | I use pyenv because I sometimes use multiple Python environments |
Python | 3.6.9 | python3 on pyenv.6.I'm using 9 3.7 or 3.There is no deep reason not to use 8 series Packages are managed using venv |
enwiki-20150112-400-r10-105752.txt.bz2 Is the text of 105,752 articles randomly sampled 1/10 from the English Wikipedia articles as of January 12, 2015, which consist of more than 400 words, compressed in bzip2 format. is there. Using this text as a corpus, I want to learn a vector (distributed expression) that expresses the meaning of a word. In the first half of Chapter 9, principal component analysis is applied to the word context co-occurrence matrix created from the corpus, and the process of learning word vectors is implemented by dividing it into several processes. In the latter half of Chapter 9, the word vector (300 dimensions) obtained by learning is used to calculate the similarity of words and perform analogy.
Note that if problem 83 is implemented obediently, a large amount (about 7GB) of main memory is required. If you run out of memory, devise a process or 1/100 sampling corpus enwiki-20150112-400-r100-10576.txt.bz2 Use /nlp100/data/enwiki-20150112-400-r100-10576.txt.bz2).
This time * "1/100 sampling corpus [enwiki-20150112-400-r100-10576.txt.bz2](http://www.cl.ecei.tohoku.ac.jp/nlp100/data/enwiki-20150112-" 400-r100-10576.txt.bz2) ”* is used.
The simplest way to convert a sentence into a word string is to separate it into words with whitespace characters. However, with this method, symbols such as periods and parentheses at the end of sentences are included in the word. Therefore, divide the text on each line of the corpus into a list of tokens with whitespace characters, and then perform the following processing on each token to remove the symbol from the word.
-Removed the following characters that appear at the beginning and end of the token:.,!?;: () []'" -Delete the token that became an empty string
After applying the above process, concatenate the tokens with spaces and save them in a file.
100 knocks of past language processing "100 knocks of language processing-71 (using Stanford NLP): Stopword" is.
import bz2
import re
#Regular expression for leading and trailing symbol removal(Line feed code at the end)
reg_sym = re.compile(r'^[.,!?;:\(\)\[\]\'"]+|[.,!?;:\(\)\[\]\'"\n]+$')
with bz2.open('./enwiki-20150112-400-r100-10576.txt.bz2', 'rt') as data_file, \
open('./080.corpus.txt', mode='w') as out_file:
for i, line in enumerate(data_file):
#Disassemble with blanks, remove symbols before and after
tokens = []
tokens.extend([reg_sym.sub('', chunk) for chunk in line.split(' ') if len(reg_sym.sub('', chunk)) > 0])
#Blank lines are not applicable
if tokens:
#File output
print(*tokens, sep=' ', end='\n', file=out_file)
#3 lines also output to console
if i < 3:
print(i, line, tokens)
The process will be completed in about 1 minute. I get this result on the console:
0 Anarchism
['Anarchism']
1
[]
2 Anarchism is a political philosophy that advocates stateless societies often defined as self-governed voluntary institutions, but that several authors have defined as more specific institutions based on non-hierarchical free associations. Anarchism holds the state to be undesirable, unnecessary, or harmful. While anti-statism is central, anarchism entails opposing authority or hierarchical organisation in the conduct of human relations, including, but not limited to, the state system.
['Anarchism', 'is', 'a', 'political', 'philosophy', 'that', 'advocates', 'stateless', 'societies', 'often', 'defined', 'as', 'self-governed', 'voluntary', 'institutions', 'but', 'that', 'several', 'authors', 'have', 'defined', 'as', 'more', 'specific', 'institutions', 'based', 'on', 'non-hierarchical', 'free', 'associations', 'Anarchism', 'holds', 'the', 'state', 'to', 'be', 'undesirable', 'unnecessary', 'or', 'harmful', 'While', 'anti-statism', 'is', 'central', 'anarchism', 'entails', 'opposing', 'authority', 'or', 'hierarchical', 'organisation', 'in', 'the', 'conduct', 'of', 'human', 'relations', 'including', 'but', 'not', 'limited', 'to', 'the', 'state', 'system']
The file is directly opened compressed using the bz2 package. Also, the read file and the write file are open at the same time.
with bz2.open('./enwiki-20150112-400-r100-10576.txt.bz2', 'rt') as data_file, \
open('./080.corpus.txt', mode='w') as out_file:
This is the main symbol removal regular expression this time. It is a brief explanation.
Regular expressions | meaning |
---|---|
^ | lead |
[] | Means grouping[] Any of the characters enclosed in |
.,!?;:()[]'" | Symbol to remove. Same at the beginning and end.\ Escape |
+ | lead/Even if there are consecutive symbols at the end |
\n | Line feed code(Only at the end) |
Vertical bar | Union(Or) |
$ | end |
reg_sym = re.compile(r'^[.,!?;:\(\)\[\]\'"]+|[.,!?;:\(\)\[\]\'"\n]+$')
\ xa0
I thought that it was not erased by the symbol later, but when I looked closely at the contents, there was a part containing \ xa0
. I left \ xa0
.
For example, it is in the file where "However B. nutans" is written, and I thought "the dot at the end of B.
has not been removed ", but internally it is B. \ xa0 nutans
. It seems that "B. nutans" is one word.
I noticed by looking at the article "[Python3] What to do if you encounter [\ xa0] during scraping".
Recommended Posts