Hunting the hunters

Google’s poor decisions will accelerate the implementation of alternative social sites such as http://bitcoinmegaphone.com/ and http://akasha.world/. 

Share

when-the-robots-rise

Political consequences of automation.

Electric cars will kill the oil industry.

Share

Displaylink setup

Recently I purchased a model# 131-00001-35 Displaylink iClever docking station to use with my laptop.

To properly configure, start by downloading and installing the drivers and xrandr. Check the system configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pacman -S dkms
# git clone https://aur.archlinux.org/displaylink.git

# chown pl -R /home/pl/Downloads
$./Downloads/displaylink/makepkg

# pacman -S linux44-headers
pacman -U ./displaylink-1.1.62-2-x86_64.pkg.tar.xz


$ cat /proc/version
Linux version 4.4.21-1-MANJARO (builduser@manjaro) (gcc version 6.2.1 20160830 (GCC) ) #1 SMP PREEMPT Thu Sep 15 19:16:23 UTC 2016
$ pacman -Q xorg-xrandr
xorg-xrandr 1.5.0-1
$ pacman -Q dkms
dkms 2.2.0.3+git151023-12
$ pacman -Q xfwm4
xfwm4 4.12.3-2

Next start the display link service and list available monitors. setprovideroutputsource activates the second display.

1
2
3
4
5
6
7
8
9


$ xrandr --listproviders
Providers: number : 2
Provider 0: id: 0x48 cap: 0xb, Source Output, Sink Output, Sink Offload crtcs: 4 outputs: 5 associated providers: 0 name:Intel
Provider 1: id: 0x12a cap: 0x2, Sink Output crtcs: 1 outputs: 1 associated providers: 0 name:modesetting

$ xrandr --setprovideroutputsource 1 0

With the display running, xrandr will list available resolutions and refresh rates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

$ xrandr
Screen 0: minimum 8 x 8, current 1600 x 960, maximum 32767 x 32767
eDP1 connected 1600x900+0+0 (normal left inverted right x axis y axis) 280mm x 160mm
1920x1080 59.93 +
1400x1050 59.98
1600x900 60.00*
1280x1024 60.02
1280x960 60.00
1368x768 60.00
1280x720 60.00
1024x768 60.00
1024x576 60.00
960x540 60.00
800x600 60.32 56.25
864x486 60.00
640x480 59.94
720x405 60.00
640x360 60.00
DP1 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
VGA1 disconnected (normal left inverted right x axis y axis)
VIRTUAL1 disconnected (normal left inverted right x axis y axis)
DVI-I-1 connected 1280x960+0+0 (normal left inverted right x axis y axis) 410mm x 256mm
1440x900 59.89 +
1280x1024 75.02 60.02
1280x960 60.00*
1152x864 75.00
1152x720 59.97
1024x768 75.08 60.00
832x624 74.55
800x600 75.00 60.32 56.25
848x480 60.00
640x480 75.00 60.00 59.94
720x400 70.08
1280x1024 (0x10b) 108.000MHz +HSync +VSync
h: width 1280 start 1328 end 1440 total 1688 skew 0 clock 63.98KHz
v: height 1024 start 1025 end 1028 total 1066 clock 60.02Hz
1280x960 (0x10c) 108.000MHz +HSync +VSync
h: width 1280 start 1376 end 1488 total 1800 skew 0 clock 60.00KHz
v: height 960 start 961 end 964 total 1000 clock 60.00Hz
1024x768 (0x110) 65.000MHz -HSync -VSync
h: width 1024 start 1048 end 1184 total 1344 skew 0 clock 48.36KHz
v: height 768 start 771 end 777 total 806 clock 60.00Hz
800x600 (0x113) 40.000MHz +HSync +VSync
h: width 800 start 840 end 968 total 1056 skew 0 clock 37.88KHz
v: height 600 start 601 end 605 total 628 clock 60.32Hz
800x600 (0x114) 36.000MHz +HSync +VSync
h: width 800 start 824 end 896 total 1024 skew 0 clock 35.16KHz
v: height 600 start 601 end 603 total 625 clock 56.25Hz
640x480 (0x118) 25.175MHz -HSync -VSync
h: width 640 start 656 end 752 total 800 skew 0 clock 31.47KHz
v: height 480 start 490 end 492 total 525 clock 59.94Hz


Screen 0 “eDP1” is my laptop screen with a resolution of 1600x99 and a refresh rate of 60 MHz.
Screen 1 “DVI-I-1” is my external display with a resolution of 1280x960.
Resolution can be set/changed with the xrandr command

1
2
3
4
5
;;;external
xrandr --output DVI-I-1 --mode 1280x960

;;;built in
xrandr --output eDP1 --mode 1600x900

Upon reboot, changes are lost. To automatically activate on boot, the systemctl displaylink.service command must be started as a service.
Systemctl requires root privileges.

1
2
3
4
# systemctl enable displaylink.service
Created symlink /etc/systemd/system/graphical.target.wants/displaylink.service → /usr/lib/systemd/system/displaylink.service.
#

The xrandr command needs to be executed after the X system is initialized during the boot process. Create an executable file with the required commands:

displaylink.sh
1
2
3
#!/usr/bin/env sh
sleep 10 && xrandr --setprovideroutputsource 1 0 && xrandr --output DVI-I-1 --mode 1280x960

Make the file executable:

1
$chmod +x ./displaylink.sh

Go into settings > settings manager > session and startup > application autostart > add

Add an autostart with the following parameters:
Name: Displaylink
Command: bash /home/<myaccount>/displaylink.sh

With this configuration I need to be docked during boot so the second display can be recognized. If I dock after boot, I can manually execute the ./displaylink.sh command to activate the monitor. Without additional configuration I can close the lid of the laptop, inactivating the built in screen, but retaining signal to the external display.

Share

clockwork-orange

Remake the parents into domesticated animals, and the wildness you fear so much reappears in their offspring as if by spontaneous generation.

Share

Sentiment analysis - DT matrix

As I work with various packages related to text manipulation, I am beginning to realize what a mess the R package ecosystem can turn into. A variety of packages written by different contributers with no coordination amongst packages, overlapping functionality, colliding nomenclature. Many functions for “convenience” when base R could do the job. I also noticed this with packages like dplyr. I have commenced learning dplyr on multiple occasions only to find I don’t need it - I can do everything with base R without loading an extra package and learning new terminology. The problem I now encounter is that as these packages gain in popularity, code snippets and examples use them and I need to learn and understand the packages to make sense of the examples.

In my previous post on text manipulation I discussed the process of creating a corpus object. In this post I will investigate what can be done with a document term matrix. Starting with the previous post’s corpus:

1
2
3

dtm <- DocumentTermMatrix(corp)

There are a variety of methods available to inspect the dtmatrix:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
> dtm
<<DocumentTermMatrix (documents: 17, terms: 5500)>>
Non-/sparse entries: 18083/75417
Sparsity : 81%
Maximal term length: 26
Weighting : term frequency (tf)
> dim(dtm)
[1] 17 5500
> inspect(dtm[2, 50:100])
<<DocumentTermMatrix (documents: 1, terms: 51)>>
Non-/sparse entries: 10/41
Sparsity : 80%
Maximal term length: 9
Weighting : term frequency (tf)

Terms
Docs accentu accept access accid accompani accord account accumul
chapter02.xhtml 0 0 0 0 0 1 1 0
Terms
Docs accus accustom ach achiev acid acquaint acquir acr across
chapter02.xhtml 0 0 0 0 0 0 0 0 0

Note the sparsity is 81%. Remove sparse terms and inspect:

1
2
3
4
5
6
7
> dtms <- removeSparseTerms(dtm, 0.1) # This makes a matrix that is 10% empty space, maximum.   
> dtms
<<DocumentTermMatrix (documents: 17, terms: 66)>>
Non-/sparse entries: 1082/40
Sparsity : 4%
Maximal term length: 26
Weighting : term frequency (tf)

Now sparsity is down to 4%. Calculate word frequencies and plot as a histogram.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
> freq <- sort(colSums(as.matrix(dtm)), decreasing=TRUE)   
> head(freq, 14)
the said like one rock girl now littl miss look mrs know day
367 204 188 184 180 170 167 164 163 148 142 133 122
come
116
> wf <- data.frame(word=names(freq), freq=freq)
> head(wf)
word freq
the the 367
said said 204
like like 188
one one 184
rock rock 180
girl girl 170

>wf$nc <- sapply(as.character(wf$word),nchar)
>wf <- wf[wf$nc >3,]

>p <- ggplot(subset(wf, freq>60), aes(word, freq))
>p <- p + geom_bar(stat="identity")
>p <- p + theme(axis.text.x=element_text(angle=45, hjust=1))
>p

We can use hierarchal clustering to group related words. I wouldn’t read much meaning into this for Picnic, but it is comforting to see the xml/html terms clustering together in the third group - a sort of positive control.

1
2
3
4
5
6
7
8
9

library(cluster)
dtms <- removeSparseTerms(dtm, 0.05) # This makes a matrix that is 10% empty space, maximum.
d <- dist(t(dtms), method="euclidian")
fit <- hclust(d=d, method="ward")
plot(fit, hang=-1)
groups <- cutree(fit, k=4) # "k=" defines the number of clusters you are using
rect.hclust(fit, k=4, border="red") # draw dendogram with red borders around the 4 clusters

We can also use K-means clustering:

1
2
3
4
library(fpc)   
d <- dist(t(dtms), method="euclidian")
kfit <- kmeans(d, 4)
clusplot(as.matrix(d), kfit$cluster, color=T, shade=T, labels=2, lines=0)

Back here I didn’t mention that when creating the epub, it would display fine on my computer, but would not display on my Nook. A solution was to pass the file through Calibre. I diff’ed files coming out of Calibre with my originals but was not able to determine the minimum set of changes required for Nook compatibility. You can download the Calibre modified epub here, and the original here. If you determine what those Nook requirements are, please inform me.

Share

Sentiment analysis - Corpus

In a previous post on text manipulation I discussed text mining manipulations that could be performed with a data frame. In this post I will explore what can be done with a corpus. Start by importing the text manipulation package tm. tm has many useful methods for creating a corpus from various sources. My texts are in a directory as xhtml files, one per chapter. I will use VCorpus(DirSource()) to read the files into a corpus data object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
> library(tm)
>
> myfiles <- paste(getwd(),"/xhtmlfiles",sep="")
> corp <- VCorpus(DirSource(myfiles), readerControl = list(language="en"))
>
> length(corp)
[1] 17
> corp[[2]]
<<PlainTextDocument>>
Metadata: 7
Content: chars: 14639

> writeLines(as.character(corp[[2]]))
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">

<head><meta charset="UTF-8" /></head>
<body><p>Chapter 11</p><p> </p><p> Mrs Fitzhubert at the breakfast table looked out on to the mist-shrouded garden, and decided to instruct the maids to begin putting away the chintzes preparatory to the....

The variable “corp” is a 17 member list, each member containing a chapter. tm provides many useful methods for word munging, referred to as “transformations”. Transformations are applied with the tm_map() function. Below I remove white space, remove stop words, stem (i.e. remove common endings like “ing”, “es”, “s”), etc.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
corp <- tm_map(corp, removeWords, stopwords("english"))
corp <- tm_map(corp, removePunctuation, preserve_intra_word_dashes = TRUE)
prop.nouns <- c("Albert","Miranda","Mike","Michael","Edith","Irma","Sara","Dora","Appleyard","Hussey","Fitzhubert","Bumpher","Leopold","McCraw","Marion","Woodend","Leopold","Lumley","pp","p" )
corp <- tm_map(corp, removeWords, prop.nouns)
corp <- tm_map(corp, content_transformer(tolower))
corp <- tm_map(corp, stemDocument)
corp <- tm_map(corp, stripWhitespace)

> writeLines(as.character(corp[[2]]))
xml version10 encodingutf-8
html xmlnshttpwwww3org1999xhtml

headmeta charsetutf-8 head
bodypchapt 2pp
manmad improv natur picnic ground consist sever circl flat stone serv fireplac wooden privi shape japanes pagoda the creek close summer ran sluggish long dri grass now almost disappear re-appear shallow pool lunch set larg white tablecloth close shade heat sun two three spre

A corpus object allows for the addition of meta data. I will add two events per chapter, which may be useful as overlays during graphing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

meta(corp[[1]], "event1") <- "Exposition of main characters"
meta(corp[[1]], "event2") <- "Journey to the rock"
meta(corp[[2]], "event1") <- "Picnic"
meta(corp[[2]], "event2") <- "Crossing the creak"
meta(corp[[3]], "event1") <- "A surprising number of human beings are without purpose..."
meta(corp[[3]], "event2") <- "Edith screams, girls disappear"
meta(corp[[4]], "event1") <- "Sarah hasn't memorized "The Hesperus""
meta(corp[[4]], "event2") <- "Drag returns from the Rock"
meta(corp[[5]], "event1") <- "Michael interviewed by Constable Bumpher"
meta(corp[[5]], "event2") <- "The red cloud"
meta(corp[[6]], "event1") <- "The garden party"
meta(corp[[6]], "event2") <- "Mike decides to search for the girls"
meta(corp[[7]], "event1") <- "Mike decides to spend the night on the rock"
meta(corp[[7]], "event2") <- "Mike hallucinates on the rock"
meta(corp[[8]], "event1") <- "Michael rescued on the rock"
meta(corp[[8]], "event2") <- "Irma is found alive"
meta(corp[[9]], "event1") <- "Letters to/from parents"
meta(corp[[9]], "event2") <- "Sara informed of her debts to the school"
meta(corp[[10]], "event1") <- "Visit from the Spracks"
meta(corp[[10]], "event2") <- "Michael and Irma meet, date, break up"
meta(corp[[11]], "event1") <- "Michael avoids luncheon with Irma"
meta(corp[[11]], "event2") <- "Fitzhuberts entertain Irma"
meta(corp[[12]], "event1") <- "Irma visits the gymnasium"
meta(corp[[12]], "event2") <- "Mademoiselle de Poitiers threatens Dora Lumley"
meta(corp[[13]], "event1") <- "Reg collects his sister Dora"
meta(corp[[13]], "event2") <- "Reg and Dora die in a fire"
meta(corp[[14]], "event1") <- "Albert describes a dream about his kid sister"
meta(corp[[14]], "event2") <- "Mr Leopold thanks Albert with a cheque"
meta(corp[[15]], "event1") <- "Mrs Appleyard lies about Sara's situation"
meta(corp[[15]], "event2") <- "Mademoiselle de Poitiers reminisces about Sara"
meta(corp[[16]], "event1") <- "Mademoiselle de Poitiers letter to Constable Bumpher"
meta(corp[[16]], "event2") <- "Sara found dead"
meta(corp[[17]], "event1") <- "Newspaper extract"
meta(corp[[17]], "event2") <- ""

> meta(corp[[2]], "event2")
[1] "Crossing the creak"

The corpus object is a list of lists. The main object has 17 elements, one for each chapter, but each chapter element is also a list. The “content” variable of the list is a list of the original xml file contents, with each element being either xml notation, a blank line, or a paragraph of text. Looking at the second chapter’s contents corp[[2]]$content is a list of 18 elements. The first paragraph of the chapter begins with element 6:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> length(corp[[2]]$content)
[1] 18
> corp[[2]]$content[1]
[1] "xml version10 encodingutf-8"
> corp[[2]]$content[2]
[1] "html xmlnshttpwwww3org1999xhtml"
> corp[[2]]$content[3]
[1] ""
> corp[[2]]$content[4]
[1] "headmeta charsetutf-8 head"
> corp[[2]]$content[5]
[1] "bodypchapt 2pp"
> corp[[2]]$content[6]
[1] " manmad improv natur picnic ground consist sever circl flat stone serv fireplac wooden privi shape japanes pagoda the creek close summer ran sluggish long dri grass now almost disappear re-appear shallow pool lunch set larg white tablecloth close shade heat sun two three spread gum in addit chicken pie angel cake jelli tepid banana insepar australian picnic cook provid handsom ice cake shape heart tom oblig cut mould piec tin mr boil two immens billycan tea fire bark leav now enjoy pipe shadow drag keep watch eye hors tether shade"
>

This corpus is the end of the preprocessing stage of the document and will be the input for a document term matrix discussed in the next post

Share

PAHR Sentiment Network

In my previous post on sentiment analysis I used a dataframe to plot the trajectory of sentiment across the novel Picnic at Hanging Rock. In this post I will use the same dataframe of non-unique, non-stop, greater than 3 character words (red line from an earlier post) to create a network of associated words. Words can be grouped by sentence, paragraph, or chapter. I have already removed stop words and punctuation, so I will use my previous grouping of every 15 words in the order they appear in the novel. Looking at my dataframe rows 10 to 20:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> d2[10:20,]
chpt word sentiment lexicon group
10 1 silent positive bing 1
11 1 ridiculous negative bing 1
12 1 supported positive bing 1
13 1 bust negative bing 1
14 1 tortured negative bing 1
15 1 hopeless negative bing 1
16 1 misfit negative bing 2
17 1 clumsy negative bing 2
18 1 gold positive bing 2
19 1 suitable positive bing 2
20 1 insignificant negative bing 2
>

You can see the column “group” has grouped every 15 words. First I create a table of word cooccurences using the pair_count function, then I use ggraph to create the network graph. The number of cooccurences are reflected in edge opacity and width. At the time of this writing, ggraph was still in beta and had to be downloaded from github and built locally. The igraph package provides the graph_from_data_frame function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

library(igraph)
library(ggraph)

word_cooccurences <- d2 %>%
pair_count(group, word, sort = TRUE)

set.seed(1900)
word_cooccurences %>%
filter(n >=4) %>%
graph_from_data_frame() %>%
ggraph(layout = "fr") +
geom_edge_link(aes(edge_alpha = n, edge_width = n)) +
geom_node_point(color = "darkslategray4", size = 5) +
geom_node_text(aes(label = name), vjust = 1.8) +
ggtitle(expression(paste("Word Network ",
italic("Picnic at Hanging Rock")))) +
theme_void()

Lets regroup every 25 words:

1
2
3

d2$group <- sort( c(rep(1:120, 25),rep(121,19)))

And now include only words with 5 occurences or more:

1
2

..... filter(n >=5) %>% ....
Share

PAHR Sentiment Trajectory

In my previous post on sentiment I discussed the process of building data frames of chapter metrics and word lists. I will use the word data frame to monitor sentiment across the book. I am working with non-unique, non-stop, greater than 3 character words (red line from the previous post). Looking at the word list and comparing to text, I can see that the words are in the order that they appear in the novel. I will use the Bing sentiment determinations from the tidytext package to annotate each word as being either of positive or negative sentiment. I will then group by 15 words and calculate the average sentiment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
##make a dataframe of all chapters
##use non.stops which also has words with <=3 chars removed

word <- non.stop[[1]]
chpt <- rep(1, length(word))
pahr.words <-data.frame( cbind(chpt, word))

for(i in 2:17){
word <- non.stop[[i]]
chpt <- rep(i, length(word))
holder <- cbind(chpt, word)
pahr.words <- rbind(pahr.words, holder)
rm(holder)
}

##I checked and words are in the order that they appear
##in the novel
library(tidytext)
bing <- sentiments %>%
filter(lexicon == "bing") %>%
select(-score)



d2 <- pahr.words %>%
inner_join(bing) %>%
cbind(sort( c(rep(1:201, 15),rep(202,4)))) ##this will group words by 15 for averaging sentiment

names(d2)[5]<-"group"

d3 <- count(d2, chpt,group,sentiment)

library(tidyr)

d4 <- spread(d3, sentiment, n)
d4$sentiment <- d4$positive - d4$negative

Plot as a line graph, with odd chapters colored black and even chapters colored grey. I also annotate a few moments of trauma within the narrative.

1
2
3
4
library(ggplot2)
mycols <- c(rep(c("black","darkgrey"),8),"black")
ggplot( d4, aes(group, sentiment, color=chpt)) + geom_line() + scale_color_manual(values = mycols) + geom_hline(yintercept=0, linetype="dashed", color="red") + annotate("text", x = 146, y = -14, label = "Hysteria in the gymnasium") + annotate("text", x = 147, y = -13, label = "x") + annotate("text", x = 12, y = -11, label = "Edith screams on Rock") + annotate("text", x = 35, y = -11, label = "x") + annotate("text", x = 68, y = -13, label = "Bad news delivered\n to Ms Appleyard") + annotate("text", x = 49, y = -13, label = "x")

We can see that the novel starts with a positive sentiment - “Beautiful day for a picnic…” - which gradually moves into negative territory and remains there for the majority of the book.

Does sentiment analysis really work? Depends on how accurately word sentiment is characterized. Consider the word “drag”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
> d2[d2$word=="drag",]
chpt word sentiment lexicon group
133 1 drag negative bing 9
141 1 drag negative bing 10
162 1 drag negative bing 11
169 1 drag negative bing 12
183 1 drag negative bing 13
198 1 drag negative bing 14
199 1 drag negative bing 14
213 1 drag negative bing 15
227 1 drag negative bing 16
250 1 drag negative bing 17
263 1 drag negative bing 18
275 2 drag negative bing 19
300 2 drag negative bing 20
457 3 drag negative bing 31
468 3 drag negative bing 32
585 4 drag negative bing 39
602 4 drag negative bing 41
630 4 drag negative bing 42
633 4 drag negative bing 43
665 4 drag negative bing 45
678 4 drag negative bing 46
679 4 drag negative bing 46
743 5 drag negative bing 50
1224 7 drag negative bing 82
2978 16 drag negative bing 199
>

There are many instances of the word drag annotated as negative. Consider the sentence “It’s a drag that sentiment analysis isn’t reliable.” That would be drag in a negative context. In Picnic, a drag is a buggy pulled by horses, mentioned many times, imparting lots of undeserved negative sentiment to the novel. Drag in Picnic is neutral and should have been discarded. Inspecting the sentiment annotated word list, many other examples similar to drag could be found, some providing negative, some positive sentiment, on average probably cancelling each other out. Even more abundant are words properly annotated, which, on balance may convey the proper sentiment. I would be skeptical, though, of any sentiment analysis without a properly curated word list.

In the next post I will look at what can be done with a corpus.

Share